Volume 4 · Chapter 1 — Compute and Containers

EC2 and Instance Fundamentals

Goutham Mayaluri 9 min read AWSCloudEC2Containers

1.1 Introduction

Compute is where your code actually runs, and EC2 - Elastic Compute Cloud - is the foundational compute service, providing the virtual machines that host applications when you need full control of the operating system. Even in a world of containers and serverless, understanding EC2 matters because it underpins many higher-level services and because choosing and sizing instances correctly is a real cost and performance decision. This chapter covers what an instance is, how you configure it, and the choices - instance type, image, storage - that determine its behaviour.

1.2 What an EC2 Instance Is

An EC2 instance is a virtual server you launch on demand, defined by a few choices. The Amazon Machine Image (AMI) is the template - the operating system and any pre-installed software the instance boots from. The instance type determines the hardware profile - how much CPU, memory, and network capability - and comes in families optimized for different workloads (general purpose, compute-optimized, memory-optimized, storage-optimized). You place the instance in a subnet within your VPC, attach security groups, and give it an IAM role so it can call AWS services without stored keys. Because the instance is software-defined, you launch, stop, resize, and terminate it in seconds - the elasticity that lets compute follow demand.

1.3 Instance Types and Right-Sizing

Instance types are grouped into families for different needs: general-purpose for balanced workloads, compute-optimized for CPU-heavy work, memory-optimized for large in-memory datasets and databases, storage-optimized for high-I/O storage workloads. Within a family, sizes scale the resources up. The skill is right-sizing - choosing the family that matches the workload’s bottleneck and a size that fits real measured usage plus headroom, rather than over-provisioning “to be safe,” which wastes money, or under-provisioning, which throttles performance. Right-sizing is an ongoing discipline informed by utilization metrics, and it’s one of the highest-impact cost levers because an oversized instance running continuously is pure waste.


# Right-size from real usage, not guesswork - check utilization before choosing a type.
aws cloudwatch get-metric-statistics --namespace AWS/EC2 \
  --metric-name CPUUtilization --dimensions Name=InstanceId,Value=i-0abc123 \
  --start-time 2026-07-01T00:00:00Z --end-time 2026-07-24T00:00:00Z \
  --period 3600 --statistics Average Maximum

1.4 Storage for Instances

Instances use storage in two main forms. EBS (Elastic Block Store) provides durable network-attached block volumes that persist independently of the instance - your data survives an instance stop or termination if the volume is retained, and volumes can be snapshotted for backup and re-attached. Instance store is temporary storage physically attached to the host that is fast but ephemeral - it’s lost when the instance stops - so it suits caches and scratch data, never data you must keep. The important distinction is persistence: EBS for anything durable (the default for most needs), instance store only for temporary high-speed scratch. EBS volumes are also billed for provisioned capacity whether or not they’re full, so oversized volumes waste money.

1.5 Instances in a Resilient Architecture

A single instance is never the resilient unit - it can fail at any time. Instances become resilient when placed behind a load balancer and managed by an Auto Scaling group across multiple Availability Zones, so failures are replaced and load is distributed (the next chapter). Instances should be treated as disposable and interchangeable - “cattle, not pets” - configured automatically from an image or bootstrapping script rather than hand-tuned, so any instance can be replaced by an identical one at any time. This mindset, combining EC2 with Auto Scaling, load balancing, and automation, is what turns raw virtual machines into a reliable, scalable compute tier.

1.6 Banking Production Scenario

A bank runs a legacy application that needs OS-level control on EC2. It chooses a memory-optimized instance family sized to the application’s real memory footprint plus headroom, placed in private subnets across two Availability Zones. Each instance boots from a hardened AMI and configures itself automatically, holds an IAM role for AWS access (no stored keys), and stores durable data on encrypted EBS volumes that are snapshotted for backup. The instances sit behind a load balancer in an Auto Scaling group, so a failed instance is automatically replaced and payday load is met by adding instances. The bank right-sizes regularly from utilization metrics and commits its steady baseline to a Savings Plan for the discount.

1.7 Troubleshooting: Instance Problems

Common EC2 issues have clear causes. An instance that’s overloaded (high CPU or memory) is under-sized or needs more instances - right-size the type or let Auto Scaling add capacity. An instance that can’t reach AWS services usually lacks the right IAM role or network path (endpoint or NAT). Data lost after an instance stopped means it was on instance store rather than EBS - the fix is durable EBS for anything that must persist. An instance that fails a load-balancer health check gets removed from rotation - investigate the application health on that instance. And a surprising bill often traces to oversized instances or orphaned EBS volumes and snapshots left behind. The diagnostic discipline is to check utilization metrics, the IAM role and networking, and storage type against the persistence requirement.

1.8 EC2 Purchasing Options at a Glance

The pricing volume covers these in depth, but this quick comparison anchors the compute-cost choices that recur throughout the exam.

