Reputation: 499
I'm using Serverless Framework & serverless-offline plugin to develop serverless web application locally, and trying to test the following procedure.
serverless.yml
plugins:
- serverless-offline
- serverless-offline-sns
functions:
publisher:
handler: publisher.main
events:
- http:
path: publish
method: post
cors: true
authorizer: aws_iam
subscriber:
handler: subscriber.main
events:
- sns: test-topic
I tested it on AWS and it worked, but I don't know how to test it locally.
serverless-offline-sns does not support subscription by lambda for now.
serverless-offline-sns supports http, https, and sqs subscriptions. email, email-json, sms, application, and lambda protocols are not supported at this time. https://www.npmjs.com/package/serverless-offline-sns
I think this is a very common use case for serverless & event-driven architecture. How do you test this on local environment?
Upvotes: 2
Views: 5905
Reputation: 2042
I was able to simulate this offline recently using the following code/config
serverless.yml
functions:
########## SNS SUBSCRIPTIONS ##########
newUser:
memorySize: 128
timeout: 120
handler: src/sns-subscribers/newUser.handler
name: sns-newUser-dev
events:
- sns:
arn: arn:aws:sns:ap-southeast-2:13XXXXXXXXXX:new-user-dev
plugins:
- serverless-offline-sns
- serverless-offline
custom:
serverless-offline-sns:
port: 4002 # a free port for the sns server to run on
debug: true
# host: 0.0.0.0 # Optional, defaults to 127.0.0.1 if not provided to serverless-offline
# sns-endpoint: http://127.0.0.1:4002 # Optional. Only if you want to use a custom endpoint
accountId: 13XXXXXXXXXX # Optional
Here's the code that triggers my offline lambda
trigger.js
const AWS = require('aws-sdk');
const sns = new AWS.SNS({
endpoint: 'http://127.0.0.1:4002',
region: 'ap-southeast-2',
});
sns.publish(
{
Message: 'new user!',
MessageStructure: 'json',
TopicArn: `arn:aws:sns:ap-southeast-2:13XXXXXXXXXX:new-user-dev`,
},
() => console.log('new user published'),
);
Run the trigger normally
node trigger.js
Note: In your example, the way you declared the sns subscription is not yet supported AFAIK.
events:
- sns: test-topic # try using ARN and sending this to the next line
You can check this github issue for more info and updates.
Upvotes: 2