How do I send email using Lambda and Amazon SES?

Last updated: 2019-11-06

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 Lambda using Amazon SES, you need the following:

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

Resolution

Create an IAM policy and execution role

1.    Create an IAM policy using the JSON policy editor. Paste this JSON policy document into the policy editor:

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

Note: For more information, including how to restrict access for this IAM policy, see Example IAM Policies for Amazon SES.

2.    Attach the IAM policy to an IAM role that you plan to assign to your Lambda function.

For detailed steps, see Adding IAM Identity Permissions (Console). Then, follow the instructions under To use a managed policy as a permissions policy for an identity (console).

Verify your Amazon SES identity

Verify the Amazon SES identity (domain or email address) that you'll send Amazon SES messages from.

Create or update your Lambda function

If you haven't done so already, create a Lambda function using the Lambda console or by building and uploading your own deployment package.

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

2.    Choose the name of your function.

3.    On the Configuration pane, under Execution role, confirm that the IAM role with Amazon SES permissions is assigned to your function.

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

Note: Replace us-west-2 with the AWS Region of your verified Amazon SES identity. Replace recipientEmailAddress with an email address that you want to send the email to. (If your account is still in the Amazon SES sandbox in the Region of your verified Amazon SES identity, you must change recipientEmailAddress to an Amazon SES-verified email address in the same account and Region.) 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") to your preferences.

// 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 = (event, context, callback) => {
    
     var params = {
        Destination: {
            ToAddresses: ["recipientEmailAddress"]
        },
        Message: {
            Body: {
                Text: { Data: "Test"
                    
                }
                
            },
            
            Subject: { Data: "Test Email"
                
            }
        },
        Source: "sourceEmailAddress"
    };

    
     ses.sendEmail(params, function (err, data) {
        callback(null, {err: err, data: data});
        if (err) {
            console.log(err);
            context.fail(err);
        } else {
            
            console.log(data);
            context.succeed(event);
        }
    });
};

Note: If you don't change us-west-2 to the Region of your verified Amazon SES identity, you get an error message saying "Email address is not verified" and the error code "MessageRejected". If you run the Lambda function in a Region where Amazon SES isn't supported, you get an error message saying "Inaccessible host" and the error code "UnknownEndpoint".

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

5.    Choose Save.

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 you?

Anything we could improve?


Need more help?