Amazon EC2 — Elastic Compute Cloud

AWS-EC2

Virtual servers on demand — instance types, AMIs, purchasing options, and how EC2 fits into a broader architecture.

awsec2computeinstancesamispotreserved

Overview

Amazon EC2 is the foundational compute service in AWS. An EC2 instance is a virtual machine running in an AWS Availability Zone, inside a VPC subnet, on a physical host managed by AWS. You choose the operating system, the hardware profile, the network configuration, and the storage. You get full control of the OS and everything running on top of it.

EC2 occupies the IaaS position in the AWS service hierarchy: maximum control, maximum responsibility. Every other compute service in AWS — ECS, EKS, Lambda, Fargate, Elastic Beanstalk — either runs on EC2 underneath or replaces it with a managed abstraction. Understanding EC2 means understanding the foundation that the rest of AWS compute is built on.


Instance Families

AWS organizes instance types into families, each optimized for a different hardware profile. The naming convention is: family``generation``additional-features.size.

For example: m7g.xlargem (general purpose), generation 7, g (Graviton processor), size xlarge.

Instance Family Reference

FamilyProfilePrimary Use Cases
t (burstable)Low baseline CPU, credit-based burstingDevelopment environments, low-traffic web servers, microservices with spiky traffic, CI build agents
m (general purpose)Balanced CPU, memory, and networkProduction web tiers, application servers, small databases, code repositories
c (compute optimized)High CPU:memory ratioWeb servers, batch processing, HPC, video encoding, scientific modeling, CPU-intensive data processing
r (memory optimized)High memory:CPU ratioIn-memory databases (Redis self-managed), real-time big data analytics, in-memory caches, SAP workloads
x (extreme memory)Highest memory per vCPUSAP HANA, large in-memory databases, HPC with large working sets
p (GPU compute)NVIDIA GPUsML training, scientific simulations, seismic analysis
g (GPU graphics/ML)NVIDIA GPUs balanced with CPUML inference, graphics rendering, game streaming, video transcoding
i (storage optimized)High-throughput local NVMe SSDNoSQL databases (Cassandra, MongoDB), data warehousing, high-IOPS log processing
d (dense HDD storage)High-capacity local HDDMassive parallel processing, Hadoop/Spark, data lakes (raw landing zone)
Graviton (arm suffix g)AWS-designed ARM CPUsGeneral purpose (M7g), compute (C7g), memory (R7g) — 20–40% better price/performance vs x86 for most workloads

Sizes

Within each family and generation, sizes scale proportionally:

nanomicrosmallmediumlargexlarge2xlarge4xlarge8xlarge12xlarge16xlarge24xlarge48xlargemetal

Each step roughly doubles vCPU, memory, and network bandwidth. Metal instances provide bare-metal access to the physical host — no hypervisor, useful for workloads that require direct hardware access or bring their own hypervisor.

T-Family Burst Mechanics

T-family instances (T3, T4g) earn CPU credits when running below baseline and spend credits when bursting above. The baseline CPU percentage is proportional to the instance size. When credits are exhausted, the instance is throttled back to baseline. T3/T4g instances operate in Unlimited mode by default — they can burst beyond credit balance and incur a small charge per excess CPU-second. Unlimited mode prevents performance degradation at the cost of variable compute charges.


Amazon Machine Images (AMIs)

An AMI is a template for launching an EC2 instance. It defines:

AMI Sources

SourceDescriptionUse Case
AWS providedAmazon Linux 2023, Ubuntu, Windows Server, RHEL, SUSE. Maintained by AWS or distribution partners.Standard starting points for clean instances
AWS MarketplacePre-configured AMIs from vendors — Fortinet firewalls, Palo Alto, CIS-hardened images, commercial databases. May incur hourly software license charges on top of EC2.Licensing BYOL software or using vendor appliances
Community AMIsPublicly shared by other AWS users.Not recommended for production — no vetting of security or configuration
Custom (Golden) AMIsAMIs you create from a configured instance. Bake in agents, configuration baselines, compliance settings.Rapid, consistent instance provisioning. Eliminates per-launch configuration time.

