AWS
Intermediate 15 min read AWS

AWS Compute Services

Servers, containers and functions — pick your abstraction

EC2, containers (ECS/EKS/Fargate) and Lambda are the compute tiers of AWS. Choosing between them is choosing between control, portability and ops burden.

EC2ECSEKSFargateLambdaAuto Scaling
Prerequisites: AWS Service Map
Before we start

What you will learn

  • Compare EC2, ECS/EKS/Fargate and Lambda for a workload
  • Describe how Auto Scaling keeps the right instance count
  • Choose compute by control, duration, state and cost profile
In 60 seconds

EC2 is raw virtual servers, ECS/EKS run containers on either EC2 or serverless Fargate, and Lambda runs functions. The ladder moves you from manage servers to manage containers to just run code.

Introduction

What is this?

EC2 is the IaaS compute service: virtual machines with AMIs, instance types, elastic IPs and Auto Scaling. ECS and EKS are container platforms: give them container images, they schedule them onto a fleet you manage (EC2) or Fargate's serverless runtime. Lambda is FaaS: upload code, specify memory/timeout, and AWS provisions and scales per invocation. Auto Scaling Groups maintain instance counts from rules or metrics.

Motivation

Why do you need it?

  • EC2 gives control for legacy/bespoke needs; containers give portability; Lambda gives zero ops.
  • Costs concentrate differently: EC2 bills per second (idle pays), Fargate per vCPU/GB-seconds, Lambda per invocation + memory-seconds.
  • The serverless family trades cold starts and platform limits for no server management.
  • Mixing tiers is normal: API (Lambda) → batch (ECS/Fargate) → heavy compute (EC2).
Think about it

Mental model

Imagine

Renting an apartment, a fully-fitted unit, or ordering room service

Think of

EC2, containers/Fargate, Lambda

EC2: you tidy and fix things (patch, maintain). Containers: furnished unit, you choose and run the furniture. Lambda: call room service — order (invoke) and it arrives (runs) without you owning the kitchen.

Mechanics

How it works

  • EC2 boots an AMI into a VPC subnet; instance type picks vCPU/memory; storage attaches as EBS volumes.
  • Auto Scaling Groups replace unhealthy instances and scale on metrics (CPU, memory, custom) or schedules.
  • ECS manages task definitions into containers on EC2 or Fargate; EKS uses the standard Kubernetes API instead.
  • Lambda runs your code in a sandbox, scales by event concurrency, bills per request and GB-seconds.
  • Tiers interoperate: an ALB can target EC2, IPs, Lambda functions or Fargate services transparently.

The AWS compute ladder.

EC2
ECS / EKS
Fargate
Lambda
Hands-on

Examples

Inspect a fleet with read-only AWS commands

Inspect a fleet with read-only AWS commands
# List instances and their AZ, state and type (read-only)
aws ec2 describe-instances --query \
'Reservations[].Instances[].{id:InstanceId,az:Placement.AvailabilityZone,state:State.Name,type:InstanceType}' \
--output table
# List ECS clusters and their services
aws ecs list-services --cluster my-cluster --output table

These read-only describe/list calls cost nothing and are the polite way to explore someone else's fleet.

Real world

In production

  • A data platform runs Spark on EMR (EC2-backed) for daily jobs, an ECS Fargate service for API workers, and Lambda for event processing.
  • A monitoring company keeps its agent fleet on EC2 spot with Auto Scaling for cost and runs dashboards on Fargate.
  • A mobile backend uses API Gateway → Lambda → DynamoDB and pays only for actual traffic.
Guidelines

Best practices

  • Prefer containers/Fargate over hand-rolled EC2 when the app runs in standard images — fewer OS-patch incidents.
  • Use Auto Scaling with a mix of purchase options (on-demand + spot) to balance cost and reliability.
  • Keep Lambda functions short, stateless and side-effect-free; push long work to ECS/Fargate.
  • Right-size before scaling — more scale on an oversized instance is a design smell.
Watch out

Common mistakes

  • Running a small bursty service 24/7 on a big EC2 box when Fargate/Lambda would idle cost down.
  • Forgetting maximums on Auto Scaling groups, then waking up to a huge fleet bill.
  • Attaching Lambda to a VPC without a NAT path and wondering why it can't reach the internet.
  • Treating instance types as equally priced across regions — prices and availability vary.
Root cause analysis

Common issues

Error
EC2 shows 2/2 status checks but can't be reached
Root cause

Security group or NACL, or the app isn't listening on the expected port

Fix

Check inbound SG rules for port/protocol/source, then verify the listener on the box (ss -tlnp).

Error
Fargate service tasks restart in a loop
Root cause

Health check failing (wrong path/port) or image pulls timing out

Fix

Confirm the container listens on the health-check path and give ECR pulls enough start timeout.

Error
Lambda throttling at scale
Root cause

Concurrency exceeded (region/account limits, lower in VPC)

Fix

Raise the reservation, retry with backoff, or fan out via SQS instead of synchronous calls.

When things break

Debugging tips

  • Instance unreachable? Check state, status checks, security group, route table and key pair, in that order.
  • Autoscaling not shrinking? Check min/max/desired and the scale-in policy's cooldown before poking the ASG.
  • Container task not starting? Read the task definition for image pull, port/CPU limits, and secrets.
  • Lambda slow? Look at Init vs runtime time in logs — cold starts and slow code need different fixes.
Interviews

Interview checkpoint

Practice

Hands-on lab

Inspect an Instance Fleet Read-Only
30 min · beginner
In progress
Objectives
  • List EC2 instances with placement, state and type
  • Group instances by AZ and type
  • Identify clearly-idle resources for cleanup

Inspect an Instance Fleet Read-Only

30 min · beginner

0/4 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

Which compute tier bills per invocation rather than per provisioned hour?

2

Fargate lets you run containers without provisioning the underlying servers.

Go deeper

Official documentation