Getting Started with AWS SES
Using AWS SES is quite simple. In this article I will try to cover how to send email through SES (using sandbox) and how to create configuration set. Some tips as well to get out of sandbox and using SES in real world scenarios.
Like many other awesome goodies from amazon, Simple Email Service is one of the very popular and highly scale-able service, used by thousands of businesses and developers across the globe. Some of the notable businesses are HBO, Siemens, Careem etc. Recently got a chance to work on it and I found it very convenient and easy to setup. The combined power of other amazon services (like SNS, SQS, Lambda) with AWS SES makes it work like a charm and fit to your business needs as required. Moreover its quite cheap as well, $0.10 for every 1,000 emails. Click here to see more on pricing.
For people who still don’t know what SES shiz is, here’s how Amazon describes it:
“Amazon Simple Email Service (Amazon SES) is a cloud-based email sending service designed to help digital marketers and application developers send marketing, notification, and transactional emails. It is a reliable, cost-effective service for businesses of all sizes that use email to keep in contact with their customers”
Read more about it here.
That’s enough of introduction, now lets get started. I will try to demonstrate the following:
- Add email recipients and verify them. In sandbox we have to verify all recipients and senders.
- Send email to verified recipients via Lambda function using AWS nodejs sdk.
- Create a configuration set to publish email sending events — like bounces, complaints, deliveries, sent emails, and rejected emails — to a particular email address via AWS SNS.
- Tips about how to get out of sandbox
1. Add Email Recipients:
On SES console home, from Identity Management in left bar, select “Email Addresses” and then click on the big blue button saying “Verify a new Email Address”. Enter your email address in the popup that appears.
Now click on the “Verify This Email Address” button. You will be prompted with a message, check your mailbox, there would be a message waiting for your attention from AWS. Click on the link inside message body of that email to verify your address as a recipient.
Before you confirm your email address, check the ‘Verification Status’ in console.
After you confirm, the status would be changed to confirm. You might need to reload.
Now we can use this email as a sender or recipient. Try adding one more email address similarly. We will require them further to send and receive emails.
2. Send Email Through Lambda and SES:
Goto AWS Lambda console and click on “Create Function” button.
Now select “Author from scratch”, give your lambda function a name, select Node.js 10.x as Runtime and in Role section, select “Create a new role with basic lambda permissions”.
Click on the orange button at bottom right to create your function. You will be redirected to the lambda function editor/designer page.
Before we start coding, we need to allow our lambda function to access AWS SES to send emails, so goto IAM console and click on ‘Roles’.
Find your AWS Lambda role from the list and click it. I named my lambda ‘medium-send-email-via-ses’ so my role name was “medium-send-email-via-ses-role-xe66ua7m”.
In the role summary page, click on “Attach Policies” button under permissions tab.
By default lambda execution role has only access to cloudwatch logs, if we need to use any other services inside it or link our lambda function to other amazon resources, we first have to explicitly grant permissions to the lambda execution role.
After clicking on “Attach Policies” button, search for “AWS SES” in policies and attach “Full access” to it. See image below.
Now go back to your lambda console, paste the code below and click save. This is a simple Node.js program that uses aws sdk to send email via SES. Make sure to change <sender_email> and <recipient_email> to the email addresses that you just verified in first step.
Before testing our function, we will first jump to step 3 — Creating a configuration set that sends bounces, complaints, deliveries, sent emails, and rejected emails to a particular email address via AWS SNS. You can safely skip this step if you want to work inside sandbox environment or you don’t feel the need of it.
3. Creating a Configuration Set:
In SES management console, click on ‘Configuration Sets’ from left sidebar. Read the description mentioned on the page to get a better understanding and then click on ‘Create Configuration Set’. Give it a name and click create.
Click to edit it. In Event destinations tab, add a destination from the dropdown and select SNS.
Select the check boxes according to your need. For demo purposes I have selected Delivery, bounce and complaint. In the topic dropdown select “Create a new Topic”.
Now give SNS topic a suitable name and click create. We will then create an email subscription to this SNS topic which will eventually send all the bounces, complaints and delivery notifications via email to the subscribed address.
Goto SNS console and click on the topic we just created and then click on “Create Subscription”. Select “Email” in protocol and enter your email address. You have to confirm your email for SNS as well just like we did in first step. Make sure to enter an email address other than the ones you have already verified for SES.
I now have a feeling that this article is getting a bit complicated. Just stick around, we are almost done :p
Go back to Lambda console and open the function we created previously. To test the function click on “test” button on top right corner of lambda editor. Search the keyword “SNS” in “Event Template” and select “Amazon SNS topic Notification” and click “Create” button.
After creating sample event, click on ‘Test’ button again to test your lambda function. If everything works fine, you will receive an email on the address you declared in your function. In case of any error you can watch the logs in cloudwatch.
Thats it ! We just send an email via SES. So simple and easy (apart from the configuration set part 😛). Also we got the ‘Mail Delivery’ alert, thanks to SES configuration set that we configured.
The Email via SES flow works like following:
Lambda -> SES -> Email
& the Configuration Set flow works like:
Lambda -> SES -> Configuration Set -> SNS -> Subscribed Email Address
But this was Sandbox and obviously if need to use it practically, we cant do it this way (explicitly verifying every email address first, plus in sandbox we can only send 200 emails a day) and we have to get out of sandbox environment.
4. Tips To Get Out of Sandbox Environment:
We have to request a limit increase to use AWS SES practically, like for marketing, email alerts, push notifications to subscribers etc. If you state your use case clearly, chances are that you will get your limit increase approved within a week. But a few things should be taken care of before applying for it, so that you give them no chance to reject your request:
- Make sure you have setup your Configuration set and you have a plan for your bounces, rejects and complaints etc. If not, AWS representative will reject your request and will ask you to setup configuration set first.
- Fill all fields, even the optional ones (like mail type, website url etc)
- Never ever select “NO” when asked “My email-sending complies with the AWS Service Terms and AUP” :p
- Always select “YES” when asked “I have a process to handle bounces and complaints”
- In Case description, mention your use case clearly and try to justify the limit you are asking for.
- Submit your request and avoid spamming :D !!!
Delete your AWS resources if you are just practicing. That’s it for today, thanks for reading. Please give feedback about the article in comments below. Feel free to ask any question or give your suggestions.