EC2
Overview
Amazon Elastic Compute Cloud (EC2) provides resizable virtual servers — called instances — in the AWS Cloud. You choose an operating system image (an AMI), an instance type (how much CPU, memory, storage and network), the network placement (VPC, subnet, security group) and storage, and within minutes you have a running server. You pay only for what you use.
EC2 is the foundation of most AWS architectures. You decide how much control you want: full root access to install anything, or combine EC2 with managed services such as load balancers, Auto Scaling and RDS so you do not have to build everything yourself.
For the SAA-C03 exam you need to pick the right instance family, the right purchasing option (On-Demand, Reserved, Savings Plans, Spot, Dedicated), the right storage (EBS vs. instance store), and understand AMIs, user data, IAM roles and the instance lifecycle.
Key Concepts
- An AMI is a template (OS + software + block-device mappings); it is Region-specific but can be copied across Regions and shared across accounts.
- Instance families: General purpose (M, T), Compute optimised (C), Memory optimised (R, X, z1d), Storage optimised (I, D, H), Accelerated computing (P, G, Inf, Trn).
- Burstable T-family instances earn CPU credits at low usage and spend them to burst; unlimited mode can incur extra charges.
- Purchasing options: On-Demand, Reserved Instances / Savings Plans (1 or 3 years), Spot (up to ~90% off, can be interrupted with 2 minutes' notice), Dedicated Hosts/Instances, and Capacity Reservations.
- EBS volumes persist independently of the instance; instance store is physically attached, very fast, but ephemeral (lost on stop, terminate or host failure).
- User data runs once at first boot (by default) with root privileges, limited to 16 KB; instance metadata is at
169.254.169.254(use IMDSv2). - Use an IAM role (instance profile) for AWS API access — never store access keys on an instance.
- Lifecycle: pending -> running -> stopping/stopped -> terminated; hibernate saves RAM to an encrypted EBS root volume; billing for compute stops when stopped.
- An instance lives in one AZ; Elastic IPs are Regional but a public IP from auto-assign changes when the instance is stopped and started.
Detailed Notes
Choosing an instance type
Instance names follow the pattern family + generation + attributes . size, for example m7g.large: m = general purpose, 7 = generation, g = Graviton (ARM), large = size. Suffixes: a AMD, i Intel, g Graviton, d local NVMe storage, n enhanced networking, e extra memory.
| Family | Optimised for | Typical workloads |
|---|---|---|
| T (burstable) | Low baseline CPU with bursts | Dev/test, small web apps |
| M | Balanced CPU/RAM | App servers, small databases |
| C | Compute | Batch, HPC, gaming, media encoding |
| R / X / z1d | Memory | In-memory databases, SAP HANA, caches |
| I / D / H | Storage I/O, high throughput | NoSQL, data warehouses, distributed file systems |
| P / G / Inf / Trn | GPU / ML accelerators | ML training/inference, graphics |
Nitro System instances offload networking, storage and security to dedicated hardware, giving near bare-metal performance, up to 100+ Gbps networking and the ability to run bare-metal (.metal) instances.
Purchasing options
| Option | Commitment | Discount | Best for | Notes |
|---|---|---|---|---|
| On-Demand | None, per-second (Linux) | 0% | Unpredictable, short-term | Highest per-hour price |
| Reserved Instances (Standard) | 1 or 3 years | Up to ~72% | Steady-state, specific instance | Can be sold on the RI Marketplace |
| Convertible RI | 1 or 3 years | Up to ~66% | Steady-state but may change type | Exchangeable |
| Savings Plans | 1 or 3 years, $/hour | Up to ~72% | Flexible usage | Compute SP covers EC2, Fargate, Lambda; EC2 Instance SP is family-specific |
| Spot | None | Up to ~90% | Fault-tolerant, flexible (batch, CI, big data) | 2-minute interruption notice |
| Dedicated Host | On-demand or 1–3 years | Varies | BYOL with per-socket/core licensing, compliance | You see sockets and cores |
| Dedicated Instance | On-demand | n/a | Single-tenant hardware, no host control | Billed per instance + per-Region fee |
| Capacity Reservation | None | None | Guarantee capacity in an AZ | Can be combined with RIs/Savings Plans |
Storage options
| Type | Persistence | Performance | Use |
|---|---|---|---|
| EBS | Survives stop (persistent) | gp3 up to 16,000 IOPS; io2 Block Express up to 256,000 IOPS | Boot volumes, databases |
| Instance store | Lost on stop/terminate | Millions of IOPS NVMe | Cache, buffers, scratch |
| EFS / FSx | Network file systems | Shared across instances | Shared content |
The root volume is deleted on termination by default (DeleteOnTermination = true); additional EBS volumes are kept by default.
Bootstrapping with user data
#!/bin/bash
dnf update -y
dnf install -y nginx
systemctl enable --now nginx
TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
INSTANCE_ID=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/instance-id)
echo "<h1>Hello from $INSTANCE_ID</h1>" > /usr/share/nginx/html/index.html
Launching from the CLI
aws ec2 run-instances --image-id ami-0abcdef1234567890 --instance-type t3.micro \
--subnet-id subnet-0abc123 --security-group-ids sg-0abc123 \
--iam-instance-profile Name=AppInstanceProfile --user-data file://bootstrap.sh \
--metadata-options HttpTokens=required
Networking on an instance
- Each instance has a primary ENI with a private IPv4; add secondary ENIs (limited by instance type) for management/dual-homed designs.
- Enhanced networking (ENA) and Elastic Fabric Adapter (EFA) (HPC, MPI) improve throughput and latency.
- Moving an ENI between instances preserves its private IP and MAC — handy for licence or failover designs.
- Source/destination check must be disabled for NAT or firewall instances.
Instance states and billing
- Stopped instances incur no compute charges but you still pay for EBS and any Elastic IP.
- Hibernate requires an encrypted root volume large enough for RAM and a supported instance type (RAM under 150 GB).
- Termination protection prevents accidental terminate via API/console.
- Linux per-second billing has a 60-second minimum.
Auto Recovery and monitoring
- Status checks: system (AWS hardware) and instance (OS). A CloudWatch alarm on StatusCheckFailed_System with the recover action moves the instance to new hardware, keeping IP, instance ID and EBS volumes.
- Basic monitoring is 5-minute; detailed monitoring is 1-minute. Memory and disk usage need the CloudWatch agent.
Real World Example
Scenario — a game studio optimises its compute bill for a new multiplayer title.
The studio runs three workloads:
- Matchmaking API — steady 24/7 load. They buy a 3-year Compute Savings Plan covering the baseline, saving roughly 60% versus On-Demand, and keep the flexibility to move to Graviton later.
- Game servers — spiky, scale with player counts. These run on On-Demand (c7g) in an Auto Scaling group sized for the daily pattern, with a small pool of Capacity Reservations around launch day to guarantee capacity in the AZs that matter.
- Nightly analytics and asset rendering — fault-tolerant batch jobs that checkpoint to S3. They use Spot Instances across several instance types and AZs, cutting that cost by about 70–80%. If Spot is reclaimed, the two-minute warning triggers a checkpoint and the job restarts elsewhere.
Instances use an IAM role to read from S3 and Secrets Manager, IMDSv2 is enforced, and a hardened golden AMI is rebuilt weekly with EC2 Image Builder. Bootstrapping with user data pulls the latest game build at launch, so scaling out takes minutes without manual steps.
Architecture Diagram
An instance is launched from an AMI into a subnet, with an IAM role, security group and EBS storage.
- 1You choose an AMI, instance type, subnet, security group and IAM role, then launch.
- 2EC2 places the instance on a Nitro host in the chosen AZ and attaches an EBS root volume.
- 3User data bootstraps the software at first boot.
- 4The instance uses its IAM role to call S3 and publishes metrics to CloudWatch; alarms can recover it automatically.
Best Practices
- ✓Right-size instances with Compute Optimizer and CloudWatch metrics; consider Graviton for better price/performance.
- ✓Match the purchasing option to the workload: Savings Plans/RIs for steady state, Spot for fault-tolerant, On-Demand for spiky or unknown.
- ✓Use IAM roles instead of access keys and enforce IMDSv2 (
HttpTokens=required). - ✓Build golden AMIs (EC2 Image Builder) and use launch templates so instances are immutable and reproducible.
- ✓Spread instances across multiple AZs and put them behind a load balancer with Auto Scaling.
- ✓Encrypt EBS volumes (and set encryption by default) and snapshot them with Data Lifecycle Manager or AWS Backup.
- ✓Patch and access via Systems Manager (Patch Manager, Session Manager) instead of open SSH.
- ✓Tag everything and use detailed monitoring plus the CloudWatch agent for memory and disk metrics.
Common Interview Questions
Exam Tips
SAA-C03 focus
- Spot = cheapest but interruptible (2-minute warning) -> batch, stateless, flexible jobs. Never for critical databases.
- Reserved/Savings Plans = steady-state, predictable. Dedicated Host = BYOL / per-socket licensing / compliance.
- Instance store is ephemeral; need persistent -> EBS, shared -> EFS, Windows shared -> FSx for Windows.
- Memory-intensive in-memory database -> R/X family; compute-bound -> C; GPUs -> P/G; high IOPS local storage -> I.
- User data runs once as root at first boot (16 KB limit); it is not re-run on reboot unless configured.
- Use an IAM role, not stored credentials; metadata endpoint is 169.254.169.254; prefer IMDSv2.
- EC2 is AZ-scoped; AMIs are Region-scoped (copy to use elsewhere); EBS snapshots can be copied across Regions.
- Auto recovery via CloudWatch alarm keeps the same instance ID, IPs and EBS data; hibernate preserves RAM state.
Finished this lesson?
0 of 64 lessons done · 0% of the course

