EC2: Virtual Machines in the Cloud

AWS Fundamentals

Chapter 3 · EC2: Virtual Machines in the Cloud

Chapter 1 named EC2 as AWS's own real, original compute service — public beta in August 2006, full production by October 2008. Chapter 2 covered who's allowed to do what. This chapter covers the thing itself: launching, configuring, and securing a real virtual machine on AWS.

Instances & AMIs

An instance is a running virtual machine. Every instance boots from an AMI (Amazon Machine Image) — a real template bundling an operating system and, optionally, pre-installed software. AWS provides official AMIs (Amazon Linux, Ubuntu, Windows Server), the AWS Marketplace offers third-party ones, and you can create your own custom AMI from a configured instance to launch identical copies of it later.

Instance Types & Families

Real AWS instance types are named by family and generation (e.g. t3.micro, m5.large), each family tuned for a different real workload shape:

FamilyReal ExamplesTuned For
General PurposeT3, T2, M5, M4Balanced CPU/memory — web servers, small databases, dev environments
Compute OptimizedC5, C4CPU-heavy workloads — batch processing, media transcoding, high-traffic web servers
Memory OptimizedR5, R4, X1Large in-memory datasets — big relational databases, in-memory caches
Storage OptimizedI3, D2, H1High-throughput local storage — data warehousing, distributed file systems
Accelerated ComputingP3, G3, F1GPU/FPGA workloads — machine learning training, rendering

Real Pricing Models

  • On-Demand — pay by the hour (or second, for many instance types), with no commitment; the most expensive per-hour rate, the most flexible.
  • Reserved — commit to a specific instance type for 1 or 3 years, in exchange for a real, documented discount of roughly 35-75% off the On-Demand rate.
  • Spot — bid on AWS's own real spare compute capacity, at discounts up to roughly 90% — but AWS can reclaim a Spot instance with only a short warning if it needs that capacity back, so there's no uptime guarantee.
  • Savings Plans (introduced November 2019) — commit to a real dollar-per-hour spend rather than a specific instance type, trading some of Reserved pricing's own rigidity for flexibility across instance families.
Matching Pricing Model to Workload
Spot is a genuinely good real fit for interruptible, fault-tolerant work (batch jobs, CI pipelines, stateless workers behind a load balancer) — never for a database's own primary instance or anything that can't tolerate an abrupt shutdown. Reserved/Savings Plans fit steady, predictable baseline load; On-Demand fits genuinely unpredictable or short-lived workloads.

Security Groups

A security group is a real virtual firewall attached to an instance's own network interface, controlling inbound and outbound traffic by port, protocol, and source/destination. Security groups are stateful — if inbound traffic on a given connection is allowed in, the matching outbound reply is automatically allowed back out, without needing a separate matching rule. (Chapter 5 covers the network-level, stateless alternative — Network ACLs — and exactly how the two differ.)

A Real, Common Mistake
Opening SSH (port 22) or a database port to 0.0.0.0/0 — every IP address on the internet — "just to make it work" is a genuinely common, real misconfiguration, and exactly the kind of overly permissive setup Chapter 1's own shared-responsibility warning and Chapter 2's own least-privilege principle both point directly at. Scope security group rules to specific, known IP ranges wherever possible.

SSH Key Pairs

Rather than a password, Linux instances are normally accessed via an SSH key pair: AWS holds the public key, you keep the private key, and only someone holding the matching private key can authenticate. The private key is generated (or provided) once, at instance launch — AWS never stores a copy of it, so losing it means losing that specific access method entirely, with no real recovery path other than provisioning a new key.

A Real Security Deep-Dive: The Instance Metadata Service

Every running EC2 instance can query a real, special local address — 169.254.169.254 — for its own metadata, including, when an IAM role is attached (Chapter 2), that role's own temporary credentials. This service is called IMDS, and its own real history is a genuine security case study in its own right.

IMDSv1's Real Weakness
The original version, IMDSv1, answered a simple, unauthenticated request with no session or token involved. AWS's own real, documented security guidance identifies several real attack vectors this made possible — including exactly the same category of vulnerability behind Chapter 2's own Capital One case study: a misconfigured web application firewall or reverse proxy, exploited via server-side request forgery (SSRF), can be tricked into making a request to that local metadata address on the application's own behalf — handing an attacker the instance's own IAM role credentials without ever needing to compromise the instance directly.

AWS's real fix, IMDSv2, requires a session to be explicitly established first via a PUT request, returning a secret token (valid up to 6 hours) that every subsequent metadata request must include as a header. Because a typical SSRF exploit can only manipulate an existing request's headers or URL — not initiate this specific, separate PUT request from the application itself — this two-step design is real, structurally more effective than requiring only a static header, and is now AWS's own recommended default.

Hands-On Exercises

Exercise 1

A team runs a short-lived, fault-tolerant batch-processing job that can be safely restarted from where it left off if interrupted. Recommend a real pricing model for this workload, and explain why it fits better than the alternatives.

📄 View solution
Exercise 2

Explain, in your own words, why security groups being stateful means you don't need a separate outbound rule to allow a reply to traffic your own inbound rule already permitted in.

📄 View solution
Exercise 3

Explain, in your own words, why IMDSv2's requirement of a separate PUT request before any metadata can be read makes it genuinely harder for a typical SSRF vulnerability to steal an instance's own IAM role credentials, compared to IMDSv1.

📄 View solution

Chapter 3 Quick Reference

  • Instance — a running VM, booted from an AMI (its own OS + software template)
  • Instance families — General Purpose (T/M), Compute Optimized (C), Memory Optimized (R/X), Storage Optimized (I/D), Accelerated Computing (P/G)
  • Pricing — On-Demand (flexible), Reserved (35-75% off, committed), Spot (up to 90% off, interruptible), Savings Plans (flexible commitment)
  • Security groups — stateful virtual firewalls; never open a port to 0.0.0.0/0 without a real reason
  • SSH key pairs — public key held by AWS, private key held by you; no real recovery if lost
  • IMDSv2 — a real, token-based fix for IMDSv1's own SSRF-exploitable weakness, now AWS's recommended default