In this article you will see how to set up email forwarding with Amazon SES using Amazon S3 and AWS Lambda.
What are you going to do ?
Let’s say you own a domain
bravokeyl.com and you want to send and receive emails using firstname.lastname@example.org or email@example.com. To acheive this you can leverage AWS services like Amazon SES, Amazon S3 and AWS Lambda.
Amazon SES: It is a mail server that can both send and receive mail on behalf of your domain.
Amazon S3: It is storage for the Internet. It is designed to make web-scale computing easier for developers.
AWS Lambda: It is a compute service that lets you run code without provisioning or managing servers.
Steps involved to set up email forwarding:
- Set up SES to recieve emails for a domain
- Store recieved emails to S3 bucket
- Create a Lambda function to forward recieved email
Before you start recieving emails for a domain using Amazon SES, you must verify the ownership of the domain. You can do this by adding TXT record to the domain’s DNS settings. To automatically route emails sent to your domain to Amazon SES, you need to add MX record to your domain settings. So sign into your AWS account and open SES console and go through this tutorial. Once you have verified your domain and enabled DKIM settings, proceed to next step.
You need to have atleast one active reciept rule set to use SES as email reciever. These rules specify SES what to do when an email is recieved for your domain. So let’s create one rule to store emails recieved to an S3 bucket. Fill the bucket name in the below fig. I used
ses-bravokeyl as S3 bucket and
emails as object prefix which is a folder I created inside the bucket.
With the S3 rule setup, whenever someone sends an email that gets stored in the S3 bucket, you can test it by sending an email to the domain email address and checking the emails (Object key prefix) folder of the ses-bravokeyl (S3 bucket) you created. For more details check the embedded video or the AWS developer guide.
Now that we have successfully stored sent emails to an S3 bucket, it’s time to process the email stored using AWS Lambda. To do this you need to create a lambda function and use AWS Lambda SES Forwarder module. So go to AWS Lambda console and add download this code, unzip it open
index.js file and change the following configuration to reflect your details then zip it and upload it to Lambda.
// Configure the S3 bucket and key prefix for stored raw emails, and the
The critical part is when you creating lambda function create a custom role with the following policy document.
That’s it, now test this by sending an email to the verified address under domain.