4

I am confused about sam locals SNS support.

Can only point a Lambda to an existing SNS topic with sam templates or, can sam also create the topic for me too?

I very much want to do the latter if possible.

BUY
  • 705
  • 1
  • 11
  • 30
red888
  • 27,709
  • 55
  • 204
  • 392

1 Answers1

6

I'm not sure when this functionality was added, but you can create SNS topics and Lambda SNS events with SAM local currently... this works with SAM Local version 0.10.0

This will create a Lambda function called "MyLambdaFunction", an SNS topic called "SNSTopicName", and it will tie the two together

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: "Test to create Lambda and SNS with SAM Local"
Resources:
  MyLambdaFunction:
    Type: 'AWS::Serverless::Function'
    Properties:
      CodeUri: .
      Runtime: nodejs8.10
      Handler: index.handler
      Description: "Function that is triggered from an SNS topic"
      MemorySize: 128
      Timeout: 10
      Events:
        SNS1:
          Type: SNS
          Properties:
            Topic:
              Ref: LambdaSNSTopic
  LambdaSNSTopic:
    Type: "AWS::SNS::Topic"
    Properties:
      DisplayName: "My SNS topic"
      Subscription:
        -
          Endpoint:
            Fn::GetAtt:
              - "MyLambdaFunction"
              - "Arn"
          Protocol: "lambda"
      TopicName: "SNSTopicName"

I'm not sure if you are able to use an existing SNS topic

Don W
  • 61
  • 1
  • 4