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.
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
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.
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.
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).
Mental model
Renting an apartment, a fully-fitted unit, or ordering room service
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.
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.
Examples
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 servicesaws ecs list-services --cluster my-cluster --output tableThese read-only describe/list calls cost nothing and are the polite way to explore someone else's fleet.
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.
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.
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.
Common issues
EC2 shows 2/2 status checks but can't be reachedSecurity group or NACL, or the app isn't listening on the expected port
Check inbound SG rules for port/protocol/source, then verify the listener on the box (ss -tlnp).
Fargate service tasks restart in a loopHealth check failing (wrong path/port) or image pulls timing out
Confirm the container listens on the health-check path and give ECR pulls enough start timeout.
Lambda throttling at scaleConcurrency exceeded (region/account limits, lower in VPC)
Raise the reservation, retry with backoff, or fan out via SQS instead of synchronous calls.
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.
Interview checkpoint
Hands-on lab
- 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
Quick quiz
Which compute tier bills per invocation rather than per provisioned hour?
Fargate lets you run containers without provisioning the underlying servers.