How do I send email using Lambda and Amazon SES?

Last updated: 2019-07-08

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 sample Lambda function code in Node.js in this article is provided as-is. Adapt the sample 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.

Important: Make sure that you use Amazon SES in the same Region as your Lambda function.

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 this sample function code:

Note: Replace recipientEmailAddress with at least one Amazon SES-verified email address. Replace sourceEmailAddress with your Amazon SES-verified sender email address. Optionally, edit the message body ("Test") and subject line ("Test Email") to your preferences.

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

// 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();

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);
        }
    });
};

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?