AMI Key Behaviors


Purchasing Options

EC2 cost is driven primarily by purchasing model. Choosing the wrong model for a workload — paying On-Demand for a database that runs 24/7, or using Reserved Instances for a batch job that runs 6 hours per week — results in significant overspending.

Purchasing Model Comparison

ModelTypical Discount vs On-DemandCommitmentInterruption RiskBest For
On-DemandNoneNoneNoneUnpredictable workloads, spikes, dev/test, short-term experiments
Reserved Instances (1yr, no upfront)~40%1 yearNoneKnown steady-state workloads, payment flexibility preferred
Reserved Instances (3yr, all upfront)~60–72%3 yearsNoneLong-lived production workloads, maximum savings priority
Savings Plans (Compute, 1yr)~54%1 year $/hr commitmentNoneFlexible coverage across EC2, Lambda, Fargate — recommended over RIs for most
Savings Plans (EC2, 1yr)~66%1 year $/hr in family/regionNoneWhen committed to a specific instance family in a specific region
Spot InstancesUp to 90%NoneHigh (2-min termination notice)Stateless batch, EMR, rendering, CI/CD agents, fault-tolerant workloads
Dedicated HostsMore expensive; BYOL savingsOn-Demand or RINoneBYOL software (Windows Server, SQL Server per-socket), compliance requiring dedicated physical server
Dedicated InstancesSmall premium over On-DemandNoneNoneWorkloads requiring physical isolation from other customers without BYOL requirements

Reserved Instances — Standard vs Convertible

Savings Plans — Preferred Over RIs

Compute Savings Plans apply across EC2 instance families, sizes, regions, OS types, Lambda, and Fargate. You commit to spending $X per hour for 1 or 3 years. Usage is automatically matched to your commitment regardless of where and how you run compute. This flexibility makes Savings Plans the preferred choice over Standard RIs for most organizations.

Spot Instances — Architecture Implications

Spot capacity can be reclaimed by AWS with a 2-minute warning when demand rises. Building on Spot requires designing for interruption:

Spot is not appropriate for databases, stateful services, or anything where an unexpected termination causes data loss or prolonged recovery.


Placement Groups

Placement groups control how EC2 instances are physically placed on hardware within a region.

TypePhysical ArrangementUse CaseConstraint
ClusterSame rack, same AZ, low-latency high-bandwidth interconnectHPC, tightly-coupled distributed computing (MPI), ML training across instancesSingle AZ only; rack failure affects all instances
SpreadDifferent underlying hardware per instanceSmall number of critical instances that must not fail together (primary + standby pairs)Max 7 instances per AZ per placement group
PartitionInstances in partitions; each partition isolated to its own rackLarge distributed systems (Hadoop, Cassandra, Kafka) that tolerate partition failure but need rack isolationUp to 7 partitions per AZ; hundreds of instances

Cluster placement groups use the same physical rack, which means rack failure takes out all instances — they trade fault tolerance for maximum network performance. Use cluster placement groups only when inter-instance bandwidth is the performance bottleneck, and accept the single-rack fault domain.


Storage Options

Amazon EBS (Elastic Block Store)

EBS provides persistent network-attached block storage. EBS volumes persist independently of the EC2 instance — you can detach a volume from one instance and attach it to another (within the same AZ). EBS volumes survive instance termination if “Delete on Termination” is set to false.

EBS Volume Types:

