Deciding When Lambda Is the Right Compute Service
This lesson explains how Lambda suits event-driven, short and spiky work with no idle cost, and how its limits show when another compute service is the better fit.
Learning outcomes
After completing this lesson, you will be able to:
- Identify the Lambda limits that shape designs and their workarounds.
- Choose a compute service from the shape of the workload.
Limits that shape the design
Several Lambda limits shape serverless designs, and each has a standard workaround. A synchronous invocation can carry at most 6 MB in its request and its response, and an asynchronous one 1 MB, so large files are never passed directly: the caller uploads the file to S3 and passes the object key, and the function reads the file itself.
The 15-minute timeout means longer work is split into steps orchestrated by Step Functions, or moved to containers or AWS Batch. The 10 GB memory limit means workloads that need more memory, such as large in-memory analytics, run on EC2 or containers. The following table summarises the limits.
| Limit | Value | Design around it by |
|---|---|---|
| Synchronous payload | 6 MB request and response | Passing S3 object keys for large files |
| Asynchronous payload | 1 MB | Passing references to S3 |
| Timeout | 15 minutes | Splitting work into Step Functions steps, or using containers |
| Memory | 10 GB | Using EC2 or containers for larger memory needs |
6MB
Synchronous request and response payload
1MB
Asynchronous payload
Designing around limits
Choosing a compute service
Choosing a compute service starts with the shape of the workload. Lambda suits event-driven work that is short, spiky or idle much of the time, because it scales automatically and costs nothing when idle. Long-running or steady, highly utilised services are usually cheaper on containers, with ECS or EKS on Fargate, or on EC2, where capacity is paid for continuously but used fully.
Workloads that need GPUs, very large memory or long-lived connections run on EC2 or on containers on EC2. Batch jobs longer than 15 minutes run on AWS Batch or as ECS tasks. Multi-step processes with waits, such as an order that waits for payment, are orchestrated by Step Functions, which calls Lambda and other services for each step. The following table summarises the choice.
| Workload | Best fit |
|---|---|
| Event-driven, short, spiky or idle much of the time | Lambda |
| Long-running or steady, highly utilised services | ECS or EKS on Fargate, or EC2 |
| GPUs, very large memory, long-lived connections | EC2 or containers on EC2 |
| Batch jobs longer than 15 minutes | AWS Batch or ECS tasks |
| Multi-step processes with waits | Step Functions orchestrating Lambda and other services |
Choosing compute
Lambda or EC2 for an image service?
A service resizes images uploaded by users. Uploads arrive in bursts, from none at night to 500 a second during events, and each takes about 2 seconds.
Lambda fits. The account's concurrency quota (1,000 by default) must be raised, or reserved concurrency set, to handle the peak without throttling. A job taking 30 minutes per image would instead suit Fargate or AWS Batch.
A video conversion job takes 40 minutes. Is Lambda a good fit?
Show answer
No. Lambda's maximum timeout is 15 minutes, so the job suits ECS or AWS Batch on Fargate or EC2, or must be split into shorter steps.
Exam Tips
Remember for the exam
- For steady, highly utilised or long-running work, use containers or EC2 instead of Lambda.
- To process files larger than the payload limit, pass S3 keys to the function.
- To exceed the 15-minute limit, split the process into Step Functions steps.
- For the lowest cost on spiky, event-driven work with idle periods, use Lambda.