Can an AWS Lambda function call another
You should chain your Lambda functions
via SNS
. This approach provides good performance, latency and scalability for minimal effort.
Your first Lambda
publishes messages to your SNS Topic
and the second Lambda
is subscribed to this topic. As soon as messages arrive in the topic, second Lambda
gets executed with the message as it's input parameter.
See Invoking Lambda functions using Amazon SNS notifications.
You can also use this approach to Invoke cross-account Lambda functions via SNS.
here's a sample code for python,
from boto3 import client as boto3_client
from datetime import datetime
import json
lambda_client = boto3_client('lambda')
def lambda_handler(event, context):
msg = {"key":"new_invocation", "at": datetime.now()}
invoke_response = lambda_client.invoke(FunctionName="another_lambda_",
InvocationType='Event',
Payload=json.dumps(msg))
print(invoke_response)
Btw, you would need to add a policy like this to your lambda role as well
{
"Sid": "Stmt1234567890",
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction"
],
"Resource": "*"
}
I found a way using the aws-sdk
.
var aws = require('aws-sdk');
var lambda = new aws.Lambda({
region: 'us-west-2' //change to your region
});
lambda.invoke({
FunctionName: 'name_of_your_lambda_function',
Payload: JSON.stringify(event, null, 2) // pass params
}, function(error, data) {
if (error) {
context.done('error', error);
}
if(data.Payload){
context.succeed(data.Payload)
}
});
You can find the doc here: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html