How to use Amazon Lambda with Amazon SNS?

Using Amazon SNS with Amazon Lambda
Using Amazon SNS with Amazon Lambda

Amazon SNS (Simple Notification Service) is a fully managed messaging service that allows you to de-couple and scale for applications. Amazon SNS allows you to have application-to-application (A2A) and application-to-person (A2P) communication.

You can read more about the Amazon SNS here. We also have a collection of articles related to Amazon SNS.

In this article, we will create a Lambda function to consume the messages published to a topic in Amazon SNS. The deployment will be using an AWS SAM template.

How does this work?

When an application publishes a message to a topic in the SNS, the SNS invokes your Lambda function asynchronously. As this is an asynchronous invocation, Lambda queues the message and handles retries. In case Amazon SNS can't reach Lambda or the message is rejected, Amazon SNS retries at increasing intervals over several hours

An example of an event with 1 message will be something like this.

{
  "Records": [
    {
      "EventVersion": "1.0",
      "EventSubscriptionArn": "Event Subscription ARN",
      "EventSource": "aws:sns",
      "Sns": {
        "SignatureVersion": "1",
        "Timestamp": "2019-01-02T12:45:07.000Z",
        "Signature": "tcc6faL2yUC6dgZdmrwh1Y4cGa/ebXEkAi6RibDsvpi+tE/1+6582j==",
        "SigningCertUrl": "Certificate used for signing",
        "MessageId": "95df01b4-9903-9903-9903-4c221d41eb5e",
        "Message": "Hello World!",
        "MessageAttributes": {
          "env": {
            "Type": "String",
            "Value": "Test"
          }
        },
        "Type": "Notification",
        "UnsubscribeUrl": "Unsubscribe URL",
        "TopicArn":"ARN of the topic",
        "Subject": "Subject"
      }
    }
  ]
}

How to configure SNS to use with Lambda?

Following is an example of an AWS SAM template that will create an SNS Topic and a Lambda function which will be triggered when a message is published to that topic.

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description:
  Node.js barebone for AWS Lambda
Globals:
  Function:
    Timeout: 300
    Runtime: nodejs14.x
    MemorySize: 512
Resources:
  MainSNSTopic:
    Type: AWS::SNS::Topic
    Properties:
      TopicName: "MainSNSTopic"
  MainFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: MainProcessorFunction
      CodeUri: "src"
      Handler: index.handleEvent
      ReservedConcurrentExecutions: 5
      Events:
        SNSEvent:
          Type: SNS
          Properties:
            Topic: !Ref MainSNSTopic

💻
You can access the template and a sample repository here.

Conclusion:

SNS is a great service that can help you in developing some async serverless applications. We can also club the SNS service along with the SQS to make sure the messages which are published can be queued up for processing. Also, SNS is good for a fan-out approach or a filter based invocation if the use-case calls for it.

I will try to cover the other scenarios in the upcoming blog post.

Subscribe to AWSMAG

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe