This post explains how to build an AWS infrastructure so you can send an email from AWS to a subscribed email account. We will use SQS, SNS and Lambda Function for this example.

Introduction

In AWS the main ways that we have to send an email are:

  • Using Amazon Simple Email Service (SES)
  • Using Amazon Simple Notification Service (SNS)

For this example we are going to use Amazon Simple Notification Service.

You can check the main differences between SES and SNS here

The components

Now i’m going to explain the components that we are going to use.

Amazon Simple Queue Service (SQS)

Amazon Simple Queue Service (SQS) is a fully managed message queuing service that enables you to decouple and scale microservices, distributed systems, and serverless applications. SQS eliminates the complexity and overhead associated with managing and operating message oriented middleware, and empowers developers to focus on differentiating work. Using SQS, you can send, store, and receive messages between software components at any volume, without losing messages or requiring other services to be available.

More Info: SQS

Amazon Simple Notification Service (SNS)

Amazon Simple Notification Service (SNS) is a highly available, durable, secure, fully managed pub/sub messaging service that enables you to decouple microservices, distributed systems, and serverless applications. Amazon SNS provides topics for high-throughput, push-based, many-to-many messaging. Using Amazon SNS topics, your publisher systems can fan out messages to a large number of subscriber endpoints for parallel processing, including Amazon SQS queues, AWS Lambda functions, and HTTP/S webhooks. Additionally, SNS can be used to fan out notifications to end users using mobile push, SMS, and email.

More Info: SNS

AWS Lambda

AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume – there is no charge when your code is not running.

With Lambda, you can run code for virtually any type of application or backend service – all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app.

More Info: Lambda

Objective

We are going to build a system using a SQS queue where we are going to send the body of the email. The lambda that long pools the queue will be triggered when the message is sent to the queue and will publish the message to the SNS topic so we can send an email with the message body.

More Info: Long Pooling

Lets Go

Create a SQS Queue where we are going to send the email body message. This queue will be a Standard Queue. This example will not have a deadletter queue configurated, but is always good to have one.

queue

We need to create a SNS Topic. An Amazon SNS topic is a logical access point which acts as a communication channel.

topic
topic

Create a subscription to that topic. The subscription resource subscribes an endpoint to an Amazon Simple Notification Service (Amazon SNS) topic. For a subscription to be created, the owner of the endpoint must confirm the subscription. The protocol of the subscription will be Email. The endpoint will be the email address that will receive the email. For this example i used a temporary mail so that i didn’t fill my personal inbox with emails.

More Info: Temp-Mail

subscription

After you configure the subscription you will receive an email with a link to confirm the subscription.

email

When you click on the link you will be redirected to the subscription confirmation page

confirmed

Create the lambda that will use long pooling to check if there are new messages in the queue and publish the messages to the SNS Topic.

In this example i used Python to develop the lambda and called it “EmailLambda”. Note: you need to set the role of the lambda so that the lambda has permissions to long pool the sqs queue and publish a message to the SNS topic.

More Info: SNS IAM Roles SQS IAM Roles

lambda

Here is the lambda code that i used.

#!/usr/bin/python3
import json
import boto3
import os

def lambda_handler(event, context):
    batch_processes=[]
    for record in event['Records']:
        send_request(record["body"])
		

def send_request(body):
    # Create an SNS client
    sns = boto3.client('sns')

    # Publish a simple message to the specified SNS topic
    response = sns.publish(
        TopicArn=os.environ['email_topic'],    
        Message=body,    
    )

    # Print out the response
    print(response)
 

Set the lambda enviroment variable email_topic to the ARN of the SNS Topic we created earlier.

topic

Add the lambda trigger and set it with the sqs queue that will receive the body of the email.

trigger

Lets Test it !!!

Now the only thing that we need to do is send a new message to the SQS queue with the email body that we want.

send message
Send message

Now we will receive an email from no-reply@sns.amazonaws.com with the body of the queue message.

Email

And there you have it. An email notification in AWS using SQS, SNS and Lambda.

Thanks for reading this post and have an…

Image result for aws awesome day

LEAVE A REPLY

Please enter your comment!
Please enter your name here