AWS · explainer · checked November 4, 2026
Lambda vs Fargate vs EC2: where to run your code on AWS
The three differ in what you hand to AWS. With AWS Lambda you hand over only a function, and it runs when an event arrives, for up to 15 minutes by default. With AWS Fargate you hand over a container and its CPU and memory. With Amazon EC2 you get a virtual server and manage it yourself. Amazon ECS is not a fourth option; it runs containers on either.
Side by side
| AWS Lambda | AWS Fargate | Amazon EC2 | |
|---|---|---|---|
| What you deploy | A function (a .zip package or a container image) | A container, described in an Amazon ECS task definition | A virtual server built from an Amazon Machine Image |
| Who manages servers | AWS: maintenance, capacity, scaling and patching | AWS; you do not provision or scale clusters of virtual machines | You: choose instance type and count, configure security and networking, manage storage |
| How it runs | Starts in response to an event or request; each invocation limited to 15 minutes by default | You start tasks; an Amazon ECS service keeps a set number of tasks running and replaces failed ones | Runs until you stop or terminate the instance |
| How you size it | Memory from 128 MB to 10,240 MB; CPU scales with memory | vCPU and memory you request for the task | An instance type: a fixed balance of compute, memory, network and storage |
| How you pay | Requests plus GB-seconds of run time | vCPU, memory and storage, rounded up to the second | Instance time, with On-Demand, Savings Plans, Reserved and Spot options |
| Best fit | Event-driven or bursty work with short runs | Containerized services and jobs with no server upkeep | Full control: special hardware, long-running or stateful servers |
Three answers to "who runs the server?"
These searches compare compute services at different distances from the hardware. The useful question is how much of the work you want to keep.
Amazon Elastic Compute Cloud (Amazon EC2) provides virtual servers. You pick an instance type, which sets the CPU, memory, network and storage, then you launch instances from an Amazon Machine Image, configure security groups and networking, and manage storage. Everything on the server above the hardware is your job.
AWS Fargate is a technology for running containers with Amazon Elastic Container Service (Amazon ECS) without managing servers or clusters of EC2 instances. You package the application in a container, set its CPU and memory, define networking and IAM policies, and launch it.
AWS Lambda is a serverless compute service: you run code without provisioning or managing servers. Lambda handles server maintenance, capacity, scaling and patching, so you write the handler and connect it to a trigger.
Lambda now has three kinds of compute. Lambda Event Functions is the name for the original function type, which runs a handler in response to events and API calls. Lambda Web Functions run a web application as an HTTP server that Lambda scales for you. Lambda MicroVMs are isolated environments, built for a dedicated environment per user or job, that can live for up to 8 hours. This page is about Event Functions.
When Lambda fits
Lambda is designed for short-lived compute tasks that do not keep state between invocations. You connect a function to a trigger, such as Amazon API Gateway, Amazon S3, Amazon SQS or Amazon EventBridge, and Lambda runs it for each event. Scaling happens by creating more execution environments as traffic arrives, and it handles each invocation independently.
The limits shape what belongs there. These are the Event Function defaults from the Lambda quotas page: - A single invocation can run for up to 15 minutes. Functions that run on Lambda Managed Instances can run up to 90 minutes when invoked asynchronously or through an event source mapping (except Amazon MQ and Amazon DocumentDB).
- Memory is 128 MB to 10,240 MB, and CPU power scales with it. At 1,769 MB a function has the equivalent of one vCPU.
- A .zip deployment package may be 250 MB unzipped, or you can deploy a container image of up to 10 GB.
- The default account limit is 1,000 concurrent executions per Region, and it can be raised.
- State might not persist between invocations. Keep durable data in a database or object store.
The first request to a new execution environment includes initialization, called a cold start. The documentation says cold starts typically affect under 1% of invocations and range from under 100 ms to over 1 second, and that provisioned concurrency pre-initializes environments when you need predictable start times.
Billing is requests plus duration, measured in GB-seconds, with a monthly free allowance. When nothing is running, nothing accrues, apart from any provisioned concurrency you pay to keep ready.
When Fargate fits
Choose Fargate when the workload is already a container, or needs to run longer than a Lambda function allows, and you still do not want to patch or scale servers. Typical examples are a web service that runs all day, a background worker that polls a queue, or a job that takes hours.
Each Fargate task has its own isolation boundary and does not share the underlying kernel, CPU resources, memory resources or elastic network interface with another task. You pay for the vCPU, memory and storage the task uses, rounded up to the nearest second, with a one-minute minimum (Windows containers have a five-minute minimum). Fargate Spot runs interruption-tolerant tasks at a discount on spare capacity, and AWS gives a two-minute warning before it takes the capacity back.
An Amazon ECS service keeps a specified number of tasks running and starts a replacement if one fails. That fits steady traffic, while Lambda fits traffic that comes in bursts or arrives as events. A service on Fargate can sit behind an Application Load Balancer or Network Load Balancer.
When EC2 fits
Choose EC2 when you need control that the managed options do not give. That includes a specific instance type, an operating system you configure yourself, software that installs on a server, or storage you manage directly. EC2 supports several ways to pay: On-Demand billing per second with a 60-second minimum, Savings Plans and Reserved Instances for a one- or three-year commitment, and Spot Instances for unused capacity. Whether a busy, steady workload is cheaper on committed EC2 capacity than on per-request pricing depends on the numbers, so compare the pricing pages for your own case.
The price of control is work: capacity, patching and scaling are yours. Amazon EC2 Auto Scaling can keep the right number of instances running.
Where ECS fits
“ECS vs Lambda” compares a container orchestrator with a function service. Amazon ECS decides where containers run, and it runs them on EC2 instances or on Fargate. So the choice is first Lambda or containers, and then, for containers, Fargate or EC2 (Amazon ECS can also run containers on ECS Managed Instances). The page on ECS vs EKS vs Fargate covers the container side in detail.
A quick way to choose
- Short, event-triggered work that can run in 15 minutes or less: Lambda.
- A container, or work that runs longer or all day, with no servers to manage: Fargate.
- Control of the server, special hardware, or steady load where committed pricing pays off: EC2.
- Parts of one system can use different options. An API can call Lambda while a long-running worker runs on Fargate.
On the exams
How certification exams use this
The exam guides ask candidates to select compute for a workload. The Solutions Architect Associate guide lists AWS compute services with appropriate use cases, including Fargate (knowledge item 3.2.k1), serverless technologies such as Lambda and Fargate (3.2.k5), choosing EC2 instance types and Lambda memory (3.2.s3, 3.2.s4), and cost-effective choice among Lambda, EC2 and Fargate (4.2.s3). The Solutions Architect Professional guide covers cost-optimized architectures that use Lambda functions (3.1.2). The Developer Associate guide covers Lambda configuration such as memory, concurrency and timeout (1.2.2) and deploying containers on Fargate (3.3.2), and the Cloud Practitioner guide covers serverless compute options (3.3.s3). Watch the wording: a run time over 15 minutes points away from a standard Lambda function, “no servers to manage” for a container points at Fargate, and control of the operating system or hardware points at EC2. The usual wrong answer picks Lambda for work that runs too long.
Test yourself
See how well you know it, free.
Each exam’s diagnostic is free, with no sign-in: questions across the whole exam, and a map of what you know and what you don’t.
AWS Certified Solutions Architect – Associate
20 free questions, no sign-in.
Take the free diagnosticAbout the examAWS Certified Solutions Architect – Professional
20 free questions, no sign-in.
Take the free diagnosticAbout the examAWS Certified Developer – Associate
20 free questions, no sign-in.
Take the free diagnosticAbout the examAWS Certified Cloud Practitioner
20 free questions, no sign-in.
Take the free diagnosticAbout the examAWS Certified DevOps Engineer – Professional
20 free questions, no sign-in.
Take the free diagnosticAbout the examAWS Certified CloudOps Engineer – Associate
20 free questions, no sign-in.
Take the free diagnosticAbout the exam
Sources
Checked against the vendors’ own documentation.
- What is AWS Lambda?
- Lambda quotas
- Understanding the Lambda execution environment lifecycle
- AWS Lambda pricing
- Architect for AWS Fargate for Amazon ECS
- Amazon ECS services
- AWS Fargate pricing
- What is Amazon EC2?
Last checked November 4, 2026. Spotted something wrong or out of date? Tell us.