Receive an email, store in S3 bucket, trigger a lambda function
This example shows how to receive an email with SES, store the email including the body on S3 and have S3 trigger a lambda function.
Use-cases
- Postprocess of email body.
Setup
- Create a SES verified Domain but do not setup the "Rule Set"
- The bucket name is stage- and account-qualified by default (
custom.defaultBucket: ses-email-body-${sls:stage}-${aws:accountId}), so it's globally unique out of the box and safe to deploy without editing anything. - If you want a different bucket name, set the
BUCKETenvironment variable (custom.bucket: ${env:BUCKET, self:custom.defaultBucket}) — but the framework derives the CloudFormation logical id for the bucket resource from the resolved bucket name (non-alphanumeric characters stripped, first letter uppercased, prefixed withS3Bucket), and that logical id is referenced bycustom.bucketRefelsewhere in this template. If you overrideBUCKET, you must also updatecustom.bucketRefto match the new logical id — runserverless packageand read the generatedAWS::S3::Bucketlogical id out of.serverless/cloudformation-template-update-stack.jsonto find the exact value. - if you change the region check if SES receiving exists in your region
Deploy
In order to deploy the example, simply run:
serverless deploy
The output should look similar to:
Deploying aws-node-ses-receive-email-body to stage dev (eu-west-1)
✔ Service deployed to stack aws-node-ses-receive-email-body-dev (42s)
functions:
postprocess: aws-node-ses-receive-email-body-dev-postprocess (1.4 kB)
Setup SNS Email Receiving Rule
- Open the Amazon SES console at https://console.aws.amazon.com/ses/
- In the navigation pane, under Email Receiving, choose Rule Sets.
- Choose Create a Receipt Rule.
- On the Recipients page, choose Next Step. (Without a adding any recipients, Amazon SES applies this rule to all recipients)
- For Add action, choose S3.
- For S3 bucket,choose Enter a bucket name and select the bucket with the name you defined in
serverless.yml - Choose Next Step
- On the Rule Details page, for Rule name, type my-rule. Select the check box next to Enabled, and then choose Next Step.
- On the Review page, choose Create Rule.
Usage
Send a test email to the receipient.
You should see a new S3 object in the bucket which contains the whole email body.
After a while, the postprocess function gets triggerd by an S3 event:
serverless logs --function postprocess
START RequestId: 695a6fa8-a711e8-ab5d-0fdb1ebfe5ea Version: $LATEST
<date> <RequestId> date: 2003T18:46:47.000Z
<date> <RequestId> subject: Test Subject
<date> <RequestId> body: Hello World
<date> <RequestId> from: Tim Turbo <tim.turbo@domain.test>
<date> <RequestId> attachments: []
END RequestId: 695a6fa8-a711e8-ab5d-0fdb1ebfe5ea
REPORT RequestId: 695a6fa8-a711e8-ab5d-0fdb1ebfe5ea Duration: 55.12 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 42 MB