TypeIOPSThroughputUse Case
gp3 (General Purpose SSD)Up to 16,000 IOPS (configurable)Up to 1,000 MB/sDefault choice for most workloads. 3,000 IOPS baseline at no extra cost.
io2 Block ExpressUp to 256,000 IOPSUp to 4,000 MB/sHighest-performance SQL/NoSQL requiring sub-ms latency. SAN replacement.
st1 (Throughput HDD)500 MB/sLarge sequential reads/writesLog processing, Kafka data volumes, large file processing
sc1 (Cold HDD)250 MB/sLowest cost per GBInfrequently accessed large volumes; archive data that still needs block access

EBS Multi-Attach (io1/io2 only): Attach one volume to up to 16 instances simultaneously in the same AZ. Requires applications to manage concurrent write coordination (cluster-aware filesystem or application-level locking).

Instance Store

Instance store is ephemeral block storage physically attached to the host machine. It is not network-attached — it communicates directly with the CPU over the PCIe bus, providing the highest possible IOPS and throughput available on EC2.

Critical behavior: data on instance store is lost when the instance stops or terminates. It persists across instance reboots (assuming the host does not fail). Instance store is also not available on all instance types — it is included with I, D, H, and some M families.

Use instance store for:

Never use instance store for data that cannot be reconstructed. If you need maximum IOPS with persistence, use io2 Block Express instead.


EC2 Instance Metadata Service (IMDS)

Every EC2 instance can query the Instance Metadata Service at the link-local address http://169.254.169.254/. The metadata service provides information about the running instance without any AWS API call:

IMDSv1 vs IMDSv2

IMDSv1 (legacy): Simple HTTP GET requests. Vulnerable to Server-Side Request Forgery (SSRF) attacks — if an application on the instance can be tricked into making HTTP requests to arbitrary URLs, an attacker can retrieve IAM credentials from the metadata endpoint.

IMDSv2 (session-oriented): Requires a two-step process:

  1. POST to http://169.254.169.254/latest/api/token with a TTL header to obtain a session token
  2. GET metadata endpoints with the X-aws-ec2-metadata-token header

IMDSv2 breaks SSRF-based metadata attacks because SSRF typically cannot control request headers. IMDSv2 is required by default on new instances as of 2024. Enforce IMDSv2-only at account level using an SCP that denies EC2 instance launches unless the HttpTokens attribute is set to required.

Retrieve IMDSv2 credentials from an application using any AWS SDK — the SDK handles the IMDS interaction automatically and caches credentials, refreshing them before expiry.


EC2 Networking

VPC and Subnet Placement

Every EC2 instance lives in a VPC subnet in a specific AZ. The subnet determines:

Elastic Network Interfaces (ENIs)

Every instance has at least one primary ENI. You can attach additional ENIs from the same VPC to an instance. Use cases:

Enhanced Networking

High-throughput instances use enhanced networking (SR-IOV) for better network performance:


User Data and Instance Initialization

User data is a script (shell script or cloud-init configuration) that runs once on the first boot of an instance. Use it to:

User data runs as root. Limit its scope to bootstrapping — avoid putting application logic in user data. For recurring configuration management, use Systems Manager State Manager or a configuration management system.

For Golden AMIs, bake as much configuration as possible into the AMI at build time. User data at launch time should only handle configuration that varies per environment or per launch (environment-specific secrets, instance registration, etc.). This dramatically reduces launch time.

Developer
AMI Builder (EC2 Image Builder)
Trigger image build pipeline
Base OS + agents + hardening + app runtime
New AMI ID published
ami-0abc123def456789 in us-east-1
Update Launch Template with new AMI ID
Launch Template version 5 → ami-0abc123def456789
Instance Refresh — rolling replacement
Replace 20% of instances at a time, 300s pause
User data runs at first boot
Fetch env-specific config from SSM Parameter Store
Health check passes — instance In Service
Old instance terminated, new instance serves traffic

Security Groups

Security groups are stateful virtual firewalls at the instance level. Rules specify:

Stateful means return traffic is automatically allowed — if your security group allows inbound TCP/443, the response traffic on the same connection is allowed outbound without an explicit outbound rule.

Key behaviors: