Skip to content

AWS EC2 — Elastic Compute Cloud

Amazon Elastic Compute Cloud (EC2) is the core compute service of AWS, providing resizable virtual machines in the cloud. It is the AWS equivalent of Azure Virtual Machines.

TermDescription
InstanceA running virtual machine
AMIAmazon Machine Image — the OS + software template for an instance
Instance TypeThe hardware spec (CPU, RAM, GPU, network)
Key PairSSH public/private key for secure access
Security GroupStateful firewall controlling inbound/outbound traffic
Elastic IPStatic public IP address that you own
User DataScript that runs on first boot to configure the instance
FamilyOptimized ForExample Types
t (General purpose, burstable)Dev/test, low-costt3.micro, t4g.small
m (General purpose, balanced)Web servers, small databasesm6i.large, m7g.xlarge
c (Compute optimized)CPU-intensive apps, HPCc6i.large, c7g.2xlarge
r (Memory optimized)In-memory databases, cachesr6i.large, r7g.4xlarge
p / g / trn (Accelerated)ML training, GPU workloadsp4d.24xlarge, g5.xlarge
i / d (Storage optimized)High I/O, data warehousesi4i.large, d3.xlarge

t3.micro is included in the AWS Free Tier (750 hours/month for 12 months).

ModelDescriptionBest For
On-DemandPay by the second, no commitmentDev/test, unpredictable workloads
Reserved Instances1 or 3-year commitment, up to 72% discountSteady-state production workloads
Savings PlansFlexible commitment on compute spend, up to 66% offFlexible reserved-like pricing
Spot InstancesSpare capacity at up to 90% discount — can be interruptedFault-tolerant batch, ML training
Dedicated HostsPhysical server dedicated to youLicensing compliance (Windows Server, SQL Server)
Dedicated InstancesYour instances run on dedicated hardwareCompliance, isolation
StorageDescriptionPersistence
EBS (Elastic Block Store)Network-attached block storage — primary OS diskPersists after stop/terminate (configurable)
Instance StorePhysically attached ephemeral storageLost when instance stops
EFS (Elastic File System)Shared NFS storage across multiple instancesPersistent, shared
S3Object storage accessed via APIPersistent, not a filesystem
Pending → Running → Stopping → Stopped → Terminated
↘ Rebooting
  • Stop — Saves state, no billing for compute (EBS still billed)
  • Terminate — Permanently deleted (EBS may be deleted depending on config)
  • Reboot — Restarts the OS, instance stays on the same host
  • Each EC2 instance belongs to a VPC and a Subnet
  • Security Groups control traffic (default: deny all inbound)
  • Public instances need an Internet Gateway and a public IP or Elastic IP
  • Private instances use a NAT Gateway for outbound internet access
  • ENI (Elastic Network Interface) — Virtual network card that can be moved between instances

Auto Scaling Groups (ASG) automatically adjust the number of instances based on demand:

  • Define minimum, desired, and maximum instance counts
  • Scale out (add instances) on high CPU/memory/custom metric
  • Scale in (remove instances) when demand drops
  • Integrated with Elastic Load Balancer (ELB) for traffic distribution
FeatureAWS EC2Azure VM
VM image formatAMIAzure Managed Image / Marketplace
Disk typeEBS (gp3, io2)Managed Disks (Standard SSD, Premium SSD)
Instance metadata169.254.169.254 IMDS169.254.169.254 IMDS
Reserved pricingReserved Instances / Savings PlansReserved VM Instances
Auto scalingAuto Scaling Group (ASG)Virtual Machine Scale Sets (VMSS)
Load balancerALB / NLBAzure Load Balancer / Application Gateway
Static IPElastic IPStatic Public IP
Terminal window
# Launch an EC2 instance
aws ec2 run-instances \
--image-id ami-0abcdef1234567890 \
--count 1 \
--instance-type t3.micro \
--key-name my-key-pair \
--security-group-ids sg-0abc123 \
--subnet-id subnet-0abc123
# List running instances
aws ec2 describe-instances \
--filters "Name=instance-state-name,Values=running" \
--query 'Reservations[*].Instances[*].[InstanceId,InstanceType,PublicIpAddress]' \
--output table
# Stop an instance
aws ec2 stop-instances --instance-ids i-0abc1234567890
# Terminate an instance
aws ec2 terminate-instances --instance-ids i-0abc1234567890