Lesson 3 of 30Core services
EC2
Launch and secure virtual servers, pick instance types and pricing models, and scale them with Auto Scaling groups.
EC2 (Elastic Compute Cloud) is AWS's virtual server service, and it sits under far more of AWS than you can see: containers on ECS, managed databases and build runners all run on EC2-style machines. Even if you end up on Lambda or Fargate, knowing what an instance, a security group and an Auto Scaling group are makes every other service easier to reason about.
An instance is a virtual machine
An instance is a virtual machine running on AWS hardware. You choose three things when you launch one:
- An AMI (Amazon Machine Image): the disk image to boot from. Amazon Linux 2023, Ubuntu 24.04, or your own image with your app baked in.
- An instance type: how much CPU, memory and network it gets.
- Where it lives: a subnet in one Availability Zone (subnets are lesson 5).
Its disk is usually an EBS volume (Elastic Block Store): a network-attached drive that lives on independently of the instance. Stop the instance and the EBS volume keeps your data. Some types also have instance store disks, which are physically attached and fast but wiped when the instance stops.
The instance boots from an AMI, a template disk image.
Instance types and families
Instance type names are a code: m7g.large means family m (general purpose), generation 7, attribute g (AWS Graviton, an ARM CPU), size large.
| Family | Optimized for | Typical use |
|---|---|---|
t (t3, t4g) | Burstable: cheap baseline CPU with credits to burst | Small web apps, dev boxes |
m (m7i, m7g) | Balanced CPU and memory | Most app servers |
c (c7g, c7i) | Compute | Video encoding, CPU-heavy APIs |
r (r7g) | Memory | Caches, in-memory analytics |
g, p | GPUs | ML training and inference |
Each size step roughly doubles CPU, memory and price: large is 2 vCPUs, xlarge 4, 2xlarge 8. Graviton (g) instances are usually around 20% cheaper for the same work if your code runs on ARM (Python, Node and Java usually do with no changes).
Security groups: a stateful firewall
A security group is a firewall attached to an instance's network interface. It only has allow rules (no deny), and everything not allowed is blocked.
The important word is stateful: if a request is allowed in, the response is automatically allowed back out, and vice versa. You only write rules for who may start a connection.
aws ec2 authorize-security-group-ingress --group-id sg-0web \ --protocol tcp --port 443 --cidr 0.0.0.0/0aws ec2 authorize-security-group-ingress --group-id sg-0db \ --protocol tcp --port 5432 --source-group sg-0webThat second rule is the pattern to remember: a security group can reference another security group as the source. "Allow 5432 from anything in sg-0web" keeps working as instances come and go, with no IP addresses to maintain.
0.0.0.0/0 and has no outbound rules at all. A browser connects on 443. Does the response get back?Getting onto the box: key pairs vs Session Manager
The classic way in is SSH with a key pair: AWS keeps the public key, you keep the .pem private key, and you open port 22 in the security group.
The better way today is SSM Session Manager. The instance runs the SSM agent (preinstalled on Amazon Linux), its role includes the AmazonSSMManagedInstanceCore policy, and you get a shell through AWS's API. No port 22, no keys to lose, and every session is logged.
aws ssm start-session --target i-0abc123def4567890User data: run a script at first boot
User data is a script EC2 runs once when the instance first boots. It is how you install packages and start your app without logging in.
#!/bin/bashdnf install -y dockersystemctl enable --now dockerdocker run -d -p 80:8000 --restart always \ 123456789012.dkr.ecr.us-east-1.amazonaws.com/api:1.4.2For anything bigger, bake an AMI with your app already installed (with a tool like EC2 Image Builder or Packer) so new instances start in seconds instead of installing for minutes.
Pricing models
You pay for an instance per second while it runs (60-second minimum on Linux). How you commit changes the price:
| Model | Discount vs on-demand | Catch |
|---|---|---|
| On-Demand | none | Most expensive, no commitment |
| Savings Plans | up to about 70% | Commit to $X per hour of compute for 1 or 3 years |
| Reserved Instances | up to about 70% | Older model, tied to an instance family |
| Spot | up to about 90% | AWS can reclaim it with a 2-minute warning |
Spot is unused AWS capacity. It is perfect for work that can be interrupted and retried (batch jobs, CI runners, rendering), and wrong for your only database.
Press Run (⌘ Enter) to see what happens.A common real setup is the last line: a baseline of steady instances on a Savings Plan, plus Spot instances for extra capacity.
Match the workload to the pricing model.
Auto Scaling groups
An Auto Scaling group (ASG) keeps a fleet of identical instances healthy and sized right. You give it a launch template (AMI, type, security groups, user data), a min, max and desired count, and the subnets (spread across AZs) to use.
It does two jobs:
- Self-healing. If an instance fails a health check, the ASG terminates it and launches a replacement.
- Scaling. A target tracking policy keeps a metric near a target, like "average CPU at 60%". More load, more instances; less load, fewer.
Press Run (⌘ Enter) to see what happens.Two instances in two AZs share the traffic.
Target tracking capacity
An Auto Scaling group with a target tracking policy tries to keep average CPU near a target. A simple model of what it does:
desired = ceil(current_instances * current_cpu / target_cpu)then clamp desired between the group's min_size and max_size. Write desired_capacity(current, cpu, target, min_size, max_size).
desired_capacity(4, 90, 60, 2, 10) → 6 (4 * 90 / 60 = 6)
desired_capacity(4, 20, 60, 2, 10) → 2 (ceil(1.33) = 2)
desired_capacity(8, 95, 50, 1, 12) → 12 (16, clamped to max)All inputs are positive numbers, and min_size <= max_size.
- AMI
- Amazon Machine Image: the disk image an instance boots from.
- EBS volume
- A network-attached block disk for an instance. Survives stops; back it up with snapshots.
- security group
- A stateful, allow-only firewall on an instance's network interface.
- Spot instance
- Spare capacity at up to about 90% off that AWS can reclaim with a 2-minute warning.
- Auto Scaling group
- Keeps a fleet of identical instances at the desired count, replacing unhealthy ones and scaling on metrics.
- user data
- A script that runs on an instance's first boot, used to install and start software.