Skip to main content
Albero Academy

Compute — EC2 & Lambda

The two ways to run code on AWS — virtual servers with EC2 and serverless functions with Lambda — and when to use each.

Chapter 2AWS15 min readBeginner friendly
  • #AWS
  • #EC2
  • #Lambda

Compute is where your code runs. AWS offers a spectrum from "a whole virtual machine you manage" to "just a function, we handle everything". The two anchors are EC2 and Lambda.

EC2 — virtual servers

EC2 (Elastic Compute Cloud) gives you a virtual machine — an instance — that you fully control: OS, software, everything. You pick an instance type (CPU/RAM), an image (AMI), and pay per second it runs. It's the closest thing to a traditional server.

Scaling EC2

  • Auto Scaling groups add/remove instances automatically based on load.
  • Load balancers (ELB) spread traffic across instances and across availability zones.
  • Together they give you a fleet that grows under load and heals when an instance dies.

Lambda — serverless

With Lambda you upload just a function; AWS runs it on demand, scales it automatically, and you pay only for the milliseconds it executes. No servers to patch or keep running. It's triggered by events — an HTTP request, a file upload, a schedule:

a Lambda handler
export const handler = async (event) => {
const name = event.queryStringParameters?.name ?? "world";
return {
statusCode: 200,
body: JSON.stringify({ message: `Hello, ${name}` }),
};
};

Containers

Between the two sit containers: package your app with Docker and run it on ECS or EKS (Kubernetes), or serverless containers on Fargate. You get consistent environments without managing full VMs.

Choosing compute

OptionBest for
LambdaEvent-driven, spiky, low-maintenance functions
EC2Full control, steady/long-running workloads
Containers (ECS/Fargate)Portable apps, microservices, consistent envs

Practice

Launch a free-tier EC2 instance, connect to it, and note how much you control. Then create a simple Lambda function behind an API Gateway URL and call it from your browser. Compare how much setup each required.

Key takeaways

  1. 01Compute is where your code runs; AWS spans full VMs to pure functions.
  2. 02EC2 gives you a virtual server you fully control, billed per second.
  3. 03Auto Scaling + load balancers turn EC2 into a self-healing, elastic fleet.
  4. 04Lambda runs just your function on demand, auto-scales, and costs nothing when idle.
  5. 05Choose Lambda for event-driven/spiky work, EC2/containers for steady, long-running apps.