Introduction
AWS Lambda and Amazon Elastic Container Service, known as Amazon ECS, are two ways of running application code on AWS. Lambda runs functions in response to events. ECS runs containers, where a container is a packaged unit that holds an application together with the libraries and settings it needs to run. This guide compares the two and explains when each should be used. A fuller overview of Lambda is provided in the guide on AWS Lambda.
What is being compared?
Lambda is a serverless service in which small functions are run when an event occurs, with no server managed by the developer and with charges only while code runs. ECS is an orchestration service that runs and manages containers, which are usually long-running processes. Containers on ECS can be run on AWS Fargate, which provides capacity without servers being managed, or on server instances that the team operates. The essential difference is the unit of execution and the degree of control.
Why the choice matters
The choice affects cost, the way the workload scales, the amount of control over the runtime, and the length of time a task may run. A short, event-driven task and a continuously running service have very different requirements, and selecting the wrong model can raise cost or introduce limits that do not suit the workload.
How each works
In Lambda, a function remains idle until an event triggers it. Copies of the function are run in parallel as traffic rises, and capacity is released as traffic falls. A single invocation may run for a maximum of fifteen minutes, and a delay known as a cold start may occur when a new environment is initialized.
In ECS, one or more containers run continuously as a service. Scaling is achieved by adding or removing container copies, according to rules that the team configures. Because the containers run continuously, they are paid for whether or not they are handling requests, but there is no fifteen-minute limit and no cold start, and full control of the runtime is retained.
Comparison diagram
Where Lambda is stronger
- No server management. Capacity and scaling are handled by AWS.
- Pay for use. Idle capacity is not paid for.
- Fast to start. Small functions reach production quickly.
- Event integration. Functions are triggered directly by many AWS services.
Where ECS is stronger
- Long-running work. Processes that run continuously or exceed the Lambda limit are supported.
- Predictable latency. A running container avoids the cold start of a function.
- Full control. The runtime, dependencies, and resources can be controlled directly.
- Steady high volume. A running container can be cheaper at constant high load.
When to choose each
- Lambda suits event-driven tasks, variable traffic, and small pieces of work.
- ECS suits long-running services, workloads that require full control, and steady high-volume traffic.
- The two are often combined, with Lambda for bursty tasks and ECS for continuous services.
Best practices
- The traffic pattern, the required duration, and the level of control should be assessed before a service is chosen.
- Lambda should be used for short, event-driven work, and ECS for continuous or long-running services.
- Cost should be estimated under both models for the expected traffic.
- The two should be combined where each is best suited.
Common mistakes
- Lambda is used for a continuously busy service, where a container would be simpler and cheaper.
- ECS is used for rare, bursty work that would cost very little under Lambda.
- The fifteen-minute limit of Lambda is overlooked for long tasks.
- Cold start latency is ignored on a Lambda path that requires fast, predictable responses.
Related AWS services
- AWS Fargate runs ECS containers without servers being managed.
- Amazon API Gateway can place an API in front of either service.
- Amazon EC2 can provide the servers on which ECS containers run.
Frequently Asked Questions
- What is the core difference between Lambda and ECS?
- Lambda runs functions in response to events, with no server managed and charges only while code runs. ECS runs containers, usually long-running processes that the team scales and manages more directly.
- What is a container?
- A container is a packaged unit that holds an application together with the libraries and settings it needs to run. It runs consistently across environments and is the unit that ECS manages.
- What is AWS Fargate?
- AWS Fargate is a way of running containers without managing the underlying servers. With Fargate, ECS runs containers on capacity that AWS provisions.
- Which is cheaper, Lambda or ECS?
- For intermittent or bursty work, Lambda is often cheaper. For continuous, high, steady load, ECS can be cheaper, because a running container is not billed per request.
- Can Lambda and ECS be used together?
- Yes. Many systems use Lambda for event-driven tasks and ECS for long-running services within the same application.
This article is the summary. The book is the full, continuously updated reference: choosing compute, cost control, scaling, and complete architectures on AWS.
View the book