AWS Lambda and AWS SES integration

Prathamesh Mahadik
6 min readNov 26, 2020

Recently, I worked on getting our AWS lambdas to send auto-emails to notify concerned teams about special events on the payload processing. We chose to use AWS SES for the same and these are the simple steps needed to setup the SES+ lambda integration.

Before beginning, let’s understand some key concepts.

Amazon Simple Email Service (SES)

Amazon SES is an email platform that provides an easy, cost-effective way for us to send and receive email using our own email addresses and domains.

Using SES, we can send marketing emails such as special offers, transactional emails such as order confirmations, and other types of correspondence such as newsletters. When we use Amazon SES to receive mail, we can develop software solutions such as email autoresponders, email unsubscribe systems, and applications that generate customer support tickets from incoming emails.

Amazon SES eliminates infrastructure challenges such as email server management, network configuration etc.When we send an email, we send it through an outbound email server. The outbound email server accepts our email content, formats it according to the email standards, and then sends the email out over the Internet. The email may pass through other servers until it reaches a recipient server. The recipient server then delivers the email to the recipient.

When we use Amazon SES, we can do two things —

Using Amazon SES as outbound email server

Using existing email server and configure it to send the outgoing emails through Amazon SES

Note — If an organization already has an email server up and running, then there is no need of using Amazon SES. But let’s say

  1. If the organization is migrating its infrastructure to AWS cloud and it has a separate physical email server, then it can keep this email server and configure this to send the emails through Amazon SES. And if the organization don’t want to send the emails through SES, then also there is no problem at all.
  2. And, if the organization is a startup, then it is very convenient to use Amazon SES, as it is not required to setup a physical email server, which will be expensive.

A sender can generate the email content in different ways. A sender can create the email by using an email client application, or use a program that automatically generates emails, like an application that sends order confirmations in response to purchase transactions.

In our project, we will use a program (Lambda function) that will generate email for us.

AWS Lambda

AWS Lambda is a serverless computing service that lets the user to run code without provisioning or managing servers and the user needs to pay for how much they use. The user can also scale it up and down according to their needs.

Now we’re done with the basics, let’s jump into the project.

Step 1: First of all, we’ll create the policy document, where we’ll specify the permissions for our lambda function.

We’ll go to IAM console and click on Policies to create a new policy, which will be named as myPolicy. In the JSON Policy editor, we’ll paste the following-

{     
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ses:SendEmail",
"ses:SendRawEmail"
],
"Resource": "*"
}
]
}

The above policy will give lambda the following permissions -

  1. SendEmail — It composes an email message and immediately queues it for sending.
  2. SendRawEmail — It also composes an email message and immediately queues it for sending. This operation is more flexible than the SendEmail API operation. When we use the SendRawEmail operation, we can specify the headers of the message as well as its content. This flexibility is useful, for example, when we want to send a multipart MIME email (such a message that contains both a text and an HTML version). We can also use this operation to send messages that include attachments.

Step 2: Now, we’ll attach this IAM policy to an IAM role, which will be assigned to our lambda function later.

In the IAM console, we’ll click on Roles and attach the policy named myPolicy. We’ll name this role as myRole.

Step 3: For sending email, we need to verify the sender’s and receiver’s email address.

Now, we’ll open the Amazon SES console and click on Go to identity management. There we’ll enter the email addresses of the sender and the receiver. A mail will be received by both the sender and the receiver, which they need to open and follow the steps for verification.

In this step, it is important to note the region in which we’re verifying the email addresses. The same region will be used in writing lambda function. Let’s say, we have verified the email addresses in ap-south-1 region. We’ll include the same region in lambda function.

Step 4: We’ll now open the Lambda console and click on Create new function. The name of our function will be myLambda and our runtime will be Node.js 12x. In the Permissions field, we’ll select use existing role and then choose myRole. In the code editor, we’ll write the following code and deploy it.

In the above code, we’ll import AWS SDK. The AWS SDK for JavaScript provides a JavaScript API for AWS services. We can use the JavaScript API to build libraries or applications for Node.js or the browser.

The rest of the codes are self-explanatory.

Step 5: Now, we’ll click on the Test button and then we’ll create new test template to pass information in the event object as an input to the function. Let’s give our Event name as testEvt. We’ll keep the default event object that is in JSON format and we’ll click on Create button. Please note that the content of JSON doesn’t have anything to do with our application, as it is only used as a payload to invoke the lambda function.

Step 6: Now, we’ll again click on the Test button to run the code. After execution, the Execution result will be displayed, where we can see the results of callback(null, {err: err, data: data}); and console.log(err); when the email is not sent and console.log(data); when the email is sent successfully.

Step 7: Now, we’ll open the inbox of the receiver (xyz.123@gmail.com). Hurray, we have an email in our inbox.

Please note that the email says — “via amazonses.com”.

So, we have successfully completed our project.

Summary

In this article, we have understood the basics of Amazon SES, AWS Lambda and finally, we’ve created a lambda function that can send an email using SES.

References

Documentation on AWS Lambda: https://docs.aws.amazon.com/lambda/index.html

Documentation on Amazon SES: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/

--

--