3

I want to use lambda to get database from dynamodb and generate compute data to cvs file.

Then attach this file with email send to customer.

Step

  1. Get data from dynamodb. (I know how to do it .)

  2. Write to file .CSV ( need help).

Because lambda doesn't have persisted data. How to write to file

  1. Attach .CSV to email and send to customer. ( need help)

2 Answers 2

3

Do 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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

  1. get your data from db
  2. format it how you need it. I'd recommend looking into streams with something like csv-write-stream.
  3. stream data to s3 with something like s3-streaming-upload or the aws-sdk.
  4. as part of the response from the SDK, you'd get the location of your file.
  5. i'd use mandrill (https://mandrillapp.com/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.

1 Comment

I was thinking on this some more and realized you didn't explicitly say you need to save the CSV. If you don't, then definitely ignore the s3 steps since it just complicates what you're trying to do. You can just stream to a buffer then base64 encode that and attach it to an email.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.