Process SNS notifications using AWS Lambda

Process Amazon SNS Notifications with AWS Lambda

Amazon Simple Notification Service (SNS) is a fully-managed, highly-scalable service that facilitates message delivery using a publish/subscribe model.

You can use Amazon Simple Notification Service (SNS) to send messages to email recipients, webhooks, or mobile devices via SMS or push notifications.

With Amazon SNS, you can deliver an unlimited number of notifications without worrying about scaling up. You can use AWS Lambda to customize how messages are processed and integrate messaging with your other AWS resources or 3rd-party services.

In this post, we will learn how to process SNS notifications with a Lambda Function.

As a simple example, you will use Python to upload a file to AWS Simple Object Storage (S3) in response to a message you publish to SNS.

Below image shows the state of the environment before and after.

  1. Create an SNS topic
  2. Create a Lambda function
  3. Configure SNS to trigger your Lambda function
  4. Publish a message in SNS
  5. Verify that Lambda processed your SNS message

1. Create an SNS Topic

AWS Simple Notification Service (SNS) is a pub/sub (publish/subscribe) messaging service. SNS enables sending messages between your AWS services and third-party services or devices. With SNS, communication between services and parties is decoupled, which allows you to build complex yet resilient messaging flows. SNS can be used to send messages to many recipients. A common use-case for SNS is delivering notifications to end-users on mobile devices or desktops.

Two very common use-cases are:

System-to-System messaging: Amazon SNS is a managed messaging service that lets you to decouple publishers from subscribers. This is useful for system-to-system messaging for microservices, distributed architectures, and serverless applications.

User notifications: Amazon SNS lets you send push notifications to mobile apps, text messages to mobile phone numbers, and plain-text emails to email addresses. You can fan out messages with a topic, or publish to mobile endpoints directly.

Let’s start by creating a Topic:

An SNS topic is a named communication channel.

Creating a topic is very simple, just click Create Topic and give it a name and description.

2. Create a Lambda Function to Upload a File to S3

Here we will create a Lambda function. We will configure the Lambda function to run when a message is published to the SNS topic that we created above.

Shown here is the lambda function with an attached role.

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 function code uploads a file into an S3 Bucket which was pre-created as sns-lab-bucket-{account_id}. The name of the file will be the subject of the message and the content of the file will be the message body.

On a side-note, you can use a Lambda function to do many different things, some examples include:

  • Put a custom metric into AWS CloudWatch
  • Add or update a record in a database
  • Post a web request to an external service

3. Configure SNS to trigger the Lambda function

Next, we need to configure a trigger for this Lambda function. To add an SNS trigger, in the Designer section, click Add trigger:

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

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

So far, we have implemented a Lambda function that uploads a file into S3. We then configured our Lambda function to be triggered by the SNS topic that we have created.

4. Publish a Message to a SNS Topic

Now we will publish a message to our SNS topic using the AWS Web Console. Our message will be processed by the Lambda function we created. When you publish the message, your Lambda function will upload a file to an S3 Bucket.

Navigate back to SNS dashboard. Notice now we have 1 topic and 1 subscriber.

Navigate into the topic and click Publish Message:

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.

Publish the message, and check the success message. Look at the subscribers section:

5. Verify the Lambda Function Processed the Message

Go to S3 from AWS console to confirm that there is a message inside the bucket

Thus, we can confirm that our Lambda function was triggered when we published a message to our SNS Topic!