Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda fails to return PDF file

Tags:

I have created a lambda function using serverless. This function is fired via API Gateway on a GET request and should return a pdf file from a buffer. I'm using html-pdf to create the buffer and trying to return the pdf file with the following command

  let response = {     statusCode: 200,     headers: {'Content-type' : 'application/pdf'},     body: buffer.toString('base64'),     isBase64Encoded : true,   };   return callback(null, response); 

but the browser is just failing to load the pdf, so I don't know exactly how to return the pdf file directly to the browser. Could'nt find a solution for that.

like image 972
sami_analyst Avatar asked Jul 27 '17 10:07

sami_analyst


People also ask

Can a Lambda function return a file?

You can't do that. Your file handle only has meaning in the context of your lambda function, it means nothing outside of it.

Can Lambda function return an image?

The following example Python 3 Lambda function can return a binary image from Amazon S3 or text to clients. The function's response includes a Content-Type header to indicate to the client the type of data that it returns.

What happens if a Lambda function fails?

You can retry, send the event to a queue for debugging, or ignore the error. Your function's code might have run completely, partially, or not at all. If you retry, ensure that your function's code can handle the same event multiple times without causing duplicate transactions or other unwanted side effects.

What are the limitations of lambda functions in AWS?

Technical LimitationsThe maximum time a function can run is 15 minutes, and the default timeout is 3 seconds. Obviously, this makes Lambda unsuitable for long-running workloads. The payload for each invocation of a Lambda function is limited to 6MB, and memory is limited to just under 3GB.


1 Answers

well, I found the answer. The settings in my response object are fine, I just had to manually change the settings in API Gateway for this to work in the browser. I have added "*/*" to binary media types under the binary settings in API Gateway console

API GATEWAY

  1. just log into your console
  2. choose your api
  3. click on binary support in the dropdown
  4. edit binary media type and add "*/*"

FRONTEND

opening the api url in new tab (target="_blank"). Probably the browser is handling the encoded base 64 response, In my case with chrome, the browser just opens the pdf in a new tab exactly like I want it to do.

like image 97
sami_analyst Avatar answered Sep 17 '22 13:09

sami_analyst