DEV Community

Cover image for AWS Lambda and Its Invocation Models
Emi Roberti
Emi Roberti

Posted on

AWS Lambda and Its Invocation Models

AWS Lambda is one of the most versatile and efficient serverless computing solutions available. By abstracting server management, AWS Lambda allows developers to focus solely on writing and deploying code without worrying about provisioning or maintaining infrastructure. Below, we delve into the various invocation models and key characteristics of AWS Lambda.

Lambda Synchronous Invocation

Synchronous invocation is the simplest way to call an AWS Lambda function. In this model, the caller waits for the Lambda function to process the request and return a response. Common use cases include:

API Gateway Integration: When an HTTP request is sent to an API Gateway, it can synchronously invoke a Lambda function to process and return data.

Real-time Applications: Scenarios requiring immediate feedback, such as chatbots or interactive dashboards.

In this mode, error handling is straightforward, as any failure or error in the function is directly returned to the caller.

Lambda Asynchronous Invocation

Asynchronous invocation, on the other hand, decouples the caller from the function execution. The function is invoked and added to a queue for processing. The caller does not wait for a response, making it suitable for applications that don’t require immediate results. AWS automatically retries failed invocations twice, ensuring reliability. Use cases include:

Event-driven Workflows: Triggered by events such as Amazon S3 bucket updates or Amazon SNS notifications.

Batch Processing: Ideal for use cases like sending email notifications or processing logs.

AWS provides mechanisms for monitoring and managing asynchronous invocations, such as Dead Letter Queues (DLQs) and Amazon EventBridge.

Lambda Event Source Mapping with Polling Invocation

Event Source Mapping is a mechanism that enables Lambda to poll event sources like Amazon SQS, DynamoDB Streams, or Kinesis Data Streams. Lambda continuously monitors these sources and invokes the function with a batch of records. Key features include:

Automatic Polling: Lambda handles polling behind the scenes, ensuring seamless integration with event sources.

Batch Processing: Functions can process multiple records in a single invocation, improving efficiency.

Checkpointing: Lambda manages checkpoints to ensure events are processed exactly once in case of failures.

This model is optimal for real-time data ingestion and processing pipelines.

Key Characteristics of AWS Lambda

Containers Under the Hood

Each AWS Lambda function operates within its own isolated environment, effectively running as its own container. This containerisation ensures:

Security: Functions are isolated from one another.

Consistency: Each invocation starts in a fresh container, ensuring no residual data between executions.

When a function is created, AWS packages it into a container, deploys it on a multi-region cloud cluster, and scales it across servers managed by AWS. This ensures high availability and fault tolerance.

Resource Allocation and Cost Efficiency

Lambda functions are allocated CPU and RAM based on user-defined configurations. The pay-as-you-go pricing model charges only for the actual compute time used, measured to the nearest 100 milliseconds. This cost-efficient model eliminates over-provisioning, making it ideal for unpredictable workloads.

Event-Driven Architecture

AWS Lambda is built around an event-driven architecture, where numerous AWS resources can generate events to trigger Lambda functions. Examples include:

  • S3: Trigger Lambda on object creation or deletion.
  • DynamoDB: Invoke Lambda for stream updates.
  • SNS and SQS: Process messages from these messaging services.

AWS Lambda integrations and triggers

This flexibility allows developers to build decoupled, modular applications with ease.

Scalability

Lambda automatically scales with the incoming request volume, scaling up or down to handle thousands of requests per second. This elasticity ensures:

High Availability: Functions scale seamlessly without manual intervention.

Cost Savings: Resources are deallocated immediately upon completion.

Maximum Execution Time

Each Lambda invocation has a maximum execution time of 15 minutes. While sufficient for most tasks, developers must design functions for efficiency. For long-running processes, consider breaking tasks into smaller sub-tasks or using Step Functions for orchestration.

AWS Lambda epitomises the power of serverless computing, offering unparalleled scalability, cost efficiency, and simplicity. With various invocation models and a robust event-driven architecture, Lambda is suitable for a wide range of use cases—from real-time applications to large-scale data processing. By understanding its invocation models and key characteristics, developers can harness its full potential to build resilient and scalable applications.

Sample code on using typescript to build a lambda and deploy with CDK

https://github.com/EmiRoberti77/cdk_ts_apigateway_lambda

Happy Coding - Emi Roberti

Top comments (2)

Collapse
 
emmabase profile image
Emmanuel Eneche

Very insightful, thanks for sharing

Collapse
 
emiroberti profile image
Emi Roberti

Thank you.. I think if some people are used to EC2 or App Runner from AWS .. Lambdas can help with some architecture designs