Complete guide to Amazon EC2 — Understanding instance types, pricing models, storage, and best practices.


What is Amazon EC2?

Amazon EC2 (Elastic Compute Cloud) provides scalable computing capacity in the AWS Cloud. It eliminates the need to invest in hardware up front, so you can develop and deploy applications faster.

In practice: EC2 gives you a virtual machine in the cloud with full OS-level control, including software installation and sizing changes as your workload evolves.


EC2 Architecture

flowchart TD
    subgraph EC2["EC2 Service"]
        I1["🖥️ Instance 1<br/>AMI: ami-01<br/>Type: m5.large<br/>State: running"]
        I2["🖥️ Instance 2<br/>AMI: ami-02<br/>Type: t3.medium<br/>State: stopped"]
        IN["🖥️ Instance N<br/>AMI: ami-01<br/>Type: c5.xlarge<br/>State: running"]
        
        SG["🔐 Security Group"]
        EBS["💾 EBS Volumes"]
    end
    
    I1 --> SG
    I2 --> SG
    IN --> SG
    I1 --> EBS
    I2 --> EBS
    IN --> EBS

EC2 Components

1. AMI (Amazon Machine Image)

An AMI is a template for instances. It contains:

AMIs exist so you can launch servers in a repeatable, controlled way. Instead of manually installing OS packages and app dependencies every time, you launch from a known image and get consistent results across environments.

ComponentDescription
Root Volume TemplateOperating system (Linux, Windows, etc.)
Launch PermissionsWhich AWS accounts can use the AMI
Block Device MappingVolumes to attach at launch

AMI Types:

TypeSourceUse Case
AWS-ProvidedAmazonQuick start, tested, supported
CommunityOther AWS usersVerified, community AMIs
MarketplaceThird-party vendorsPre-configured software
CustomYou createYour specific configuration

Note: AMIs are region-specific. To use in another region, copy the AMI. When to use what: Start with AWS-provided AMIs for learning and early builds, move to custom AMIs (golden images) for production standardization, and use Marketplace AMIs when you need pre-licensed or vendor-managed software.


2. Instance Types

Common Pattern: {family-token}.{size}

Instance types exist because workloads fail for different reasons: some are CPU-bound, some memory-bound, some network-bound. Picking the right family and size improves both performance and cost efficiency.

Example: m5.large
         │   │
         │   └─ Size (nano, micro, small, medium, large, xlarge, 2xlarge, etc.)
         └───── Family token (family + generation + options; e.g., m5, c7g, c7gn)

Instance Families

FamilyOptimized ForvCPU:Memory RatioUse Cases
General Purpose (m)Balanced1:4Web servers, app servers
Burstable (t)Low cost, burstableVariableDev/test, microservices
Compute Optimized (c)High CPU1:2Batch processing, gaming
Memory Optimized (r)High memory1:8/1:16Databases, caches, in-memory
Accelerated (p, g, inf)GPUs/FP GAsVariableML, graphics, HPC
Storage Optimized (i, d, h)High disk I/OVariableData warehouses, file servers
Graviton (t4g, m6g, c6g)ARM-based, cost-effectiveVariableLinux workloads
SizevCPUsMemoryNetworkExample Uses
nano20.5 GBLowMicroservices
micro11 GBLowDev environments
small12 GBLow to moderateSmall apps
medium24 GBLow to moderateWeb servers
large28 GBLow to moderateApp servers
xlarge416 GBModerateProduction apps
2xlarge832 GBModerateHigh-performance apps
4xlarge1664 GBHighDatabases
8xlarge32128 GBHighBig data
16xlarge64256 GBHighML training

Generation Comparison

FamilyGen 4Gen 5Gen 6
Generalm4m5m6i/m6g
Computec4c5c6i/c6g
Memoryr4r5r6i/r6g

Rule: Newer generations typically offer better price-performance. Selection tip: Choose family first (workload profile), then size (capacity), then generation (price-performance). Start slightly above your expected baseline and right-size using CloudWatch metrics.


