AWS EC2 CLI – Cheat sheet

Below is the cheat sheet of AWS CLI commands for EC2.

If you are new to EC2, it’s recommended that you go through this free AWS EC2 crash course.

If you want to know how to install AWS CLI please follow steps on this post

Get help

aws ec2 help

Create instance EC2 Classic

aws ec2 run-instances --image-id ami-xxxxxxxx --count 1 --instance-type t1.micro --key-name MyKeyPair --security-groups my-sg

Create instance in VPC

aws ec2 run-instances --image-id ami-xxxxxxxx --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-xxxxxxxx --subnet-id subnet-xxxxxxxx

Start instance

aws ec2 start-instances --instance-ids <instance-id>

Stop instance

aws ec2 stop-instances --instance-ids <instance-id>

Reboot instance 

aws ec2 reboot-instances --instance-ids <instance-id>

Terminate instance

aws ec2 terminate-instances --instance-ids <instance-id>

View console output

aws ec2 get-console-output --instance-id <instance-id>

Describe Instance

aws ec2 describe-instances --instance-ids <instance-id>

Create an AMI

aws ec2 create-image \
 --instance-id <instance-id> \
 --name myAMI \
 --description 'CloudVedas Test AMI'

List images(AMIs)

aws ec2 describe-images --image-ids <ami-id>

List  security groups

aws ec2 describe-security-groups

Create security group

aws ec2 create-security-group --vpc-id vpc-1234abcd --group-name db-access --description "cloudvedas db access"

Get details of security group

aws ec2 describe-security-groups --group-names <group-name>

Delete Security group

aws ec2 delete-security-group --group-id sg-1234abcd

List key pairs

aws ec2 describe-key-pairs

Create keypair

aws ec2 create-key-pair --key-name <value>

Import keypair

aws ec2 import-key-pair --key-name keyname_test --public-key-material file:///cldvds/sagu/

Delete keypair

aws ec2 delete-key-pair --key-name <value>

Check the networking attribute

aws ec2 describe-instance-attribute --instance-id <instance-id> --attribute sriovNetSupport

Add tags to instance

aws ec2 create-tags --resources i-xxxxxxxx --tags Key=Name,Value=MyInstance

Add EBS volume

aws ec2 --block-device-mappings "[{\"DeviceName\":\"/dev/sdf\",\"Ebs\":{\"VolumeSize\":20,\"DeleteOnTermination\":false}}]"

List EBS volumes

aws ec2 describe-volumes

Check snapshot associated with EBS volume

aws ec2 describe-volumes --volume-ids vol-01c6l3de3v21bd46s

Note:- All the above commands are taken from different AWS EC2 CLI reference guides and put in one place over here. Please run the commands after due diligence as we won’t be responsible for any mistakes in executing the commands and it’s consequences.  If you have any concern or query feel free to contact us.