Gathering detailed insights and metrics for @whisperlab/serverless-offline-sns
Gathering detailed insights and metrics for @whisperlab/serverless-offline-sns
Gathering detailed insights and metrics for @whisperlab/serverless-offline-sns
Gathering detailed insights and metrics for @whisperlab/serverless-offline-sns
npm install @whisperlab/serverless-offline-sns
Typescript
Module System
Node Version
NPM Version
TypeScript (100%)
Total Downloads
216
Last Day
2
Last Week
5
Last Month
9
Last Year
48
123 Stars
310 Commits
87 Forks
4 Watching
4 Branches
42 Contributors
Latest Version
0.77.0
Package Id
@whisperlab/serverless-offline-sns@0.77.0
Unpacked Size
139.92 kB
Size
26.20 kB
File Count
15
NPM Version
8.1.0
Node Version
16.13.0
Cumulative downloads
Total Downloads
Last day
0%
2
Compared to previous day
Last week
150%
5
Compared to previous week
Last month
200%
9
Compared to previous month
Last year
-31.4%
48
Compared to previous year
Looking for a maintainer for this project, email me if you are interested.
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
1npm install serverless-offline-sns --save
Let serverless know about the plugin
1plugins: 2 - serverless-offline-sns
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.
1custom: 2 serverless-offline-sns: 3 port: 4002 # a free port for the sns server to run on 4 debug: false 5 # host: 0.0.0.0 # Optional, defaults to 127.0.0.1 if not provided to serverless-offline 6 # sns-endpoint: http://127.0.0.1:4567 # Optional. Only if you want to use a custom SNS provider endpoint 7 # sns-subscribe-endpoint: http://127.0.0.1:3000 # Optional. Only if you want to use a custom subscribe endpoint from SNS to send messages back to 8 # accountId: 123456789012 # Optional 9 # location: .build # Optional if the location of your handler.js is not in ./ (useful for typescript)
For example, if you would like to connect to AWS and have callbacks coming via ngrok, use:
1serverless-offline-sns: 2 sns-endpoint: sns.${self:provider.region}.amazonaws.com 3 sns-subscribe-endpoint: <ngrok_url> 4 remotePort: 80 5 localPort: <ngrok_port> 6 accountId: ${self:provider.accountId}
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:
1serverless 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.
1functions: 2 pong: 3 handler: handler.pong 4 events: 5 - sns: test-topic
Or you can use the exact ARN of the topic, in 2 ways:
1functions: 2 pong: 3 handler: handler.pong 4 events: 5 - sns: 6 arn: "arn:aws:sns:us-east-1:123456789012:test-topic" # 1st way 7 - sns: "arn:aws:sns:us-east-1:123456789012:test-topic-two" # 2nd way
Here's a demo of some code that will trigger this handler:
1var AWS = require("aws-sdk"); // must be npm installed to use 2var sns = new AWS.SNS({ 3 endpoint: "http://127.0.0.1:4002", 4 region: "us-east-1", 5}); 6sns.publish({ 7 Message: "{content: \"hello!\"}", 8 MessageStructure: "json", 9 TopicArn: "arn:aws:sns:us-east-1:123456789012:test-topic", 10}, () => { 11 console.log("ping"); 12});
Note the region that offline-sns will listen on is what is configured in your serverless.yml provider.
In order to listen to localstack SNS event, if localstack is started with docker, you need the following:
1custom: 2 serverless-offline-sns: 3 host: 0.0.0.0 # Enable plugin to listen on every local address 4 sns-subscribe-endpoint: 192.168.1.225 #Host ip address 5 sns-endpoint: http://localhost:4575 # 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.
In order to listen to a hosted SNS on AWS, you need the following:
1custom: 2 serverless-offline-sns: 3 localPort: ${env:LOCAL_PORT} 4 remotePort: ${env:SNS_SUBSCRIBE_REMOTE_PORT} 5 host: 0.0.0.0 6 sns-subscribe-endpoint: ${env:SNS_SUBSCRIBE_ENDPOINT} 7 sns-endpoint: ${env:SNS_ENDPOINT}
If you want to unsubscribe when you stop your server, then call sls offline-sns cleanup
when the script exits.
If you have multiple serverless services, please specify a root directory:
1custom: 2 serverless-offline-sns: 3 servicesDirectory: "/path/to/directory"
The root directory must contain directories with serverless.yaml files inside.
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 -
1serverless offline-sns start
serverless-offline-sns
supports http
, https
, and sqs
subscriptions. email
, email-json
,
sms
, application
, and lambda
protocols are not supported at this time.
When using sqs
the Endpoint
for the subscription must be the full QueueUrl
returned from
the SQS service when creating the queue or listing with ListQueues
:
1// async 2const queue = await sqs.createQueue({ QueueName: 'my-queue' }).promise(); 3const subscription = await sns.subscribe({ 4 TopicArn: myTopicArn, 5 Protocol: 'sqs', 6 Endpoint: queue.QueueUrl, 7}).promise();
Happy to accept contributions, feature requests and issues.
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
Found 14/18 approved changesets -- score normalized to 7
Reason
SAST tool detected but not run on all commits
Details
Reason
6 existing vulnerabilities detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Score
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More