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

AWS Lambda Amazon ECS Unit of run a function a container Trigger an event a request to a running service Duration up to fifteen minutes no fixed limit Scaling automatic configured by the team Cost model per request and time for running containers Best for short, event-driven continuous or long-running work services

Where Lambda is stronger

Where ECS is stronger

When to choose each

Best practices

Common mistakes

Further reading in this library

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.
AWS Serverless Architecture Handbook cover
Go deeper ยท Book as a Service™
AWS Serverless Architecture Handbook

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