2.7 SES: Email Delivery
Mass email delivery is a delicate business—if you do it wrong, your server can get blacklisted. To ease the burden of email server configuration, Amazon has created SES (Simple Email Service). In this lesson we'll expand our example to listen for DynamoDB inserts and send a confirmation email to the customer.
1.Introduction3 lessons, 13:15
2.Amazon Web Services10 lessons, 1:17:39
3.Conclusion1 lesson, 01:25
2.7 SES: Email Delivery
Hi. Welcome back to Explore Amazon Web Services. In this lesson, we are going to add email delivery to our order pipeline, using the simple email service, SES. Before you can start sending emails, you need to verify either a domain or individual email addresses. I've already verified a no-reply address but going to add another one. Once you have started the verification process, you will receive an email with a link you need visit. After that your email will be verified. When using the main verification, you will need to add a domain records to verify ownership and control. When starting out with SES, you will only have send box access. This means you can only send 200 emails per day at the rate of one email per second and must of verified both the sender and recipient. You can request a sending limiting freeze per support ticket for. Which also allows you to send to all possible recipients. You can also use SMTP to send emails, but you need to manually create separate credentials for this first. SES doesn't only allow you to send emails but also to receive them. To use this, you can specify receipt rules that determine what to do when a email is received. For example, when we have an artist email address. We can say that we want to store an email and an S3 bucket. Publish it to a SMS topic or execute a Lambda function. Of course there's also IP address filters to limit allocations you can receive emails from. As an example in our order processing, we want to send an email to the customer stating we have received the order request. We will do this in a Lambda function and use a trigger, so, whenever the Dynamo TV receives a new record, the function will be executed. Since I'm going to use node modules again, I can't edit the function inline. I've already prepared a basic skeleton for lambda functions as I showed you in the Lambda lesson. Since the database trigger can receive an event of bulk data, I'm going to use a sync to process the records in parallel. Now onto the handler function itself. The event we receive is quite complex. This is the content of our event JSON file. You receive a records dictionary, which contains an event name and other metadata. And in the dictionary, the key and the new image dictionary. This is where the data lies. If you receive an update event, you would also have an old image dictionary that contains the data before the update. The dictionary doesn't contain the keys and values directly. Inside the type, and returns string. You have to pass the data yourself. So back to our function. We want to loop over all the records of the event. I'm also going to quickly write the callback function, the looping is done, which is just appropriate Lambda call back. First, we check if the event is an insert. If not, we will ignore it. Then, we can start processing the other. I'm just going to put the new image into a new variable to make it easier to read. To send an email, we have to use the send email function on the SES object. It takes very specific parameters. First, I'm going to set the destination. I can use two addresses to specify the recipients. But there are also CC addresses and BCC addresses. All those are always an array of email addresses. We put our customer email here. Notice the .s, which gets the actual value. Next is the message section, where we first specify a body. We can specify an HTML body and a text body. In both cases, I'm going to specify the character sets to be UTF-8. Then we need to specify the subject, again with the character set. Now, we need to specify the source, or the from address. This has to be a verified email address in all cases. In the callback, we just call next to notify async we are done processing. The test and the shell work great, and I also received an email on my phone. Before I can upload the function to Lambda, I first need to create it. To start off, I'm using the DynamoDB process screen blueprint. It lets me configure an event's right away. Now you see why we need to use async. The event that can trigger the function has a patch size to not flood your account with events. Then we have to set the name and specify a role. There is a role template to use the DynamoDB event stream. So let's use that. In our final verification step, I can decide if I want to enable the event cells right away I'll wait after I'm done testing. Since we tested locally already, I'm pretty confident that this will work on lambda 2. After creation, I need to copy the ARN and paste it into my grant file. Then I can deploy it using the shell. Now that it's done, I can use our producer HTML file to start the chain. It should now use the topic to put a message in the queue. Process Lambda, insert a row into the DynamoDB table, which triggers a Lambda email function. Pretty awesome. When I look at the Lambda monitoring, it shows invocation counts. And when I check the SES console, there is also an increase of the sent emails. Great. Now you know how to send emails using Amazon Simple Email Service. And the next lesson we're going to step away from our order processing, and look at website and asset hosting using S3 and the distribution service CloudFront. See you there.