Having an issue with setting up SNS triggers

Hello,

I’m trying to create a couple of lambdas that respond to SNS topics. My code is as follows:

  iamRoleStatements:
    - Effect: Allow
      Action:
        - sns:Publish
        - sns:Subscribe
      Resource: !Ref PublishTopic

functions:
  Publish:
    handler: com.PublishHandler
    events:
      - sns:
        arn: !Ref PublishTopic
        topicName: ${self:provider.stage}-publish-topic
        filterPolicy:
          state:
            - start
  SNSToSlack:
    handler: com.SlackHandler
    events:
      - sns:
        arn: !Ref PublishTopic
        topicName: ${self:provider.stage}-publish-topic

And further down I have the following:

resources:
  Resources:
    PublishTopic:
      Type: AWS::SNS::Topic
      Properties:
        DisplayName: ${self:provider.stage}-publish-topic
    PublishTopicEmailSubscription:
      Type: AWS::SNS::Subscription
      Properties:
        Endpoint: <email address here>
        Protocol: email
        TopicArn: !Ref PublishTopic

For some reason, the triggers/subscriptions for the lambdas are never getting created when I deploy. (The email subscription defined in the resources section works fine) If I go into the console and manually set up the triggers this works fine. It seems like the events section is just straight up getting ignored… Am I missing something? And is setting up that email subscription at the bottom messing the other subscriptions up?

You need to add spaces to the events configuration as below:

functions:
  Publish:
    handler: com.PublishHandler
    events:
      - sns:
          arn: !Ref PublishTopic
          topicName: ${self:provider.stage}-publish-topic
          filterPolicy:
            state:
              - start
  SNSToSlack:
    handler: com.SlackHandler
    events:
      - sns:
          arn: !Ref PublishTopic
          topicName: ${self:provider.stage}-publish-topic

See this instruction https://serverless.com/framework/docs/providers/aws/events/sns/

3 Likes