EC2 Pricing Models

EC2 pricing models exist to let you trade flexibility vs commitment. The more predictable your usage, the more you can usually save.

1. On-Demand Instances

AttributeValue
CommitmentNone
Upfront CostNone
SavingsNone (base price)
TermPay by second/hour
Best ForShort-term, spiky, unpredictable workloads

Use On-Demand when requirements are still changing. It is the safest default for new systems because there is no lock-in, and later you can move steady portions to Savings Plans or RIs.

Advantages:

  • No commitment
  • Easy to start/stop
  • Highest cost

2. Reserved Instances (RI)

AttributeValue
Commitment1 or 3 years
Upfront CostAll, partial, or no upfront
SavingsUp to 75%
TermFixed term
Best ForSteady-state, predictable usage

RIs exist to reward long-term commitment on stable workloads. Use them only after you have measured a consistent baseline; otherwise you risk paying for unused commitment.

RI Purchase Options:

OptionUpfrontSavingsUse Case
All Upfront100%Highest (up to 75%)Have budget, know usage
Partial Upfront~50%High (~60-70%)Balance upfront vs monthly
No Upfront0%Moderate (~40-50%)No upfront budget

RI Scope:

ScopeDescriptionFlexibility
RegionalDiscount applies to any instance in region (within family)Highest
ZonalDiscount applies to specific AZLower
DedicatedTenancy dedicatedSpecific use case

Note: Reserved Instances are a billing discount, not a physical instance reservation.


3. Spot Instances

AttributeValue
CommitmentNone
Upfront CostNone
SavingsUp to 90%
TermCan be interrupted with 2-minute warning
Best ForFault-tolerant, flexible workloads

Spot exists so AWS can sell spare capacity at deep discounts. Use Spot when interruption is acceptable and your workload can retry, checkpoint, or restart safely.

How Spot Works:

Spot Price = Supply/Demand

Your Max Price ≥ Spot Price → Instance runs
Your Max Price < Spot Price → Instance interrupted (2-min warning)

Spot Best Practices:

  • Use with Auto Scaling Groups
  • Implement graceful shutdown
  • Use Spot Instance Interruption Notices
  • Don’t use for stateful workloads

Use Cases:

  • Batch processing
  • Big data processing
  • CI/CD
  • Containerized workloads
  • Web crawling

4. Savings Plans

AttributeValue
Commitment1 or 3 years
Upfront CostNone or flexible
SavingsUp to 72%
ScopeCompute or instance family
Best ForFlexible compute commitment

Savings Plans are often the easiest way to reduce cost without tightly locking instance attributes. They are usually a better first optimization than RIs for teams that change instance families or move between EC2, Fargate, and Lambda.

Savings Plan Types:

TypeScopeSavingsFlexibility
Compute SPAll EC2, Fargate, LambdaUp to 66%Highest
EC2 Instance SPSpecific instance family in regionUp to 72%Lower

Savings Plans vs RI: Savings Plans offer more flexibility; RIs offer higher savings for specific instances.


5. Dedicated Hosts

AttributeValue
CommitmentNone for On-Demand hosts; 1 or 3 years with Dedicated Host Reservations
Upfront CostVaries
SavingsVaries
TermOn-demand or reservation term based
Best ForCompliance, licensing (BYOL)

Dedicated Hosts exist for physical isolation and software licensing controls. Use them when regulations or BYOL contracts require visibility or affinity to a specific host.

Use Cases:

  • Per-core software licensing
  • Strong compliance requirements
  • Full control over instance placement

EC2 Storage Options

Storage options exist because compute and data have different lifecycles. Some data must survive restarts and replacement; some data is temporary and only needs speed.

EBS (Elastic Block Store)

Network-attached block storage for EC2 instances.

EBS helps by decoupling storage from a single host so data can persist even when an instance is stopped, replaced, or recovered.

