Process Amazon SNS Notifications with AWS Lambda

Updated: 2023-09-01
4 min read

Lab

Creating an Amazon SNS Topic

1. In the AWS Management Console search bar, enter SNS, and click the Simple Notification Service result under Services:

alt

  1. In the left-hand side menu, click Topics:

alt

If you can’t see the left-hand menu, to expand it, click the following:

alt

  1. Click Create topic:

alt

  1. In the Create topic form, ensure to have selected the Standard type, and enter the following values accepting the defaults for values not specified:
  • Name: lab-topic

alt

You can leave the Display name field empty for this Lab. When you create topics where the recipients receive messages over SMS (Short Message Service) you are required to provide a value.

  1. At the bottom of the form, click Create topic:

alt

Creating an AWS Lambda Function

1. In the AWS Management Console search bar, enter Lambda, and click the Lambda result under Services:

alt

You will see the Functions list page.

  1. Click Create function:

alt

  1. In the Create function form, ensure Author from scratch is selected:

alt

  1. In the Create function form, enter lab-function in the Function name field:

alt

  1. In the Create function form, in the Runtime drop-down, select Python 3.8:

alt

  1. In the Create function form, click Change default execution role and select Use an existing role:

alt

  1. In the Existing role drop-down, select lambda_s3_put:

alt

The role you have selected has been pre-populated for this Lab. Usually when using Lambda you will create a specific role for your function.

  1. To create your function, click Create function:

alt

Implementing an AWS Lambda Function to Upload to S3

  1. Scroll down to the Code source section and double-click lambda_function.py.

  2. In the code editor, replace the contents with the following Python code:

from datetime import datetime
import boto3

account_id = boto3.client('sts').get_caller_identity()["Account"]
s3 = boto3.resource('s3')


def lambda_handler(event, context):
    record = event['Records'][0]['Sns']
    message = record['Message']
    subject = record['Subject']
 
    print("Subject: %s" % subject)
    print("Message: %s" % message)

    s3.Object(f"sns-lab-bucket-{account_id}", subject).put(Body=message)

    return "SUCCESS"

The function code you entered processes a message from SNS. The code uploads a file into an S3 Bucket which was pre-created as a part of this lab. The name of the file will be the subject of the message and the content of the file will be the message body.

You can use a Lambda function to do many different things. Some examples include:

  • Process web-requests
  • Put a custom metric into AWS CloudWatch
  • Add or update a record in a database
  • Post a web-request to an external service
  1. To save your changes and deploy your function, at the top of the Code source section, click Deploy:

alt

You will see a notification that your function has been deployed:

alt

 4. To add an SNS trigger, in the Function overview section, click Add trigger:

alt

alt

  1. In the Select a trigger dropdown, enter SNS, and click the SNS result:

alt

alt

  1. In the SNS topic drop-down, select lab-topic:

alt

alt

The SNS topic field will be filled with the ARN (Amazon Resource Name) of your SNS topic.

  1. To add your SNS trigger, click Add:

alt

alt

You will see a notification that your trigger has been added:

alt

In SNS terminology, by adding an SNS trigger you have “subscribed” your Lambda function to the SNS topic.

Publishing a Message to an Amazon SNS Topic

  1. Navigate to the AWS SNS service.

  2. In the left-hand side menu, click Topics:

alt

  1. In the list of topics, click lab-topic:

alt

  1. Click Publish message:

alt

  1. In the Message details section of the form, in the Subject field, enter lab-subject:

alt

  1. In the Message body section of the form, in the Message body to send to the endpoint textbox, enter Lab Message:

alt

Usually when you publish a message to an SNS topic, you would include meaningful data in the message body. The content of the message body is often called the “payload” of a message. In SNS, the payload can be plain text, or it can be a structured payload such as JSON, XML, or some other format. The service or device subscribed to your topic can use the data in the payload to determine what action to take in response to receiving a message.

  1. To publish your message, click Publish message:

alt

You will see a notification, similar to the following, confirming your message has been published:

alt

Verifying the AWS Lambda Function Processed the Message

1. In the AWS Management Console search bar, enter S3, and click the S3 result under Services:

alt

  1. In the list of S3 Buckets, click the Bucket beginning with sns-lab-bucket-:

alt

  1. In the list of objects you will see a file called lab-subject:

alt

This file was uploaded to the S3 bucket by your Lambda function.