Invoking Lambda from SNS

Invoking Lambda from SNS

The following pages address social networking.

Introduction to SNS with CFN – email version 【Introduction to SNS with CFN - email version】 AWS SNS is a messaging service. In this introductory article, we will show you how to specify Email as the n...

In the above page, we specified an email address as the subscriber.

This time, we will check how to specify a Lambda function as a subscriber.


Diagram of invoking Lambda from SNS

The configuration is generally the same as in the page introduced at the beginning of this article.

Lambda function 2 sends a message to the SNS topic.
In other words, this function acts as a publisher.
The function gets the UNIX time from the current date/time and sends it as a message.

Specify Lambda function 1 as a subscriber to the SNS topic.
This function determines whether the UNIX time passed as a message is even or odd.

CloudFormation template files

The above configuration is built using CloudFormation.
The CloudFormation templates are located at the following URL

Explanation of key points of the template files

(Reference) Publish to SNS with Lambda function

    Type: AWS::Lambda::Function
        - !Ref Architecture
          REGION: !Ref AWS::Region
          TOPIC: !Ref TopicArn
        ZipFile: |
          import boto3
          import datetime
          import os
          import time

          topic = os.environ['TOPIC']
          region = os.environ['REGION']

          client = boto3.client('sns', region_name=region)

          def lambda_handler(event, context):
            now =
            now_str = now.strftime('%Y-%m-%d %H:%M:%S.%f')
            unix_time = str(time.mktime(now.timetuple()))
            print(now_str, unix_time)

            response = client.publish(
      FunctionName: !Sub "${Prefix}-function-02"
      Handler: !Ref Handler
      Runtime: !Ref Runtime
      Role: !GetAtt FunctionRole2.Arn

    Type: AWS::IAM::Role
    DeletionPolicy: Delete
        Version: 2012-10-17
          - Effect: Allow
            Action: sts:AssumeRole
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
        - PolicyName: SNSPublishPolicy
            Version: 2012-10-17
              - Effect: Allow
                  - sns:Publish
                  - !Ref TopicArn
Code language: YAML (yaml)

Publish messages from Lambda functions to SNS.
For details, please refer to the page mentioned at the beginning of this article.

For more information on how to create a Lambda function in CloudFormation, please refer to the following page.

3 parterns to create Lambda with CloudFormation (S3/Inline/Container) 【Creating Lambda with CloudFormation】 When creating a Lambda with CloudFormation, there are three main patterns as follows. Uploading the code to an S3 buc...

This time, we will describe the code to be invoked inline.

Specify Lambda function in SNS subscriber

    Type: AWS::SNS::Topic
        - Endpoint: !Ref FunctionArn1
          Protocol: lambda
      TopicName: !Ref Prefix
Code language: YAML (yaml)

Set the subscriber with the Subscription property.
In this case, the SNS message will be received by a Lambda function, so specify the ARN of the function in the Endpoint property and “lambda” in the Protocol property.

When a Lambda function is specified as a subscriber, the SNS invokes the function internally.
Therefore, it is necessary to authorize the SNS to invoke the function.

    Type: AWS::Lambda::Permission
      FunctionName: !Ref Function1
      Action: lambda:InvokeFunction
      SourceArn: !Ref Topic
Code language: YAML (yaml)

I set it up with reference to the following official AWS page

Receive SNS messages by Lambda function

    Type: AWS::Lambda::Function
        - !Ref Architecture
        ZipFile: |
          def lambda_handler(event, context):
            subject = event['Records'][0]['Sns']['Subject']
            message = event['Records'][0]['Sns']['Message']
            print(subject, message)

            unix_time = int(float(message))

            if (unix_time % 2) == 0:
              print('{int} is even !'.format(int=unix_time))
              print('{int} is odd !'.format(int=unix_time))
      FunctionName: !Sub "${Prefix}-function-01"
      Handler: !Ref Handler
      Runtime: !Ref Runtime
      Role: !GetAtt FunctionRole1.Arn
Code language: YAML (yaml)

Messages sent out from SNS are stored in the event object.
This time, the received UNIX time is determined to be even/odd.


Using CloudFormation, we will build this environment and check the actual behavior.

Create CloudFormation stacks and check resources in stacks

Create CloudFormation stacks.
For information on how to create stacks and check each stack, please refer to the following page

CloudFormation’s nested stack 【How to build an environment with a nested CloudFormation stack】 Examine nested stacks in CloudFormation. CloudFormation allows you to nest stacks. Nested ...

After checking the resources in each stack, information on the main resources created this time is as follows

  • SNS topic: fa-100
  • Lambda function 2: fa-100-function-02
  • Lambda function 1: fa-100-function-01

Check the created resource from AWS Management Console.
Check the SNS topic.

Detail of SNS 1.
Detail of SNS 2.

You can see that the SNS topic has been successfully created.
You can also see that the Lambda function 1 is registered as a subscriber.

Check the Lambda function.

Detail of Lambda 1.
Detail of Lambda 2.

We can see that two functions have also been created successfully.
If we look at function 1, we can also see that invokes from SNS are allowed.

Checking Operation

1st time

Now that we are ready, we invoke Function 2.
From the Test tab of Function 2, click on “Test” to execute it.

Detail of Lambda 3.

You will see that the function has been successfully executed.
You can see that the UNIX time was calculated from the current date and time and that it was sent out as a SNS message.
This time the UNIX time was “1669204672”.

Next, check the log of Function 1.

Detail of Lambda 4.

There is evidence that the function was executed.
This means that the SNS message was published by the publisher function 2, which automatically invoked the subscriber function 1.

As a result of the even/odd determination of UNIX time, “1669204672 is even ! was output.

2nd time

Perform the same operation again.

The following is the execution log of both functions.

Detail of Lambda 5.
Detail of Lambda 6.

This time, a SNS message with UNIX time “1669204701” was sent out by function 2.
Function 1 is automatically executed in response to this and outputs “1669204701 is odd ! is odd !


We have confirmed how to specify a Lambda function to the SNS subscriber.