💡Types of Invocation
>>> Understanding the different ways to Invoke the AWS Lambda Functions
Hello World… AWS Lambda functions can be invoked in several ways, each suited to different use cases and operational needs. Understanding the various invocation methods is crucial for optimizing serverless applications' performance, cost, and scalability. This article explores the primary types of AWS Lambda function invocations, including synchronous and asynchronous invocations, and other advanced methods such as event source mappings and manual invocations via the AWS Management Console or API. By examining these invocation types, you’ll gain insights into utilizing AWS Lambda for diverse application scenarios.
What is Lambda Function?
A Lambda function in the context of AWS (Amazon Web Services) is a serverless compute service that allows you to run code without provisioning or managing servers. AWS Lambda automatically scales your application by running code in response to triggers such as data changes, system state shifts, or user actions. You simply write the code and define the event source, and AWS Lambda takes care of executing the code in a highly available environment.
What is Invocation?
In the context of AWS Lambda, it is a process of executing or running a lambda function. When an invocation occurs, an event is passed to the lambda function which then processes the event according to the function’s code and returns a response or performs an action.
Types of Invocations:
- Synchronous Invocation
- Asynchronous Invocation
- Poll Based Invocation
Synchronous Invocation:
- The caller waits for the function to process the event and return a response. The lambda function processes the event and returns a response directly to the caller. It is useful when you need an immediate response from the Lambda function, such as in API Gateway or an application that needs to handle the response immediately.
- Steps:
Invoke the function (using Invoke API with InvocationType set to RequestResponse)
Function Execution
Response. - Working:
Invocation Request triggers the lambda function by making an API call or using an AWS service like API Gateway, AWS SDK, etc to invoke the function.
AWS Lambda Processing — Receives the invocation and starts processing it. Assigns event data to the function, and begins executing the function’s code.
Function Execution — Executes the code, and processes the event data provided in the invocation request.
Timeout Handling — Monitors the execution time of the function. If the function doesn’t complete within the configured time limit, lambda terminates execution.
Response Generation — Once the function completes its execution, it generates a response. This response contains any output or result produced by the function during execution.
Return Response — Lambda sends back the response to the invoker whether it is an API client, another service, or any other that triggered the function.
Completion — After sending the responses, lambda considers that the invocation has been completed. It frees up resources allocated for processing that invocation and waits for the next invocation. - Examples: Web Applications, User Authentication, Data Processing Pipelines, Real-Time Image Processing, Chatbots, etc.
- Advantages:
Timeouts — The client will wait for the Lambda function to complete. If the function takes longer than the configured timeout, the client will receive a timeout error. The default timeout for a Lambda function is 3 seconds, but it can be configured up to 15 minutes.
Error Handling — If the Lambda function fails, the client receives a detailed error response, including error type and stack trace (if available).
Cost — Billing is based on the duration the function runs and the memory allocated to it. Both execution time and the resources consumed during execution impact the cost.
Scalability — AWS Lambda automatically scales the number of function instances in response to the incoming request rate. There is no need to provision or manage servers. - AWS Services:
- Elastic Load Balancing (Application Load Balancer)
- Amazon CloudFront
- Amazon API Gateway
- Amazon Kinesis Data Firehose
- Amazon Alexa
- Amazon Cognito
- Amazon Lex
Asynchronous Invocation:
- It is designed for use cases where the client doesn’t need to wait for the function to complete processing and can continue executing other tasks. Instead of waiting for the function to return a response, the client sends the request and continues with other work, while AWS Lambda processes the event in the background.
- When there’s no need for an immediate response and can allow the function to process the event at a later time. Useful for tasks that can be deferred, such as processing logs or handling large volumes of events.
- Steps:
Invoke the function (using the Invoke API with InvocationType set to Event)
Event Queuing
Function Execution
Error Handling, and Retires. - Working:
Invocation Request triggers the lambda function by making an API call or using an AWS service like API Gateway, AWS SDK, etc to invoke the function.
Accepted Response — Accepts the event.
Event Queuing — The queuing mechanism ensures that events are processed in the order they are received and helps manage the concurrency of function executions.
Function Execution — Executes the code, and processes the event data provided in the invocation request.
Handling Failures — Dead Letter Queue (DLQ) where failed events are sent for further analysis or processing. This allows monitoring and troubleshooting failures without losing the event data.
Completion — Depending on the event source and configuration lambda may perform additional actions such as sending notifications or triggering downstream processes. - Retry Attempts: Range from 0 to 2.
- Exponential Backoff algorithm is used between retries which increases the time between each retry attempt exponentially. This prevents overwhelming downstream services with retry attempts.
- Examples: Batch Processing (Analyzing logs, processing records from a data stream, or performing ETL operations on bulk data), Event-Driven Architectures, Scheduled Tasks, File Processing, Microservices, Webhooks, Asynchronous APIs, etc.
- Advantages:
Event Queuing — Events are queued before being processed by the Lambda function. This helps handle a high volume of incoming requests and smoothens out traffic spikes.
Retries — AWS Lambda retries failed invocations twice, with an exponential backoff strategy. This helps ensure reliability and resilience in the face of transient errors.
Error Handling — Errors can be captured and managed using DLQs, CloudWatch Logs, or AWS Lambda Destinations. DLQs (SQS queues or SNS topics) store failed events for later analysis and processing. AWS Lambda Destinations can be configured to send results to another AWS service based on success or failure.
Cost — Similar to synchronous invocations, the cost is based on the function’s execution duration and the amount of memory allocated. Scalability — AWS Lambda automatically scales to handle the volume of incoming events. It manages the scaling of function instances, ensuring high availability and reliability. - AWS Services:
- Amazon Simple Storage Service
- Amazon Simple Email Service
- Amazon Simple Notification Service
- AWS Config
- AWS CodeCommit
- Amazon CloudWatch Logs
- AWS CloudFormation
- Amazon CloudWatch Events
Poll-Based Invocation:
- Also known as Stream-based Invocation or Event Source Mapping.
- Poll-based invocation in AWS Lambda involves using Lambda to process messages from services like Amazon SQS, Amazon DynamoDB Streams, Amazon Kinesis, and Amazon MSK (Managed Streaming for Apache Kafka). In these scenarios, AWS Lambda polls the service for new records and automatically invokes the Lambda function with the retrieved data. This approach is useful for event-driven architectures where you need to process data streams or message queues asynchronously.
- Steps:
Set up the Source service
Create a Lambda function
Configure the Event Source mapping. - Working:
Invocation Request — AWS Lambda is configured to poll an Amazon SQS queue.
Accepted Response — Lambda detects new messages in the SQS queue and acknowledges them.
Event Queuing — Messages are added to the internal event queue in the order they are received.
Function Execution — Lambda invokes the function with the queued messages. The function processes each message.
Handling Failures — If processing fails, Lambda retries the message. If retries fail, the message is sent to a DLQ.
Completion — After successful processing, the function completes, and any necessary post-processing actions are taken. - Examples: Real-Time Data Processing, Asynchronous Workflows, Data Ingestion and ETL, Microservices Communication, etc.
- Advantages:
Batch Processing — Lambda can read and process multiple records in a single batch, which can improve efficiency and reduce costs. The batch size can be configured according to the workload and performance requirements.
Error Handling and Retries — If the Lambda function fails to process a batch of records, Lambda will automatically retry with the same batch. For persistent errors, you can configure DLQs to capture failed records for later analysis and reprocessing.
Concurrency — Lambda automatically scales the polling and processing to match the volume of incoming data. Concurrency limits can be set to control the maximum number of concurrent executions.
Visibility Timeout (for SQS ) — The visibility timeout determines how long a message remains invisible to other consumers while being processed by Lambda. Ensure the function processes the message within this timeout to avoid duplicates. - AWS Services:
- Amazon SQS
- Amazon DynamoDB Streams
- Amazon Kinesis
- Amazon MSK (Managed Streaming for Apache Kafka)
Let’s Connect:
Feel free to reach out, share your thoughts, or ask any questions. I’m excited to engage with you and learn from each other as we navigate this exciting field!
LinkedIn: Sai Manasa
GitHub: Sai Manasa
Happy Invoke-ing 🫵
Happy Learning💻
