serverless-offline-sns0.53.0 • Public • Published
A serverless plugin to listen to offline SNS and call lambda fns with events.
For an example of a working application please see serverless-offline-sns-example
This plugin provides an SNS server configured automatically without you specifying an endpoint.
If you'd rather use your own endpoint, e.g. from your AWS account or a localstack SNS server endpoint, you can put it in the custom config. See below for details.
Install the plugin
npm install serverless-offline-sns --save
Let serverless know about the plugin
Note that ordering matters when used with serverless-offline and serverless-webpack. serverless-webpack must be specified at the start of the list of plugins.
Configure the plugin with your offline SNS endpoint, host to listen on, and a free port the plugin can use.
custom:serverless-offline-sns:port: 4002 # a free port for the sns server to run ondebug: false# host: 0.0.0.0 # Optional, defaults to 127.0.0.1 if not provided to serverless-offline# sns-endpoint: # Optional. Only if you want to use a custom endpoint# accountId: 123456789012 # Optional
In normal operation, the plugin will use the same --host option as provided to serverless-offline. The host parameter as shown above overrides this setting.
If you are using the serverless-offline plugin serverless-offline-sns will start automatically. If you are not using this plugin you can run the following command instead:
serverless offline-sns start
Configure your function handlers with events as described in the Serverless SNS Documentation
Here's an example
serverless.yml config which calls a function on an SNS notifcation. Note that the offline-sns plugin will automatically pick up this config, subscribe to the topic and call the handler on an SNS notification.
functions:pong:handler: handler.pongevents:- sns: test-topic
Or you can use the exact ARN of the topic:
functions:pong:handler: handler.pongevents:- sns:arn: "arn:aws:sns:us-east-1:123456789012:test-topic"
Here's a demo of some code that will trigger this handler:
var AWS = ; // must be npm installed to usevar sns =endpoint: ""region: "us-east-1";sns;
Note the region that offline-sns will listen on is what is configured in your serverless.yml provider.
Localstack docker configuration
In order to listen to localstack SNS event, if localstack is started with docker, you need the following:
custom:serverless-offline-sns:host: 0.0.0.0 # Enable plugin to listen on every local addresssns-subscribe-endpoint: 192.168.1.225 #Host ip addresssns-endpoint: # Default localstack sns endpoint
What happens is that the container running localstack will execute a POST request to the plugin, but to reach outside the container, it needs to use the host ip address.
If you use serverless-offline this plugin will start automatically.
However if you don't use serverless-offline you can start this plugin manually with -
serverless offline-sns start
lambda protocols are not supported at this time.
Endpoint for the subscription must be the full
QueueUrl returned from
the SQS service when creating the queue or listing with
// asyncconst queue = await sqs;const subscription = await sns;
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!