Write a Sample Lambda to Send Emails using SES in AWS

What is SES?

Amazon Simple Email Service (SES) is AWS’s server-less, easy to set up, cost-effective solution to send and receive high-volume emails. It eliminates the overhead of building and managing an SMTP server and email infrastructure. SES oversees the complexity of email infrastructure, deliverability, monitoring etc. It allows to send emails asynchronously, from outside the application within seconds. Some of the use-cases where SES is used are:

i) Transactional email for user-triggered actions like account-related activity

ii) Email notifications to existing users such as alerts

iii) Marketing email to targeted lists like newsletters

What features does SES provide?

You can send/receive emails with Amazon Simple Email Service using the console, SMTP interface, or SES API.

  • If you want to use an SMTP-enabled software package or programming language, or integrate SES with your existing mail server, you can use Amazon SES SMTP interface.
  • If you want to call SES using raw HTTP requests, you can use Amazon SES API.

Cost of SES

The cost of sending emails using SES depends on two things — the number of emails sent and the total size of emails. For example, if you send 100k emails with a total email size of 256 GB and additional 100 GB data as attachment, the cost would be :

Cost of number of emails sent = (100000–62,000 free emails) × $0.0001 per email = $3.80

Cost for data = (256 GB + 100 GB) × $0.12 per GB of data = $42.72

Note: Data includes headers, message content (including text and images), and attachments.

Total cost for above example = $46.52

Sample code to send mails using SES in Lambda

In the below sample, the tech stack used is Nodejs Lambda function and Async API sendEmail of SES to asynchronously send emails from a defined from and to address. The following is a high level component design representing what you will be building:

Step 1: Setup emails in SES

AWS SES requires that every email address (or the domain of that address) must be verified before it can be used in “From”, “Source”, “Sender”, or “Return-Path” address. To do this:

  1. Go to SES Service in AWS Console and Click “Email Addresses” under Identity Management. Remember the AWS Region where you are registering the email.
  2. Click on “Verify a new email address” and add the email address to which emails will be sent. Remember email addresses are case sensitive.
  3. The above action will send a verification mail from AWS to the added email address to confirm Id and accept emails. Once the user of that email address verifies, you can check “Verification Status” turns to “verified”.
  4. You can “Send a Test Email” to verify the same and check the stats of sent emails in the SES console.

Note 1: Amazon SES has endpoints in multiple AWS Regions, and the verification status of the email address is separate for each region. If you want to send an email from the same identity in more than one region, you must verify that identity in each region.

Note 2: To send emails to multiple users of a domain, you can verify the domain instead of verifying email addresses individually. Follow the documentation here for verifying domains.

Note 3: To automate the verification process, you can use SES API: VerifyEmailIdentity.

Note 4: You can create your own custom verification email template instead of the standard one sent by AWS.

Step 2: Setup Lambda function to send emails

  1. Once the email is verified, it is now time to create a Lambda. If you have read this article, you already know how to create one. Go to Lambda in AWS console and create a function from scratch. Choose Nodejs runtime, keep all other settings the same, and click on “Create function”.
  2. You need to give permissions to Lambda to allow it to send emails. When you create a function as above, AWS auto-generates an execution role that can be viewed at [Lambda Function] -> Permissions -> Execution Role. Attach a new inline policy to Lambda’s IAM execution role as follows:

3. Now, let’s write the function code in Nodejs runtime that calls async API sendEmail of SES and awaits for the response to ensure lambda processing finishes after the mail is sent.

Note 1: Replace eu-west-1 with the AWS Region of your verified Amazon SES identity in the code. In the case of region mismatch, you will get an error message saying “Email address is not verified” and error code “MessageRejected”

Note 2: If you run the Lambda function in a region where Amazon SES isn’t supported, you will get an error message saying “Inaccessible host” and error code “UnknownEndpoint”

Note 3: If there is an error in configuring any email parameter, you will see an error log similar to below in the console and error code “MissingRequiredParameter”

Step 3: End to End Testing

In the Lambda console, configure any sample test event for your function as there is no test payload required for this example. Click on Test.

And Voila!! You will get a response in terminal saying, “MAIL SENT SUCCESSFULLY!!”. You can head over to the email to confirm:

Thank you for reading! If you found this helpful, here are some next steps you can take:

  1. Check out official AWS SES documentation to know more features!
  2. Send some claps my way!
  3. Follow me on Medium and connect with me on LinkedIn!

Software Development Engineer @amazon | GSoC’18 Mentor@systers | Open source enthusiast | Love to participate in Hackathons | Optimizations is secret of my code

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store