AWS EC2 Cheat Sheet

Here’s a cheat sheet for common AWS EC2 (Elastic Compute Cloud) commands and operations using the AWS Command Line Interface (AWS CLI):

EC2 Instances

List EC2 Instances:

aws ec2 describe-instances

Describe Specific Instance:

aws ec2 describe-instances --instance-ids i-1234567890abcdef0

Create EC2 Instance:

aws ec2 run-instances --image-id ami-12345678 --instance-type t2.micro --key-name your-key-pair

Terminate EC2 Instance:

aws ec2 terminate-instances --instance-ids i-1234567890abcdef0

Key Pairs

List Key Pairs:

aws ec2 describe-key-pairs

Create Key Pair:

aws ec2 create-key-pair --key-name your-key-pair --query 'KeyMaterial' --output text > your-key-pair.pem

Import Key Pair:

aws ec2 import-key-pair --key-name your-key-pair --public-key-material file://your-key-pair.pub

Security Groups

List Security Groups:

aws ec2 describe-security-groups

Create Security Group:

aws ec2 create-security-group --group-name your-security-group --description "Your Security Group"

Authorize Ingress Rule:

aws ec2 authorize-security-group-ingress --group-id sg-0123456789abcdef0 --protocol tcp --port 22 --cidr 0.0.0.0/0

Volumes and Snapshots

List Volumes:

aws ec2 describe-volumes

Create EBS Volume:

aws ec2 create-volume --availability-zone us-east-1a --size 8 --volume-type gp2

Attach Volume to Instance:

aws ec2 attach-volume --volume-id vol-0123456789abcdef0 --instance-id i-1234567890abcdef0 --device /dev/sdf

Create EBS Snapshot:

aws ec2 create-snapshot --volume-id vol-0123456789abcdef0 --description "Your Snapshot"

Elastic Load Balancer (ELB)

List Load Balancers:

aws elbv2 describe-load-balancers

Create Application Load Balancer:

aws elbv2 create-load-balancer --name your-load-balancer --subnets subnet-12345678 subnet-87654321 --security-groups sg-0123456789abcdef0

EC2 Spot Instances

Request Spot Instances:

aws ec2 request-spot-instances --spot-price "0.03" --instance-count 1 --type "one-time" --launch-specification file://spot-instance-config.json

List Spot Instance Requests:

aws ec2 describe-spot-instance-requests

EC2 Auto Scaling

Create Auto Scaling Group:

aws autoscaling create-auto-scaling-group --auto-scaling-group-name your-asg --launch-configuration-name your-launch-config --min-size 2 --max-size 5 --desired-capacity 3 --vpc-zone-identifier subnet-12345678 subnet-87654321

List Auto Scaling Groups:

aws autoscaling describe-auto-scaling-groups

These commands cover some basic AWS EC2 CLI operations. Adjust the parameters according to your specific use case and explore the AWS CLI documentation for more options and details: AWS CLI EC2 Command Reference.