Chatwithcloud logo

ChatWithCloud

AWS GenAI Tools

Calling Lambda Functions From a Different Lambda Function: A Comprehensive Guide

In this guide, we will demonstrate how you can invoke AWS Lambda functions from another Lambda function using the AWS SDK and leveraging TypeScript functionalities. This comes handy for developers who need to design serverless microservices architecture on AWS.

Code

import { Lambda } from 'aws-sdk'; const lambda = new Lambda(); const callLambdaFunction = async () => { const params = { FunctionName: 'YourLambdaFunctionName', InvocationType: 'RequestResponse', Payload: JSON.stringify({ key: 'value' }), }; const result = await lambda.invoke(params).promise(); console.log(`Response from invoked Lambda: ${result.Payload}`); }; callLambdaFunction().catch(error => console.error(error));

Detailed Code Explanation

We first import the Lambda class from aws-sdk package. Then we create an instance of the Lambda class which we will use to call the AWS services.

The callLambdaFunction is an async function that calls the AWS Lambda function. The params object contains the parameters for the invoke function:

The lambda.invoke(params).promise() line is where we call the invoke method of the Lambda class with the params object. We then convert the callback-based method to a Promise-based method.

The last line involves error handling for any errors that might occur during the invocation process and effectively ends with a log statement.

Expected Output

The console.log statement will print the response payload from the invoked Lambda function to the console. Here is an example of the expected output in JSON format:

{ "Response from invoked Lambda": "{ \"output\": \"something\" }" }

Considerations & Caveats

  1. Do not forget to replace 'YourLambdaFunctionName' with the actual name of the Lambda function you're invoking.
  2. AWS SDK refers to your default profile's configuration from your machine for AWS credentials. Make sure you have set them up properly.
  3. Payload data should be a JSON formatted string, not a JavaScript object.
  4. 'RequestResponse' invocation type won't return until the function execution is completed. If your function takes a long time to execute, it might time out.

Required IAM Permissions

The IAM role associated with your Lambda functions needs to have the AWSLambdaExecute and AWSLambdaInvokeFullAccess permissions:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "lambda:InvokeFunction" ], "Resource": "*" } ] }

FAQ

Q1: What happens when I use 'Event' as the InvocationType?

Answer: The 'Event' type is for asynchronous execution. Your invoke call will return as soon as the function execution starts.

Q2: What is the expected response from the invoke method?

Answer: Using 'RequestResponse' as the invocation type, you'll get the response from your Lambda function. In an 'Event' type, you receive an HTTP 202 response.

Q3: Can one Lambda function invoke more than one Lambda function?

Answer: Yes, one Lambda function can invoke multiple Lambda functions, but it must have adequate permissions to do so.

Q4: Will the invoker Lambda function wait till the invoked Lambda function execution completes?

Answer: This depends on your InvocationType. 'RequestResponse' invocation type will wait until the function execution completes, while 'Event' will not.

This ends our comprehensive guide on invoking Lambda functions from another Lambda function.





Related articles
Get the number of invocations for Lambda functions in the last 24 hoursMonitor and Notify When Approaching Service LimitsGet Current IAM Identity TypeScript