EC2 vs Lambda
Learn when to choose a server you manage (EC2) versus serverless code execution (Lambda), with cost, scaling, and simplicity tradeoffs.
This lesson is purely conceptual — no AWS usage required.
EC2 vs Lambda at a glance
What EC2 is really like
An EC2 instance is a virtual server in AWS. You choose the operating system, connect to the instance, install software, manage updates, and decide when to start, stop, reboot, or terminate it. With On-Demand Instances, you pay by the second with a 60-second minimum.
EC2 is a good fit when
You need OS-level control
- Pick the operating system
- Install custom software and packages
- Configure server-level settings
You need always-running work
- Traditional web server or API
- Long-running background processes
- Work that runs continuously, not in short bursts
EC2 can scale, but you usually configure that using EC2 Auto Scaling, which adds or removes instances based on policies and health checks.
What Lambda is really like
Lambda runs your code without the need to manage servers. It scales up and down automatically and uses pay-per-use pricing. Lambda is commonly used for event-driven tasks and can be invoked by other AWS services.
Note
Standard Lambda functions have a maximum timeout of 15 minutes (900 seconds). This makes Lambda a strong fit for short tasks, request handling, automation, and event processing — but not for traditional long-lived servers.
Lambda sends invocation metrics to CloudWatch automatically, and logs go to CloudWatch Logs when the execution role has the needed permissions.
The key tradeoff: control vs simplicity
With EC2, you get deep control. You pick the instance type, operating system, installed software, and server configuration. Instance types differ in compute, memory, and storage capabilities.
With Lambda, you give up server control in exchange for less setup and automatic scaling. You focus on function code, permissions, triggers, timeouts, and memory. CPU is allocated in proportion to memory.
The real question is not "Which one is better?" It is:
Do I need server control, or do I want to just run code?
Cost shape
Monitoring and troubleshooting
Decision rules
Choose EC2 if
- You need a traditional web server
- You need to install custom software or OS packages
- You need a long-running background process
- You want full control over the machine
Choose Lambda if
- Your code runs because of events
- Your workload is short-lived
- You want less infrastructure to manage
- Traffic may be uneven or bursty
Quick comparison
| Topic | EC2 | Lambda |
|---|---|---|
| What it is | Virtual server you manage | Code execution without managing servers |
| Server management | You manage OS and software | AWS manages servers |
| Scaling | Usually set up with Auto Scaling | Scales automatically |
| Pricing shape | Pay while instance runs | Pay per request and execution time |
| Best for | Traditional apps, custom servers, long-running processes | Event-driven apps, short tasks, automation |
| Timeout model | No Lambda-style execution cap | Standard functions up to 15 minutes |
| Monitoring | CloudWatch metrics and alarms | CloudWatch metrics and Logs built in |
Micro-activity 1: Choose the better service
Micro-activity 2: Pick your compute
Your answers are saved automatically.
Summary
EC2 is a virtual server you manage, while Lambda runs code without you managing servers. EC2 gives you full control over the instance lifecycle, Lambda automatically scales with pay-per-use pricing.
EC2 is stronger when you need full machine control, custom environments, or long-running services. Lambda is stronger when work is event-driven, short-lived, and you want less operational overhead. Standard Lambda functions have a 15-minute maximum timeout, which is one of the clearest practical boundaries.
For observability, both integrate with CloudWatch, but Lambda has very built-in metrics and logging out of the box, while EC2 monitoring often involves more deliberate setup.