Nodejs - Invoke an AWS.Lambda function from within another lambda function
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
AWS Lambda is a serverless computing service that lets you run code without provisioning or managing servers. Node.js, being lightweight and efficient, is a popular choice for AWS Lambda functions. One common scenario in AWS Lambda applications is invoking one Lambda function from another. This can be part of a microservices architecture or simply a way to modularize and organize a large application. In this article, we will explore how to invoke an AWS Lambda function from within another Lambda function using Node.js.
Prerequisites
To follow this guide, you need:
- Basic knowledge of AWS Lambda and Node.js.
- AWS SDK for JavaScript (AWS-SDK).
- IAM role with permissions to invoke Lambda functions.
Setting Up AWS-SDK in Lambda
Before we can invoke a Lambda function, we need to ensure that our function has included the AWS-SDK, which is pre-installed in the AWS Lambda Node.js runtimes. However, we still need to require it in our code:
IAM Role Configuration
To enable your Lambda function to invoke another function, the executing function must have the appropriate permissions. This is typically managed through IAM roles. You need to add the following permission to your IAM role policy:
Replace REGION, ACCOUNT_ID, and TARGET_FUNCTION_NAME with your specific details.
Invoking Another Lambda Function
Once we have the AWS-SDK set up and the appropriate permissions, we can write a function to invoke another Lambda. Here's a step-by-step explanation:
Step 1: Configure the Lambda Client
We need to create a Lambda client object using the AWS-SDK:
Step 2: Define the Invocation Parameters
Prepare the parameters, which include the function name and any payload data you want to pass:
Step 3: Invoke the Target Lambda
Invoke the Lambda function using the invoke method:
Example Code
Here is a complete example encapsulated in an AWS Lambda handler:
Handling Invocation Responses
Depending on the InvocationType specified, you can receive different types of responses:
- RequestResponse: Waits for function execution to complete and returns the response.
- Event: Asynchronous invocation, returns immediately without waiting for the function to complete.
Handling responses and errors appropriately will depend on your application's requirements.
Additional Considerations
Cold Start Latency
Invoking a Lambda function can sometimes experience cold start latency, especially if the function has not been invoked recently. Consider this when designing real-time applications.
Monitoring and Logging
AWS CloudWatch can be used to monitor and log invocations, helping you debug failures and optimize performance.
Security Best Practices
- Ensure your Lambda function's permissions are limited to only the required resources.
- Secure any sensitive data or environment variables passed with your payloads.
Summary Table
| Factor | Details |
| Setup | AWS-SDK is pre-installed; IAM role needs invoke permissions |
| Parameters | FunctionName, InvocationType, Payload |
| Invocation Types | RequestResponse, Event |
| Error Handling | Via callback or promise rejection |
| Region and Permissions | Ensure correct region & permissions are set |
| Monitoring | Use CloudWatch for monitoring and debugging |
Conclusion
Invoking one AWS Lambda function from another can significantly enhance modularity and separation of concerns in your applications. With Node.js and AWS-SDK, it's a straightforward task but requires proper permission setup and an understanding of invocation types and their side-effects. By following the steps outlined in this article, you'll be able to efficiently chain Lambda functions in your serverless applications.
Related reading
- NodeJS 14.x - Native AWS Lambda Import/Export Support
- node.js AWS dynamodb updateItem
- Nodejs AWS SDK S3 Generate Presigned URL
- NodeJS How do I Download a file to disk from an aws s3 bucket?
- NodeJS 7 How to Show the Correct Stack Trace in Async Mongoose Promises
- Node.js + Nginx - What now?
- NodeJS with DynamoDB throws error AttributeValue may not contain an empty string
- Not able to connect to Kafka on AWS EC2

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.