Thought leadership from the most innovative tech companies, all in one place.

How to Send an Email Notification Using AWS Lambda

A guide to Amazon SNS.

Photo by Stephen Phillips - Hostreviews.co.uk on Unsplash

Amazon Simple Email Service(SES) and Amazon Simple Notification Service (SNS) are two services that you can use to send emails. SES is a cost-effective and scalable email service that can be used to send several email types, including transactional, marketing, or mass email messages. SNS enables you to send messages to users via SMS, mobile push, and email (Email is only one method of the many methods). With SNS you can send one message to many subscribers. SES and SNS have variations in their costs. If you consider the delivery email, with SES you can send a standard email with all the formatting whereas with SNS you do not have a lot of room for formatting (That is why it is called a notification). So you should consider all these and implement accordingly.

This is an activity you can try using AWS Lambda and SNS. The task is to send an email notification using AWS Lambda.

First, you need to go to Amazon SNS and create a topic. You should create a topic, create a subscription and then publish to a topic. Then all the subscribers will get that notification(to Gmail or any other mail address).

Create Topic screen

Create Topic screen

Choose a topic type (standard).

Choose a topic

Choose a topic

FIFO topics are used in combination with Amazon Simple Queue Service (Amazon SQS) FIFO queues, to build applications that require messages to be sent and processed in a strict sequence and without duplicates.

SNS FIFO is a new feature. Read more here.

The display name will be the “from” address. You can change this later if you wish.

Next, create a subscription. Select “email” as the protocol. Give your preferred email address to the endpoint. You can only give one email address at a time. If you want to send a message to multiple users, you have to create multiple subscriptions. Once the subscription is created, you’ll get an email to subscribe to the topic. Email Subject: AWS Notification — Subscription Confirmation. Click it to confirm.

Create Subscription

Create Subscription

Get the SNS Topic ARN.

image

Create a Lambda function. Paste the below code. Replace the snsArn with your ARN.

import boto3

def lambda_handler(event, context):
    # TODO implement

    client = boto3.client('sns')
    snsArn = 'arn:aws:sns:Region:AccountID:TestTopic'
    message = "This is a test notification."

    response = client.publish(
        TopicArn = snsArn,
        Message = message ,
        Subject='Hello'
    )

One last check. Does your Lambda IAM role have AWS managed policy AmazonSNSFullAccess? Add it and test. That’s all!

Email Received.

Email Received

Let’s assume your lambda function is triggered by some other event. That event output is a JSON string as follows and you want to capture it. Then add it to the email. You have to do a simple change.

{"name":"John", "age":30, "car":"null"}

import boto3

def lambda_handler(event, context):
    # TODO implement

    client = boto3.client('sns')
    snsArn = 'arn:aws:sns:Region:AccountID:TestTopic'
    message = "This is a test notification."

   ** name = event.get("name")**

    response = client.publish(
        TopicArn = snsArn,
        Message = message ,
        Subject='Hello ' + **name**
    )

Test with JSON data

Test with JSON data

That’s all! Thanks for reading.




Continue Learning