Volume TypeUse CaseIOPSThroughput
gp2/gp3General purpose SSDUp to 16,000Up to 1,000 MB/s
io1/io2High-performance SSDUp to 64,000/256,000Up to 1,000/4,000 MB/s
st1Throughput-optimized HDD~500Up to 500 MB/s
sc1Cold HDD~250Up to 250 MB/s

In practice, gp3 is the default for most application and boot volumes. Use io1/io2 for latency-sensitive databases with consistently high IOPS needs. Use st1/sc1 only for large sequential workloads where low cost per GB is more important than latency.

Instance Store

Physically-attached ephemeral storage.

CharacteristicValue
PersistencePersists for the instance lifetime on that host (lost on stop/hibernate/terminate or host failure)
PerformanceVery high
CostIncluded with instance
Use CasesTemporary data, caching, buffers

Instance Store exists for very fast local disk with no separate volume billing. Use it for scratch space, caches, temporary buffers, or intermediate processing outputs. Avoid it for irreplaceable data, because data is lost when the instance stops/terminates or host fails.

Quick decision: If data must survive instance replacement, use EBS. If data is disposable and speed matters most, use Instance Store.


EC2 Networking

Security Groups

Virtual firewall at instance level.

Security Groups exist to enforce least-privilege network access close to the workload. They help by making allowed traffic explicit and stateful, which simplifies return traffic handling.

FeatureValue
TypeStateful
RulesAllow only
ScopeInstance level
Return TrafficAutomatic

Use separate Security Groups by role (for example, web, app, db) and allow traffic between groups instead of broad CIDR ranges whenever possible.

Elastic IP

Static public IP address.

Elastic IPs exist so public endpoints can stay stable when instances are replaced. This is helpful for legacy allowlists and fixed external integrations, but use them sparingly because public IPv4 addresses are billed.

AttributeValue
Cost (In Use)Charged hourly for public IPv4 addresses (including Elastic IPs)
Cost (Idle/Unattached)Charged hourly (same public IPv4 charge model)
Limit5 per region (soft limit, can increase)

For most production apps, prefer an ALB/NLB, Route 53, or Global Accelerator rather than attaching many Elastic IPs directly to instances.

Placement Groups

Logical grouping of instances.

Placement Groups exist to control how EC2 places instances on hardware, which lets you optimize for either performance or fault isolation.

TypeDescriptionUse CaseLimit
ClusterLow latency, high bandwidthHPC, MLSingle AZ
PartitionSpread across partitions (reduced failure blast radius)Hadoop, Kafka7 partitions per group
SpreadDistinct underlying hardware per instance for failure isolationCritical appsUp to 7 running instances per AZ per group

Choose Cluster for low-latency east-west traffic, Partition for distributed systems that need blast-radius separation, and Spread for small sets of critical instances that must not share hardware.


EC2 Lifecycle

Instance States

flowchart LR
    pending["⏳ pending"] --> running["▶️ running"]
    running --> stopping["⏸️ stopping"]
    stopping --> stopped["⏹️ stopped"]
    stopped --> pending
    running --> shutting["🔄 shutting-down"]
    stopped --> shutting
    shutting --> terminated["❌ terminated"]
StateBillingData Persistence
pendingNo
runningYesEBS yes, Instance Store yes (persists across reboot)
stoppingNo (except hibernation)
stoppedNoEBS yes, Instance Store no (data lost on stop)
shutting-downNo
terminatedNoEBS default delete (can change), Instance Store no

Lifecycle states matter because they directly affect both billing and data durability. Stopping an instance can reduce compute cost while preserving EBS data, whereas termination is intended for permanent removal.


EC2 Features

Auto Scaling

Automatically scale EC2 capacity based on conditions.

Auto Scaling exists to keep performance stable under changing load while avoiding overprovisioning. Use target tracking first for most workloads, then layer scheduled scaling when traffic patterns are predictable.

Scaling PolicyDescription
Target TrackingMaintain target metric (e.g., CPU 50%)
Simple ScalingAdd/remove instances based on threshold
Step ScalingScale based on step adjustments
Scheduled ScalingScale at specific times

