Wisdom of Jim
  • Blog
  • About Jim

A place to obtain the wisdom of Jim.

Sending an Email From AWS Lambda Function in Node.JS (With AWS Simple Email Service)

11/13/2016

Comments

 
Yep, I got this example working today as an example for an upcoming talk I'm given about AWS Lambda. Here's the beautiful email in my inbox, sent from my AWS Lambda function:
Picture

Leverage The Power of Email from Lambda

If you really want to have AWS Lambda completely automate your life (or company) and act as your personal army of virtual elves then you will most likely come to a point where you would like to send a message from your Lambda function either to yourself or to someone else. This message could be in the form of an email, a text message, a mobile alert, etc. All these are possible with Lambda, but in this post I'll go over how to send a message via email using AWS SES (Simple Email Service). 
Picture

Verify Your Email Addresses

It's important to understand how SES works. You don't just fire off emails randomly to any email address (although if you're into that sort of thing you could use something like Twilio from your Lambda function). Instead, you register an email address by entering it in into the AWS SES Email Addresses section and then clicking the link that gets sent to that address in a verification email.
Picture

Give Your Lambda Function Permission To Use SES

Keep in mind that you won't be able to use the default basic execution policy for your lambda function that uses SES to send emails. Instead, you'll need to make a custom policy on AWS IAM that allows these actions. Also in IAM, go to the Roles section.
Picture

You can then click on the role to edit it. Expand the dropdown for Inline Policy and click the button that says Create Policy. You can then create a new custom policy and name it anything you like. Now just fill in the policy text area with this JSON code snippet. Pay special attention to the array corresponding to the "Actions" keyword in the policy object. The last two strings are what give the Lambda function the permissions needed to send emails. ​Here's a working policy for reference: 
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents",
                "ses:SendEmail",
                "ses:SendRawEmail"
            ],
            "Resource": "*"
        }
    ]
}

Write the Node.js Script

Now all that's left to do is write the code for the Lambda function! Lucky for you, I've already done just that, and it's pretty straightforward. 
var aws = require('aws-sdk');
var ses = new aws.SES({
   region: 'us-west-2'
});

exports.handler = function(event, context) {
    console.log("Incoming: ", event);
   // var output = querystring.parse(event);

    var eParams = {
        Destination: {
            ToAddresses: ["jim@wisdomofjim.com"]
        },
        Message: {
            Body: {
                Text: {
                    Data: "Hey! What is up?"
                }
            },
            Subject: {
                Data: "Email Subject!!!"
            }
        },
        Source: "mr.jim@gmail.com"
    };

    console.log('===SENDING EMAIL===');
    var email = ses.sendEmail(eParams, function(err, data){
        if(err) console.log(err);
        else {
            console.log("===EMAIL SENT===");
            console.log(data);


            console.log("EMAIL CODE END");
            console.log('EMAIL: ', email);
            context.succeed(event);

        }
    });

};

Special Thanks

Also special thanks to the answer of this stack overflow question for inspiring the code here. I hope you enjoyed this post and can find lot's of great uses for sending emails from your Lambda functions!
Comments
comments powered by Disqus

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Java
    Javascript
    Lean
    Life
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim

WoJ © 2015 - 2016