Lesson 6 of 6 AWS Lambda

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.

LimitValueDesign around it by
Synchronous payload6 MB request and responsePassing S3 object keys for large files
Asynchronous payload1 MBPassing references to S3
Timeout15 minutesSplitting work into Step Functions steps, or using containers
Memory10 GBUsing EC2 or containers for larger memory needs

6MB

Synchronous request and response payload

1MB

Asynchronous payload

Scenario questions

Designing around limits

Exam skills 2.1.5

Design around Lambda's limits.

Log in to try this activity Interactive activities are free with an account.

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.

WorkloadBest fit
Event-driven, short, spiky or idle much of the timeLambda
Long-running or steady, highly utilised servicesECS or EKS on Fargate, or EC2
GPUs, very large memory, long-lived connectionsEC2 or containers on EC2
Batch jobs longer than 15 minutesAWS Batch or ECS tasks
Multi-step processes with waitsStep Functions orchestrating Lambda and other services
Match the pairs

Choosing compute

Exam skills 2.1.5 · 4.2.3

Match each workload with the compute service that suits it best.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

EC2 fleet
Idle at nightPays for running instances
Burst to 500 per secondAuto Scaling takes minutes
OperationsPatch and manage instances
Fits limits?—
Lambda
Idle at nightPays nothing
Burst to 500 per secondAbout 1,000 concurrent executions within seconds (500 x 2 s)
OperationsNone
Fits limits?Yes: 2 seconds is well under 15 minutes

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.

Check yourself

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.