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:
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).
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.
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.
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:
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.
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!
The posts on this site are written and maintained by Jim Lynch. About Jim...