AWS Cheatsheet
EC2
Use this AWS reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Launch & Terminate
# Launch instance aws ec2 run-instances \ --image-id ami-0c55b159cbfafe1f0 \ --instance-type t3.micro \ --key-name my-keypair \ --security-group-ids sg-0abc123 \ --subnet-id subnet-0abc123 \ --count 1 \ --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=web-server}]' # Terminate aws ec2 terminate-instances --instance-ids i-0abc123def456 # Stop / Start / Reboot aws ec2 stop-instances --instance-ids i-0abc123 aws ec2 start-instances --instance-ids i-0abc123 aws ec2 reboot-instances --instance-ids i-0abc123
Describe & Filter Instances
# All instances (table view) aws ec2 describe-instances --output table # Filter by state aws ec2 describe-instances \ --filters "Name=instance-state-name,Values=running" # Filter by tag aws ec2 describe-instances \ --filters "Name=tag:Name,Values=web-server" # Get public IPs of running instances aws ec2 describe-instances \ --filters "Name=instance-state-name,Values=running" \ --query 'Reservations[*].Instances[*].[InstanceId,PublicIpAddress,Tags[?Key==`Name`].Value|[0]]' \ --output table
Key Pairs
aws ec2 create-key-pair --key-name my-keypair \ --query 'KeyMaterial' --output text > my-keypair.pem chmod 400 my-keypair.pem aws ec2 describe-key-pairs aws ec2 delete-key-pair --key-name my-keypair # Import existing public key aws ec2 import-key-pair --key-name my-keypair \ --public-key-material fileb://~/.ssh/id_rsa.pub
Security Groups
# Create aws ec2 create-security-group \ --group-name my-sg \ --description "Web server SG" \ --vpc-id vpc-0abc123 # Allow SSH from a specific IP aws ec2 authorize-security-group-ingress \ --group-id sg-0abc123 \ --protocol tcp --port 22 \ --cidr 203.0.113.0/24 # Allow HTTP from anywhere aws ec2 authorize-security-group-ingress \ --group-id sg-0abc123 \ --protocol tcp --port 80 --cidr 0.0.0.0/0 # Revoke a rule aws ec2 revoke-security-group-ingress \ --group-id sg-0abc123 \ --protocol tcp --port 22 --cidr 0.0.0.0/0 aws ec2 describe-security-groups --group-ids sg-0abc123 aws ec2 delete-security-group --group-id sg-0abc123
AMIs
# Create AMI from running instance aws ec2 create-image \ --instance-id i-0abc123 \ --name "my-app-v1.0" \ --no-reboot # List your AMIs aws ec2 describe-images --owners self \ --query 'Images[*].[ImageId,Name,CreationDate]' --output table # Find latest Amazon Linux 2023 AMI aws ec2 describe-images \ --owners amazon \ --filters "Name=name,Values=al2023-ami-*-x86_64" \ "Name=state,Values=available" \ --query 'sort_by(Images, &CreationDate)[-1].ImageId' \ --output text # Deregister AMI aws ec2 deregister-image --image-id ami-0abc123
EBS Volumes
# Create volume aws ec2 create-volume \ --size 20 \ --volume-type gp3 \ --availability-zone us-east-1a # Attach to instance aws ec2 attach-volume \ --volume-id vol-0abc123 \ --instance-id i-0abc123 \ --device /dev/xvdf # Detach aws ec2 detach-volume --volume-id vol-0abc123 # Create snapshot aws ec2 create-snapshot \ --volume-id vol-0abc123 \ --description "backup-2024-01" # List snapshots aws ec2 describe-snapshots --owner-ids self \ --query 'Snapshots[*].[SnapshotId,VolumeSize,StartTime,Description]' \ --output table
Instance Types Quick Reference
| Family | Optimized For | Examples |
|---|---|---|
t3/t4g | Burstable general purpose | t3.micro, t4g.small |
m7i/m7g | Balanced (prod general) | m7i.large, m7g.xlarge |
c7i/c7g | Compute intensive | c7i.2xlarge |
r7i/r7g | Memory intensive | r7i.4xlarge |
i4i | Storage (NVMe SSD) | i4i.xlarge |
p4d/p5 | GPU (ML training) | p4d.24xlarge |
inf2 | ML inference (Inferentia) | inf2.xlarge |
g5 | GPU (rendering, inference) | g5.xlarge |
User Data (startup script)
aws ec2 run-instances \ --image-id ami-0c55b159cbfafe1f0 \ --instance-type t3.micro \ --user-data file://init.sh \ ...
# init.sh #!/bin/bash yum update -y yum install -y nginx systemctl enable --now nginx
Elastic IPs
# Allocate aws ec2 allocate-address --domain vpc # Associate with instance aws ec2 associate-address \ --instance-id i-0abc123 \ --allocation-id eipalloc-0abc123 # Release (to avoid charges when unattached) aws ec2 release-address --allocation-id eipalloc-0abc123 aws ec2 describe-addresses
Auto Scaling
# Create launch template aws ec2 create-launch-template \ --launch-template-name web-lt \ --version-description "v1" \ --launch-template-data file://lt-data.json # Create Auto Scaling group aws autoscaling create-auto-scaling-group \ --auto-scaling-group-name web-asg \ --launch-template "LaunchTemplateName=web-lt,Version=1" \ --min-size 1 --max-size 5 --desired-capacity 2 \ --vpc-zone-identifier "subnet-0abc,subnet-0def" # Scale manually aws autoscaling set-desired-capacity \ --auto-scaling-group-name web-asg \ --desired-capacity 3
Instance Metadata (from inside EC2)
# IMDSv2 (recommended) TOKEN=$(curl -sX PUT "http://169.254.169.254/latest/api/token" \ -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") curl -s http://169.254.169.254/latest/meta-data/instance-id \ -H "X-aws-ec2-metadata-token: $TOKEN" curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/ \ -H "X-aws-ec2-metadata-token: $TOKEN"
Placement Groups
| Type | Use Case |
|---|---|
cluster | Low-latency HPC — all instances in same AZ/rack |
spread | Max resilience — each instance on separate hardware |
partition | Large distributed systems (Kafka, Cassandra) — rack-level isolation |
aws ec2 create-placement-group --group-name my-pg --strategy cluster