Skip to main content
Skip to main content
Still in beta — questions, comments or suggestions? aramb@aramb.dev

EC2 vs Lambda

Learn when to choose a server you manage (EC2) versus serverless code execution (Lambda), with cost, scaling, and simplicity tradeoffs.

15 min
Introductory
No AWS Account NeededFREE

This lesson is purely conceptual — no AWS usage required.

EC2 vs Lambda at a glance

What it is
EC2: Virtual server you manage
Lambda: Code execution without managing servers
Control
EC2: More control, more responsibility
Lambda: Less control, less setup
Best for
EC2: Always-running apps, OS-level control
Lambda: Event-driven, short-lived tasks

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

How you pay
EC2 On-Demand: Per second while the instance runs
Lambda: Per request and execution time
Idle cost
EC2 On-Demand: Pays even when idle
Lambda: No charge when not invoked
Best fit
EC2 On-Demand: Always-on servers
Lambda: Spiky or burst workloads

Monitoring and troubleshooting

Metrics
EC2: CloudWatch monitors CPU, network, disk — you configure what to watch
Lambda: AWS publishes invocation metrics to CloudWatch automatically
Logs
EC2: Requires CloudWatch agent or explicit log streaming setup
Lambda: Logs go to CloudWatch Logs by default when the execution role has permissions
Feel
EC2: More deliberate setup needed
Lambda: Observability feels more built-in

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

TopicEC2Lambda
What it isVirtual server you manageCode execution without managing servers
Server managementYou manage OS and softwareAWS manages servers
ScalingUsually set up with Auto ScalingScales automatically
Pricing shapePay while instance runsPay per request and execution time
Best forTraditional apps, custom servers, long-running processesEvent-driven apps, short tasks, automation
Timeout modelNo Lambda-style execution capStandard functions up to 15 minutes
MonitoringCloudWatch metrics and alarmsCloudWatch metrics and Logs built in

Micro-activity 1: Choose the better service

Practice
1 / 6

A basic Linux web server that must stay running all day. Which service is the better fit?


Micro-activity 2: Pick your compute

Micro-Activity · Pick your compute
0 / 4 answered
1
2
3
4

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.


Quiz

Knowledge Check
1 / 10

Which service is a virtual server that you manage?