Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Compile SNS Events

This plugins compiles the function SNS event to a CloudFormation resource.

How it works

Compile SNS Events hooks into the deploy:compileEvents lifecycle.

It loops over all functions which are defined in serverless.yml. For each function that has a SNS event defined, a corresponding SNS topic will be created.

You have two options to define the SNS event:

The first one is to use a simple string which defines the "Topic name" for SNS. The lambda function will be triggered every time a message is sent to this topic.

The second option is to define the SNS event more granular (e.g. the "Topic name" and the "Display name") with the help of key value pairs.

Take a look at the Event syntax examples below to see how you can setup a SNS event.

A corresponding lambda permission resource is created for the SNS event.

Those two resources are then merged into the compiled CloudFormation template.

Event syntax examples

Simple SNS setup

This setup specifies that the forward function should be run every time a message is sent to the "messages" SNS topic.

# serverless.yml
functions:
  forward:
    handler: message.forward
    events:
      - sns: messages

SNS setup with extended event options

This configuration sets up a SNS topic with the name "lambda-caller". The "Display name" of the topic is "Used to chain lambda functions". The run function is executed every time a message is sent to the "lambda-caller" SNS topic.

# serverless.yml
functions:
  run:
    handler: event.run
    events:
      - sns:
          topicName: lambda-caller
          displayName: Used to chain lambda functions

SNS setup with pre-existing topic ARN

If you already have a topic that you've created manually, you can simply just provide the topic arn instead of the topic name using the topicArn property. Here's an example:

# serverless.yml
functions:
  run:
    handler: event.run
    events:
      - sns:
          topicArn: some:arn:xxx

Or as a shortcut you can provide it as a string value to the sns key:

# serverless.yml
functions:
  run:
    handler: event.run
    events:
      - sns: some:arn:xxx

The framework will detect that you've provided an ARN and will give permission to SNS to invoke that function. You need to make sure you subscribe your function to that pre-existing topic manually, as there's no way to add subscriptions to an existing topic ARN via CloudFormation.