How do I send email using Lambda and Amazon SES?

Last updated: 2021-09-15

I want to use AWS Lambda to send email using Amazon Simple Email Service (Amazon SES). How do I configure that?

Short description

To send email from a Lambda function using Amazon SES, do the following:

1.    Create an AWS Identity and Access Management (IAM) policy and execution role for Lambda to run the API call.

2.    Verify your Amazon SES identity (domain or email address).

3.    Create or update a Lambda function that includes logic for sending email through Amazon SES.

Note: To include a PDF attachment in your emails, you must use the Amazon SES SendRawEmail API operation. For more information, see Sending raw email using the Amazon SES API.

Resolution

Note: The example Node.js Lambda function code in this article is provided as-is. Adapt the example to your use case, or design your own in your preferred programming language.

Create an IAM policy and execution role for Lambda to run the API call

1.    Create an IAM policy using the JSON policy editor. When you create the policy, paste the following JSON policy document into the policy editor:

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

Note: For more information and examples of how to restrict access to this policy, see Example IAM policies for Amazon SES.

2.    Attach the IAM policy to an IAM role. For instructions, see the To use a managed policy as a permissions policy for an identity (console) section in Adding IAM identity permissions (console).

Note: You will assign this IAM role to your Lambda function in the following steps.

Verify your Amazon SES identity (domain or email address)

To verify a domain, see Verifying domains in Amazon SES.

To verify an email address, see Verifying email addresses in Amazon SES.

Create or update a Lambda function that includes logic for sending email through Amazon SES

1.    If you haven't done so already, create a Lambda function.

Note: You can create a Lambda function by using the Lambda console or by building and uploading a deployment package.

2.    In the Lambda console, in the left navigation pane, choose Functions.

3.    Choose the name of your function.

4.    On the Configuration tab, in the Permissions pane, look at the function's Execution Role. Verify that the IAM role with Amazon SES permissions that you created earlier is listed. If the correct IAM role isn't listed, assign the correct role to the function.

5.    Under Function code, in the editor pane, paste the following example function code:

Important: Replace us-west-2 with the AWS Region that your verified Amazon SES identity is in. Replace "RecipientEmailAddress", ... with the email address or addresses that you want to send the email to. Replace SourceEmailAddress with your Amazon SES-verified sender email address, or any email address from an Amazon SES-verified domain. Optionally, edit the message body ("Test") and subject line ("Test Email").

// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

var aws = require("aws-sdk");
var ses = new aws.SES({ region: "us-west-2" });
exports.handler = async function (event) {
  var params = {
    Destination: {
      ToAddresses: ["RecipientEmailAddress", ...],
    },
    Message: {
      Body: {
        Text: { Data: "Test" },
      },

      Subject: { Data: "Test Email" },
    },
    Source: "SourceEmailAddress",
  };
 
  return ses.sendEmail(params).promise()
};

For more information on using the sendEmail API, see the AWS SDK for JavaScript documentation.

5.    Choose Deploy.

(Optional) Send a test email

1.    In the Lambda console, configure a test event for your function.

Note: The test payload is required but isn't used for this code example.

2.    Choose Test. Lambda uses Amazon SES to send the test email to your recipient.


Did this article help?


Do you need billing or technical support?