├── README.md ├── handler.js ├── package.json └── serverless.yml /README.md: -------------------------------------------------------------------------------- 1 | # Serverless Lambda chaining 2 | 3 | Serverless service which shows how one can chain Lambdas through SNS. 4 | The [configuration](https://serverless.com/framework/docs/providers/aws/events/sns/#sns) which triggers the chain mechanims is defined on [serverless.yml](/serverless.yml). 5 | 6 | ## Installation 7 | 8 | Make sure that you use Serverless v1. 9 | 10 | 1. Run `serverless install --url https://github.com/pmuens/serverless-lambda-chaining` to install the service in your current working directory 11 | 2. Next up cd into the service with `cd serverless-lambda-chaining` 12 | 3. Run `npm install` 13 | 4. Replace the `accountId` in the `handler.js` file with your AWS account id 14 | 5. Deploy with `serverless deploy` 15 | 16 | ## How to use 17 | 18 | 1. Run `serverless invoke --function firstLambda` to trigger the chaining process (this will publish a message to the SNS topic called `dispatcher` the `secondLambda` will listen to) 19 | 2. Run `serverless logs --function secondLambda` to see that the message which was send to the `dispatcher` topic successfully triggered the `secondLambda` function 20 | 21 | ## AWS services used 22 | 23 | - Lambda 24 | - SNS 25 | 26 | ## References 27 | https://serverless.com/framework/docs/providers/aws/events/sns/#sns 28 | -------------------------------------------------------------------------------- /handler.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const AWS = require('aws-sdk'); 4 | 5 | module.exports.firstLambda = (event, context, callback) => { 6 | const sns = new AWS.SNS(); 7 | 8 | const accountId = ''; 9 | 10 | const params = { 11 | Message: 'triggering other Lambda(s)', 12 | TopicArn: `arn:aws:sns:us-east-1:${accountId}:dispatcher` 13 | }; 14 | 15 | sns.publish(params, (error, data) => { 16 | if (error) { 17 | callback(error); 18 | } 19 | callback(null, { message: 'Message successfully published to SNS topic "dispatcher"', event }); 20 | }); 21 | }; 22 | 23 | module.exports.secondLambda = (event, context, callback) => { 24 | // print out the event information on the console (so that we can see it in the CloudWatch logs) 25 | console.log(`I'm triggered by "firstLambda" through the SNS topic "dispatcher":\n${JSON.stringify(event, null, 2)}`); 26 | 27 | callback(null, { event }); 28 | }; 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serverless-lambda-chaining", 3 | "version": "0.1.0", 4 | "description": "Serverless service which shows how one can chain Lambdas through SNS", 5 | "scripts": { 6 | "test": "echo \"Error: no test specified\" && exit 1" 7 | }, 8 | "author": "", 9 | "license": "MIT", 10 | "dependencies": { 11 | "aws-sdk": "^2.6.7" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: serverless-lambda-chaining 2 | 3 | provider: 4 | name: aws 5 | runtime: nodejs4.3 6 | region: us-east-1 7 | stage: dev 8 | iamRoleStatements: 9 | - Effect: "Allow" 10 | Resource: "*" 11 | Action: 12 | - "sns:*" 13 | 14 | functions: 15 | firstLambda: 16 | handler: handler.firstLambda 17 | secondLambda: 18 | handler: handler.secondLambda 19 | events: 20 | - sns: dispatcher 21 | --------------------------------------------------------------------------------