Chaining Lambda Functions using the Serverless Framework — Part 2

TL;DR

Prerequisites

Create Lambda Function

serverless create --template aws-nodejs --path lambda-chaining-2
npm install --save aws-sdk
service: lambda-chaining-2
provider:
name: aws
runtime: nodejs6.10
iamRoleStatements:
- Effect: "Allow"
Resource: "*"
Action:
- "sns:*"
'use strict'
const aws = require('aws-sdk')
module.exports.hello = (event, context, callback) => {
let sns = new aws.SNS()
let opts = {
Message: 'payload from invoking lambda',
TopicArn: process.env.mySnsTopicArn,
};
sns.publish(opts, (err, data) => {
if (err) {
console.log('error while sending message over sns: ' + err)
callback(err, null)
} else {
const response = {
statusCode: 200,
body: 'message sent'
}
callback(null, response)
}
})
}
module.exports.ciao = (event, context, callback) => {
let message = event.Records[0].Sns.Message
console.log("received msg: " + message)
const response = {
statusCode: 200,
body: 'ciao world'
}
callback(null, response)
}
custom:
mySnsTopic: "chain"
mySnsTopicArn: "arn:aws:sns:#{AWS::Region}:#{AWS::AccountId}:${self:custom.mySnsTopic}"
functions:
hello:
handler: handler.hello
environment:
mySnsTopicArn: ${self:custom.mySnsTopicArn}
ciao:
handler: handler.ciao
events:
- sns: ${self:custom.mySnsTopic}

Deploy Lambda Functions

serverless deploy -v

Invoke Lambda Function

serverless invoke -f hello -l
serverless logs -f ciao

Conclusion

Links

--

--

--

Inspire and helping people to do what they love and do best. Connecting the dots and creating harmonious environments. Cloud Native Architect @dataone .

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Making containers your most secure run-time environment

How to fully utilize tofuNFT to trade NFTs

Kibana proxy for AWS ECS (Fargate) edit

Flash Stock Rom on Itel Vision 1505

Flash Stock Rom on Itel

Ace Your Next Interview With CodeFights

Building custom Headless CMS that powers Blogging platform

Why You should build your next mobile app with Flutter

Software Engineering at Zymergen

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Noah Ispas

Noah Ispas

Inspire and helping people to do what they love and do best. Connecting the dots and creating harmonious environments. Cloud Native Architect @dataone .

More from Medium

Instantly update your Lambda functions (development)

The curious case of zombie lambdas

PandaDoc API Enhancements: Jan & Feb Edition

Building a REST API in Node.js with AWS Lambda, API Gateway, DynamoDB, and Serverless Framework