Option Commitment Discount Best for
On-Demand None None (baseline) Spiky, unpredictable, short-term, dev
Savings Plans 1 or 3 yr ($/hr) Large Steady baseline; Compute SP = flexible across EC2/Fargate/Lambda
Reserved Instances 1 or 3 yr (attributes) Largest (Standard) Steady, known instance type; Convertible = exchangeable
Spot None (spare capacity) Up to ~90% Fault-tolerant, interruptible (batch, CI); 2-min reclaim warning
Dedicated Host On-Demand or reserved Varies Per-socket/core BYOL licensing, compliance, hardware visibility

The senior pattern: commit the steady baseline (Savings Plans/RIs), absorb peaks On-Demand or serverless, run interruptible work on Spot, and reserve Dedicated Hosts only for licensing or isolation.

Interview Question

What are the key choices when launching an EC2 instance, and what does right-sizing mean?

Launching an EC2 instance comes down to a few key choices that determine its behaviour, cost, and security. The Amazon Machine Image (AMI) is the template the instance boots from - the operating system and any pre-installed software - so choosing or building a hardened, standardized AMI matters for consistency and security. The instance type sets the hardware profile - the amount of CPU, memory, and network capability - and instance types come in families optimized for different workloads: general-purpose for balanced needs, compute-optimized for CPU-heavy work, memory-optimized for large in-memory datasets and databases, and storage-optimized for high-I/O workloads, with sizes scaling resources within each family. You also choose the subnet (which VPC and Availability Zone it lives in, and whether it’s public or private), the security groups that control its traffic, the storage (durable EBS volumes versus ephemeral instance store), and the IAM role that lets it call AWS services without stored keys. Right-sizing means choosing the instance family that matches the workload’s actual bottleneck and a size that fits real measured usage plus a sensible headroom - rather than over-provisioning “to be safe,” which wastes money because an oversized instance running continuously is pure waste, or under-provisioning, which throttles performance. It’s driven by data: I look at utilization metrics like CPU and memory over time and pick the smallest instance that comfortably handles the real load, then revisit it as usage changes, because right-sizing is an ongoing discipline, not a one-time guess. It’s one of the highest-impact cost levers, and crucially it doesn’t compromise performance or resilience because it’s based on actual usage. So the key choices are AMI, instance type and size, network placement, storage, security groups, and IAM role, and right-sizing is the practice of matching the type and size to real workload needs to avoid both waste and throttling - a data-driven balance rather than a safety-margin guess.

Interview Question

What's the difference between EBS and instance store, and why treat instances as disposable?

EBS and instance store are two kinds of storage for EC2 with a critical difference in persistence. EBS - Elastic Block Store - provides durable, network-attached block volumes that persist independently of the instance’s lifecycle: the data survives stopping or terminating the instance (if the volume is retained), volumes can be snapshotted for backup and restored, and they can be detached and re-attached to another instance. Instance store is temporary storage physically attached to the host machine - it’s fast because it’s local, but it’s ephemeral: when the instance stops or is terminated, that data is lost. So the distinction is durability - EBS for anything you must keep, which is the default for most needs, and instance store only for temporary high-speed scratch data like caches that you can afford to lose. A common real incident is losing data because it was written to instance store, so the rule is that persistent data belongs on EBS. The related principle is treating instances as disposable - “cattle, not pets.” A single instance can fail at any time, and AWS designs for that assumption, so resilience never comes from a carefully hand-tuned individual server; it comes from architecture. You place instances behind a load balancer in an Auto Scaling group across multiple Availability Zones, and you configure each instance automatically from an image or bootstrap script rather than by hand, so any instance is interchangeable and can be replaced by an identical one at any moment. Treating instances as disposable means a failed or unhealthy instance is simply terminated and replaced automatically, deployments can roll instances rather than patching them in place, and no unique state lives on any one instance - durable data is on EBS or in managed databases, not on the instance’s local disk. This is what makes the compute tier reliable and scalable: individual instances are cheap, replaceable units, and the system stays healthy by replacing failures rather than nursing pets. So EBS versus instance store is about persistence - keep durable data on EBS - and the disposability mindset is about resilience - never depend on any single instance, keep them interchangeable and automatically replaceable, and hold state off the instance.

Chapter Revision Notes

  • An EC2 instance is defined by its AMI (OS template), instance type/size (hardware profile), subnet, security groups, storage, and IAM role.
  • Instance families target workloads (general, compute-, memory-, storage-optimized); right-size to real usage plus headroom, not guesswork.
  • EBS = durable network-attached volumes (snapshot, persist, re-attach); instance store = fast but ephemeral scratch.
  • Treat instances as disposable “cattle” - behind a load balancer, in an Auto Scaling group across AZs, auto-configured, state off-instance.
  • Right-sizing and cleaning up orphaned volumes/snapshots are high-impact cost levers.