Synchronous and Asynchronous Invocations in AWS Lambda
AWS Lambda supports two primary invocation types: Synchronous and Asynchronous. Understanding these invocation methods is crucial for optimizing your Lambda functions for different scenarios.
What are Synchronous Invocations?
In a Synchronous Invocation, the caller waits for the Lambda function to complete execution and return a response. This type is best suited for use cases where the result of the function is immediately required.
Real-life Analogy:
Imagine ordering coffee at a café and waiting at the counter until the coffee is prepared. Only after receiving your coffee do you leave.
Example Code:
Here’s a sample Lambda function to demonstrate a synchronous invocation:
import time
import json
def lambda_handler(event, context):
time.sleep(5) # Simulates processing time
print("Processing complete")
return {
"statusCode": 200,
"body": json.dumps("Hello from Lambda!")
}
Execution Flow:
- The caller invokes the Lambda function.
- The caller waits for the function to finish execution (e.g., 5 seconds in this case).
- The function returns the result to the caller.
What are Asynchronous Invocations?
In an Asynchronous Invocation, the caller sends the request to Lambda and receives a response immediately without waiting for the function to complete execution. This method is ideal for background processing tasks.
Real-life Analogy:
Imagine ordering coffee at a café and returning to your table. The staff delivers the coffee to your table once it’s ready.
Key Differences:
- The function execution runs independently.
- The caller is not blocked while waiting for the function to finish.
Asynchronous Invocation via AWS CLI:
You can trigger a Lambda function asynchronously using the AWS CLI:
aws lambda invoke \
--function-name yourLambdaFunctionName \
--invocation-type Event \
output.txt
--invocation-type Event
: Specifies asynchronous invocation.- The response status will be
202 Accepted
, indicating the request has been queued.
Synchronous vs. Asynchronous: Key Differences
Feature | Synchronous Invocation | Asynchronous Invocation |
---|---|---|
Response Time | Caller waits for the function to finish execution. | Caller receives immediate acknowledgment. |
Use Cases | Real-time APIs, user-facing operations. | Background tasks, batch processing. |
Invocation Type | Default when invoked from API Gateway or CLI without flags. | Use --invocation-type Event in CLI. |
Response | Returns the function’s result. | Provides a status acknowledgment. |
Monitoring Asynchronous Invocations
Asynchronous invocations are processed in the background. To monitor these executions:
- Check the CloudWatch Logs for execution details.
- Use the
log_stream_name
from the Lambda context object to trace the specific execution.
Common Use Cases
Synchronous Invocation:
- APIs: Calling a database or fetching data from an external service.
- Real-time operations: Requiring immediate user feedback.
Asynchronous Invocation:
- S3 Trigger: Automatically generating thumbnails for uploaded images.
- Event Processing: Processing logs, analytics, or notifications.
Summary
- Synchronous Invocation: Caller waits for the function to complete, suitable for real-time needs.
- Asynchronous Invocation: Caller queues the request and moves on, ideal for background tasks.
- Choose the invocation type based on your application's requirements.
For more AWS tutorials, visit learning-ocean.com.