Nexeh
Nexeh

Reputation: 263

CSV Export using Api Gateway and Lambda

What I would like to do:

What I would like to do is have a url which would return to the caller a CSV file which is essentially a export of data. I would like this to remain to be a serverless solution.

What I have done:

I have created an AWS API Gateway with the URL I want. I have created a lambda that will query the database and create a CSV string of that data. That data is placed in a JSON object and returned. API gateway then gets the CSV data from the json object and returns CSV to the caller with appropriate headers to indicate tht it is a CSV and attachment. Testing from the browser I get the download automatically just like I intended.

The problem I see:

This works well until there is a sizable amount of data at which point I start getting "body size is too long".

My attempts to resolve:

I did some googling around and I see others have had similar issues. In one solution I saw that they return a link to the file that they created. This solution seems viable for them because they had a server. For my serverless architecture it seems to be a little trickier. I could take and store the file into S3 but then i would have to return a link to S3. That seems like it could work but doesn't feel right like im missing a configuration option. It also feels like im exposing the implementation by returning the s3 urls as well.

I have looked around for tutorials and example of people doing similar things and i haven't found any.

My Questions:

Is there a way to do this? Is there another solution that i dont know of? How do i return a file, in this case CSV, from API Gateway of a larger size

Upvotes: 5

Views: 8399

Answers (1)

Mark B
Mark B

Reputation: 200850

There is a limit of 6 MB for AWS Lambda response payloads. If the files you need to server are larger than that you won't be able to serve them directly from Lambda.

Using S3 to store and serve the files is the standard way of doing something like this. I would leave the S3 bucket private and generate S3 Pre-signed URLs in the Lambda function. That will limit the time that the CSV file is available for download, and it will prevent people from being able to guess the URLs of files you are serving. You would use an S3 Lifecycle Policy to archive or delete the files after a period of time.

Upvotes: 15

Related Questions