SkillSprout
Chapter 5 · Lesson 1
Beginner
9 min read

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.

FamilyOptimised forTypical workloads
T (burstable)Low baseline CPU with burstsDev/test, small web apps
MBalanced CPU/RAMApp servers, small databases
CComputeBatch, HPC, gaming, media encoding
R / X / z1dMemoryIn-memory databases, SAP HANA, caches
I / D / HStorage I/O, high throughputNoSQL, data warehouses, distributed file systems
P / G / Inf / TrnGPU / ML acceleratorsML 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

OptionCommitmentDiscountBest forNotes
On-DemandNone, per-second (Linux)0%Unpredictable, short-termHighest per-hour price
Reserved Instances (Standard)1 or 3 yearsUp to ~72%Steady-state, specific instanceCan be sold on the RI Marketplace
Convertible RI1 or 3 yearsUp to ~66%Steady-state but may change typeExchangeable
Savings Plans1 or 3 years, $/hourUp to ~72%Flexible usageCompute SP covers EC2, Fargate, Lambda; EC2 Instance SP is family-specific
SpotNoneUp to ~90%Fault-tolerant, flexible (batch, CI, big data)2-minute interruption notice
Dedicated HostOn-demand or 1–3 yearsVariesBYOL with per-socket/core licensing, complianceYou see sockets and cores
Dedicated InstanceOn-demandn/aSingle-tenant hardware, no host controlBilled per instance + per-Region fee
Capacity ReservationNoneNoneGuarantee capacity in an AZCan be combined with RIs/Savings Plans

Storage options

TypePersistencePerformanceUse
EBSSurvives stop (persistent)gp3 up to 16,000 IOPS; io2 Block Express up to 256,000 IOPSBoot volumes, databases
Instance storeLost on stop/terminateMillions of IOPS NVMeCache, buffers, scratch
EFS / FSxNetwork file systemsShared across instancesShared content

The root volume is deleted on termination by default (DeleteOnTermination = true); additional EBS volumes are kept by default.

Bootstrapping with user data

bash
#!/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

bash
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

EC2 instance lifecycle components
Architecture
AWS Cloud
AMIOS + software
IAM roleinstance profile
VPC
Availability Zone
Subnet
Security group
EC2 instancem7g.large
EBS volumegp3 root
S3via IAM role
CloudWatchmetrics and alarms

An instance is launched from an AMI into a subnet, with an IAM role, security group and EBS storage.

  1. 1You choose an AMI, instance type, subnet, security group and IAM role, then launch.
  2. 2EC2 places the instance on a Nitro host in the chosen AZ and attaches an EBS root volume.
  3. 3User data bootstraps the software at first boot.
  4. 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