Edit on github

#SNS

In the following example we create a new SNS topic with the name dispatch which is bound to the dispatcher function. The function will be called every time a message is sent to the dispatch topic.

functions:
  dispatcher:
    handler: dispatcher.dispatch
    events:
      - sns: dispatch

You're also able to add the same SNS topic to multiple functions:

functions:
  dispatcher:
    handler: dispatcher.dispatch
    events:
      - sns: dispatch
  dispatcher2:
    handler: dispatcher2.dispatch
    events:
      - sns: dispatch

This will run both functions for a message sent to the dispatch topic.

#Using a pre-existing topic

If an arn: is specified, the framework will give permission to the topic to invoke the function and subscribe the function to the topic.

functions:
  dispatcher:
    handler: dispatcher.dispatch
    events:
      - sns: arn:xxx
functions:
  dispatcher:
    handler: dispatcher.dispatch
    events:
      - sns:
          arn: arn:xxx

Or with intrinsic CloudFormation function like Fn::Join or Fn::GetAtt.

functions:
  dispatcher:
    handler: dispatcher.dispatch
    events:
      - sns:
          arn:
            Fn::Join:
              - ":"
              - - "arn:aws:sns"
                - Ref: "AWS::Region"
                - Ref: "AWS::AccountId"
                - "MyCustomTopic"
          topicName: MyCustomTopic

Note: If an arn string is specified but not a topicName, the last substring starting with : will be extracted as the topicName. If an arn object is specified, topicName must be specified as a string, used only to name the underlying Cloudformation mapping resources.

#Setting a display name

This event definition ensures that the aggregator function gets called every time a message is sent to the aggregate topic. Data aggregation pipeline will be shown in the AWS console so that the user can understand what the SNS topic is used for.

functions:
  aggregator:
    handler: aggregator.handler
    events:
      - sns:
          topicName: aggregate
          displayName: Data aggregation pipeline

#Setting a filter policy

This event definition creates an SNS topic which subscription uses a filter policy. The filter policy filters out messages that don't have attribute key pet with value dog or cat.

functions:
  pets:
    handler: pets.handler
    events:
      - sns:
          topicName: pets
          filterPolicy:
            pet:
              - dog
              - cat