Voiced by Amazon Polly |
Introduction
This blog will discuss invoking an AWS Lambda function from another and how this can improve your application, scalability, and performance.
One interesting feature of AWS Lambda is the ability to trigger a Lambda function from another Lambda function. This is useful in scenarios where you create a workflow or series of events triggered by a specific action. There are two ways to call a Lambda function from another Lambda function: Asynchronous and Synchronous.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
Asynchronous vs. Synchronous Invocations
Asynchronous invocation is invoked when the triggering Lambda function does not wait for a response from the invoked Lambda function. Instead, it immediately returns a response and continues its execution. The invoked Lambda function then runs in the background and does not affect the execution of the triggering Lambda function. This approach is useful when the result of the invoked Lambda function is not required to run the Lambda function or when you want to run multiple Lambda functions simultaneously.
Synchronous invocation is invoked when a triggering Lambda function waits for a response from the invoked Lambda function before continuing with its execution. This approach is useful when the initiating Lambda function needs the result of the invoked Lambda function to execute its logic. You can use synchronous calls, for example, to get the result of an API call before making another API call.
When choosing between synchronous and asynchronous invocations, it’s important to consider the requirements of your application and the desired outcome. Synchronous invocation is required when you need the result of the invoked AWS Lambda function to continue your logic. However, the asynchronous invocation is better if you run multiple Amazon Lambda functions simultaneously or don’t need the results of the Lambda functions called.
From a performance point of view, asynchronous invocations are advantageous because they can handle more requests than synchronous invocations. Asynchronous invocation can also help improve the overall responsiveness of your application by allowing multiple Lambda functions to run simultaneously.
Working of Asynchronous and Synchronous Invocations
This section describes how to set up and use the two types of invocations on AWS Lambda: synchronous and asynchronous.
Synchronous Invocation
Create two AWS Lambda functions and set the appropriate triggers for synchronous invocation. To trigger a second Lambda function from the first AWS Lambda function, use the AWS SDK for Lambda to send a request to the second function. Here is an example in Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import boto3 Import json def first_function(event, context): #Trigger second function client = boto3.client(‘lambda’) Response = client.invoke( FunctionName = ‘second_funtion’, InvocationType = ‘RequestResponse’, Payload = json.dumps(event) ) #Get response from second function second_function_response = json.loads(response[‘Payload’].read().decode(‘utf-8)) return second_function_response |
Asynchronous Invocation
To set up an asynchronous invocation, you can use the same process as a synchronous invocation but change the invocation type from “RequestResponse” to “Event”. Here is an example in Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import boto3 import json def first_function(event, context): #Trigger second function client = boto3.client(‘lambda’) Response = client.invoke( FunctionName = ‘second_funtion’, InvocationType = ‘Event’, Payload = json.dumps(event) ) #Continue processing return ‘Second funtion triggered asynchronously’ |
Conclusion
AWS Lambda’s ability to trigger one Lambda function from another offers many benefits, such as easy setup, scalability, cost-effectiveness, and improved performance. Both asynchronous and synchronous invocations have their benefits and drawbacks, and their implementation depends on the specific use case and resources available. Asynchronous regeneration is generally better for performance and complex tasks, while synchronous regeneration is simpler to implement and useful for tasks that do not require real-time updates.
Making IT Networks Enterprise-ready – Cloud Management Services
- Accelerated cloud migration
- End-to-end view of the cloud environment
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 850k+ professionals in 600+ cloud certifications and completed 500+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner, Amazon CloudFront Service Delivery Partner, Amazon OpenSearch Service Delivery Partner, AWS DMS Service Delivery Partner, AWS Systems Manager Service Delivery Partner, Amazon RDS Service Delivery Partner, AWS CloudFormation Service Delivery Partner, AWS Config, Amazon EMR and many more.
FAQs
1. Can I trigger a Lambda function from another AWS service?
ANS: – Yes, you can trigger a Lambda function from another AWS service, such as Amazon S3, Amazon DynamoDB, Amazon SNS, etc. Simply create a trigger for the AWS Lambda function in the desired service and configure the appropriate settings.
2. What happens if the invoked function fails?
ANS: – The response will contain an error message if the invoked function fails. If the invocation type is RequestResponse, the error message will be returned to the calling function. If the invocation type is Event, the error message will be logged to Amazon CloudWatch Logs.
3. Which concept is better for performance?
ANS: – Asynchronous regeneration is generally better for performance, allowing the server to handle multiple requests simultaneously without being blocked. However, it also depends on the specific use case and available resources.

WRITTEN BY Raghavendra Santosh Kulkarni
Raghavendra is a skilled Full Stack Developer with expertise in a wide range of technologies. He has a strong working knowledge of AWS and is always looking to learn about new and emerging technologies. In addition to his technical skills, Raghavendra is a highly motivated and dedicated professional, committed to delivering high quality work.
Comments