AWS

AWS EC2 CLI

AWS EC2 CLI commands for compute instance management.

27 commands
Windows MacOS Linux
#compute #cloud

Instance Management

List all EC2 instances
aws ec2 describe-instances
Launch an instance
aws ec2 run-instances --image-id ami-abc123 --instance-type t3.micro --key-name mykey
Start a stopped instance
aws ec2 start-instances --instance-ids i-1234567890abcdef0
Stop a running instance
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
aws ec2 reboot-instances --instance-ids i-1234567890abcdef0 # Reboot an instance
Terminate an instance
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0

Security Groups

Create security group
aws ec2 create-security-group --group-name my-sg --description "My SG"
List all security groups
aws ec2 describe-security-groups
Add inbound rule
aws ec2 authorize-security-group-ingress --group-id sg-123 --protocol tcp --port 22 --cidr 0.0.0.0/0
Remove inbound rule
aws ec2 revoke-security-group-ingress --group-id sg-123 --protocol tcp --port 22 --cidr 0.0.0.0/0
Delete a security group
aws ec2 delete-security-group --group-id sg-123

Key Pairs

Create key pair
aws ec2 create-key-pair --key-name mykey --query 'KeyMaterial' --output text > mykey.pem
List all key pairs
aws ec2 describe-key-pairs
Delete a key pair
aws ec2 delete-key-pair --key-name mykey
Import public key
aws ec2 import-key-pair --key-name mykey --public-key-material fileb://~/.ssh/id_rsa.pub

AMI Management

List your AMIs
aws ec2 describe-images --owners self
Create AMI from instance
aws ec2 create-image --instance-id i-123 --name "my-ami"
Deregister an AMI
aws ec2 deregister-image --image-id ami-123
Copy AMI to another region
aws ec2 copy-image --source-image-id ami-123 --source-region us-east-1 --name "copy"

EBS Volumes

List all EBS volumes
aws ec2 describe-volumes
Create EBS volume
aws ec2 create-volume --size 100 --volume-type gp3 --availability-zone us-east-1a
Attach volume to instance
aws ec2 attach-volume --volume-id vol-123 --instance-id i-123 --device /dev/sdf
Detach volume from instance
aws ec2 detach-volume --volume-id vol-123
Create volume snapshot
aws ec2 create-snapshot --volume-id vol-123 --description "backup"

Quick Commands

List all EC2 instances and their details
aws ec2 describe-instances
Start one or more stopped EC2 instances
aws ec2 start-instances --instance-ids <id>
Stop one or more running EC2 instances
aws ec2 stop-instances --instance-ids <id>