AWS: How to get one lambda to trigger another lambda
This lambda will trigger the second lambda.
Step 1 - Configure IAM
First create an IAM role that will allow you to not only trigger a lambda but also allow one lambda to trigger another lambda.
My role will be named role-lambda-basic-execution
.
Step 2 - Create Lambda Function #1
This lambda function is actually pretty handy. If you provide it with an IP address, it will respond back with the geo location (lat, lng). I will name it TrackIp
.
The code is written in ECMA Script 6 for Node v8.10.
exports.handler = async (event) => {
// TODO implement
return 'Hello from Lambda!'
};
Step 3 - Create Lambda Function #2 (Caller)
Similar to the functin above, I will create a new method named caller
.
var aws = require('aws-sdk');
var lambda = new aws.Lambda({
region: 'us-west-2' //change to your region
});
exports.handler = async (event, context, callback) => {
lambda.invoke({
FunctionName: 'arn:aws:lambda:us-west-2:xxxxxx:function:TrackIP',
Payload: JSON.stringify(event, null, 2)
}, function(error, data) {
if (error) {
context.done('error', error);
}
if(data.Payload){
context.succeed(data.Payload)
}
});
};