I want to use lambda
to get database from dynamodb
and generate pute data to cvs
file.
Then attach this file with email send to customer.
Step
Get data from dynamodb. (I know how to do it .)
Write to file .CSV ( need help).
Because lambda doesn't have persisted data. How to write to file
- Attach .CSV to email and send to customer. ( need help)
I want to use lambda
to get database from dynamodb
and generate pute data to cvs
file.
Then attach this file with email send to customer.
Step
Get data from dynamodb. (I know how to do it .)
Write to file .CSV ( need help).
Because lambda doesn't have persisted data. How to write to file
- Attach .CSV to email and send to customer. ( need help)
2 Answers
Reset to default 3Do you have an existing setup for sending email? You don't necessarily need to save a file to create a file attachment when sending an email.
simply ignore the fs.readFile statement in the response below
Sending mails with attachment via NodeJS
If you're writing a CSV, you'll probably need to stream the data to S3. Lambda has a pretty good example here for streaming image data from a buffer. Obviously you're not using an image but the concept is about the same.
- get your data from db
- format it how you need it. I'd remend looking into streams with something like csv-write-stream.
- stream data to s3 with something like s3-streaming-upload or the aws-sdk.
- as part of the response from the SDK, you'd get the location of your file.
i'd use mandrill (https://mandrillapp./api/docs/messages.html) because it's
free, easy, and awesome. you can set the attachment content as base64. Yours might look something like this:"attachments": [ { "type": "text/csv", "name": "myfile.csv", "content": new Buffer( myCsvContent ).toString('base64') } ]
I haven't tested this but did something similar recently and this general approach should work for you.