Compute: EC2, Containers and Lambda
Overview
Compute is the processing power that runs your programs. AWS offers several ways to get it, from full virtual servers you control to tiny functions that run only when triggered.
The easiest comparison is transport. Amazon EC2 is like renting a car: you choose the model and drive it yourself. Containers are like a taxi fleet service where vehicles are standardised. AWS Lambda is like a ride-hailing trip: you only pay for the journey and never think about the vehicle.
For the exam you must recognise what each compute service does, which ones are serverless, and how Auto Scaling and load balancing give applications elasticity and high availability.
Key Concepts
- Amazon EC2 provides resizable virtual servers (instances) in the cloud, launched from an AMI (Amazon Machine Image).
- EC2 instance families are optimised for different needs: general purpose, compute optimised, memory optimised, storage optimised and accelerated computing (GPU).
- Auto Scaling automatically adds or removes EC2 instances based on demand; Elastic Load Balancing (ELB) distributes traffic across healthy targets in multiple AZs.
- AWS Lambda runs code in response to events with no servers to manage; you pay per request and execution time (maximum 15 minutes per invocation).
- Amazon ECS and Amazon EKS run containers (EKS uses Kubernetes); AWS Fargate removes the need to manage the underlying servers for either.
- AWS Elastic Beanstalk deploys web applications by handling provisioning, load balancing and scaling for you.
- Amazon Lightsail offers simple virtual servers with predictable pricing for small projects; AWS Batch runs batch computing jobs at scale.
- EC2 purchasing options include On-Demand, Reserved Instances, Savings Plans, Spot Instances and Dedicated Hosts.
Detailed Notes
Amazon EC2 essentials
You pick an AMI (operating system + software), an instance type, networking and storage, then launch. You manage the guest OS and applications. Billing is per second for Linux (one-minute minimum) on most instance types.
Instance type families
| Family | Optimised for | Example use | Example type |
|---|---|---|---|
| General purpose | Balanced | Web servers, small databases | t3, m7i |
| Compute optimised | High CPU | Batch, gaming, media encoding | c7g |
| Memory optimised | Large memory | In-memory databases, analytics | r7g |
| Storage optimised | High disk I/O | NoSQL, data warehousing | i4i |
| Accelerated computing | GPUs / chips | ML training, graphics | p5, g5 |
EC2 purchasing options
| Option | Discount | Commitment | Best for |
|---|---|---|---|
| On-Demand | None | None | Short, unpredictable workloads |
| Savings Plans | Up to ~72% | 1 or 3 years of spend ($/hour) | Steady usage with flexibility |
| Reserved Instances | Up to ~72% | 1 or 3 years for a configuration | Steady, predictable usage |
| Spot Instances | Up to ~90% | None, can be interrupted with 2-minute notice | Fault-tolerant, flexible jobs |
| Dedicated Hosts | Varies | Optional | Bring-your-own licences, compliance |
Scaling and load balancing
Auto Scaling groups keep a desired number of healthy instances and add capacity when metrics (like CPU) rise. Elastic Load Balancing spreads requests; the common types are:
| Load balancer | Layer | Use |
|---|---|---|
| Application Load Balancer (ALB) | 7 (HTTP/HTTPS) | Web apps, path/host routing |
| Network Load Balancer (NLB) | 4 (TCP/UDP) | Extreme performance, static IPs |
| Gateway Load Balancer | 3 | Third-party virtual appliances |
Serverless and containers
| Service | What it is | Servers to manage? |
|---|---|---|
| Lambda | Event-driven functions | None |
| Fargate | Serverless compute engine for containers | None |
| ECS | AWS-native container orchestration | EC2 or Fargate |
| EKS | Managed Kubernetes | EC2 or Fargate |
| Elastic Beanstalk | Application deployment platform | Created for you |
Launching an instance and a function
aws ec2 run-instances --image-id ami-0abcdef1234567890 --instance-type t3.micro \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=web-1}]'
aws lambda invoke --function-name resize-image --payload '{"key":"photo.jpg"}' out.json
Dedicated and capacity-related compute options
| Option | What it is |
|---|---|
| Dedicated Instances | Instances on hardware dedicated to your account (may share host with your other instances) |
| Dedicated Hosts | A whole physical server you control; supports BYOL by socket/core visibility |
| On-Demand Capacity Reservations | Reserve capacity in a specific AZ for any duration, billed whether used or not |
| AWS Auto Scaling | Service that scales EC2, ECS, DynamoDB and Aurora resources via scaling plans |
Choosing the right option
{
"needOSControlOrLegacySoftware": "EC2",
"shortEventDrivenTask": "Lambda",
"packagedMicroservices": "ECS or EKS with Fargate",
"justDeployMyWebApp": "Elastic Beanstalk",
"simpleLowCostWebsite": "Lightsail"
}
Remember the shared responsibility split: on EC2 you patch the OS; with Lambda and Fargate AWS manages the underlying hosts.
Real World Example
A news website runs its web tier on EC2 instances in an Auto Scaling group behind an Application Load Balancer spread across two AZs, so traffic spikes during breaking news are absorbed automatically.
When an editor uploads an image, an S3 event triggers a Lambda function that creates thumbnails, costing fractions of a cent per image. Overnight analytics jobs run on Spot Instances because they can be re-run if interrupted, cutting compute cost by more than half.
Architecture Diagram
- 1Visitors reach the load balancer, which checks target health.
- 2Requests are spread across instances in multiple Availability Zones.
- 3Auto Scaling adds or removes instances as demand changes.
- 4Uploads to S3 trigger Lambda for event-driven processing without servers.
Best Practices
- ✓Run instances across multiple AZs behind a load balancer for availability.
- ✓Right-size instances using monitoring data and pick current-generation types.
- ✓Use Spot for interruptible work and Savings Plans for steady baselines.
- ✓Prefer Lambda or Fargate when you want to avoid server management.
- ✓Use IAM roles for EC2, not embedded credentials, and patch AMIs regularly.
Common Interview Questions
Exam Tips
CLF-C02 focus
- "Run code without managing servers" = Lambda.
- "Containers without managing servers" = Fargate.
- "Cheapest, can be interrupted" = Spot; "steady 1-3 year usage" = Reserved / Savings Plans.
- "Deploy a web app without managing infrastructure details" = Elastic Beanstalk.
Finished this lesson?
0 of 43 lessons done · 0% of the course