Elastic Load Balancing (ELB)

Distribute incoming traffic across multiple targets.

ELB helps by decoupling traffic entry from individual instances, enabling health checks, rolling deployments, and horizontal scaling.

TypeUse CaseFeatures
Application LBHTTP/HTTPSContent-based routing, path-based routing
Network LBTCP/UDP/TLSExtreme performance, static IPs
Gateway LBLayer 3 applicationsIP protocol routing

Use ALB for web applications and microservices, NLB for high-throughput low-latency TCP/UDP, and GWLB when inserting virtual network appliances.

User Data

Script executed at first launch.

User Data exists for bootstrap automation so instances become usable immediately after launch. It is best for lightweight setup; for complex configuration, pair it with configuration management or image-based workflows.

#!/bin/bash
# Install updates
yum update -y
 
# Install Apache
yum install -y httpd
 
# Start Apache
systemctl start httpd
systemctl enable httpd
 
# Create webpage
echo "<h1>Hello from $(hostname -f)</h1>" > /var/www/html/index.html

Instance Metadata

Retrieve instance information from within the instance.

Instance metadata helps applications discover runtime context (instance ID, IAM role credentials, region/AZ) without hardcoding values.

# Get instance ID
curl http://169.254.169.254/latest/meta-data/instance-id
 
# Get availability zone
curl http://169.254.169.254/latest/meta-data/placement/availability-zone
 
# Get local IP
curl http://169.254.169.254/latest/meta-data/local-ipv4
 
# Get IAM role credentials
curl http://169.254.169.254/latest/meta-data/iam/security-credentials/

For security, require IMDSv2 and block unnecessary metadata access from untrusted processes.


EC2 vs Lambda: When to Use

Use EC2 When…Use Lambda When…
Long-running processesEvent-driven workloads
Full OS control neededStateless functions
Complex applicationsSimple tasks
Consistent performance neededSpiky/unpredictable traffic
Need direct hardware accessWant to avoid managing servers
Running containers (without ECS/EKS)Quick deployments
Need specialized hardware (GPU)Short execution time

EC2 Best Practices

PracticeWhy
Use AMIs for consistencyReproducible deployments
Use Security Groups, not security by obscurityExplicit access control
Tag your instancesCost allocation, organization
Use IAM roles, not access keysNo credential management
Monitor with CloudWatchVisibility and alerting
Use Auto ScalingCost optimization + availability
Use placement groups for performanceLower latency
Use Reserved Instances for steady workloadsCost savings
Use Spot for fault-tolerant workloadsMaximum savings
Enable detailed monitoring1-minute metrics

TL;DR

EC2 Pricing Decision Tree

Workload Type
    ├── Predictable, steady-state
    │   └── Reserved Instances (up to 75% savings)
    ├── Flexible, changing compute needs
    │   └── Savings Plans (up to 72% savings)
    ├── Fault-tolerant, can be interrupted
    │   └── Spot Instances (up to 90% savings)
    └── Short-term, unpredictable
        └── On-Demand (base price)

Key Points

  • EC2 = Virtual machines in AWS (IaaS)
  • AMI = Template (OS + software)
  • Instance Types = CPU/memory/storage combo (e.g., m5.large)
  • Pricing = On-Demand (base), Reserved (save up to 75%), Spot (save up to 90%), Savings Plans (flexible)
  • EBS = Persistent network storage; Instance Store = High-speed local storage (ephemeral across stop/terminate)
  • Security Groups = Stateful firewall for instances
  • Auto Scaling = Automatic scaling based on demand
  • ELB = Distribute traffic across instances

Resources

Amazon EC2 Documentation Complete EC2 user guide.

EC2 Instance Types Detailed comparison of all instance types.

EC2 Pricing Detailed pricing for all purchasing options.

EC2 AMI Catalog Browse and select AMIs in the AWS Console.