Cheat Sheet #day18 - Amazon EC2 Commands

Cheat Sheet #day18 - Amazon EC2 Commands

Amazon EC2 Commands Cheat Sheet

Amazon EC2 (Elastic Compute Cloud) allows you to launch and manage virtual servers in the cloud. Below is a cheat sheet of common Amazon EC2 commands using the AWS CLI (Command Line Interface).

Setup

  • Configure AWS CLI

      aws configure
    

    This command will prompt you to enter your AWS Access Key, Secret Key, region, and output format.

EC2 Instances

  • Launch a New EC2 Instance

      aws ec2 run-instances --image-id ami-0abcdef1234567890 --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-12345678 --subnet-id subnet-6e7f829e
    
  • List EC2 Instances

      aws ec2 describe-instances
    
  • Start an EC2 Instance

      aws ec2 start-instances --instance-ids i-1234567890abcdef0
    
  • Stop an EC2 Instance

      aws ec2 stop-instances --instance-ids i-1234567890abcdef0
    
  • Reboot an EC2 Instance

      aws ec2 reboot-instances --instance-ids i-1234567890abcdef0
    
  • Terminate an EC2 Instance

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

Security Groups

  • Create a Security Group

      aws ec2 create-security-group --group-name MySecurityGroup --description "My security group"
    
  • List Security Groups

      aws ec2 describe-security-groups
    
  • Add an Inbound Rule to a Security Group

      aws ec2 authorize-security-group-ingress --group-id sg-12345678 --protocol tcp --port 22 --cidr 0.0.0.0/0
    
  • Remove an Inbound Rule from a Security Group

      aws ec2 revoke-security-group-ingress --group-id sg-12345678 --protocol tcp --port 22 --cidr 0.0.0.0/0
    

Key Pairs

  • Create a Key Pair

      aws ec2 create-key-pair --key-name MyKeyPair --query 'KeyMaterial' --output text > MyKeyPair.pem
    
  • List Key Pairs

      aws ec2 describe-key-pairs
    
  • Delete a Key Pair

      aws ec2 delete-key-pair --key-name MyKeyPair
    

Elastic IP Addresses

  • Allocate a New Elastic IP Address

      aws ec2 allocate-address
    
  • Associate an Elastic IP Address with an Instance

      aws ec2 associate-address --instance-id i-1234567890abcdef0 --allocation-id eipalloc-12345678
    
  • Disassociate an Elastic IP Address

      aws ec2 disassociate-address --association-id eipassoc-12345678
    
  • Release an Elastic IP Address

      aws ec2 release-address --allocation-id eipalloc-12345678
    

AMIs (Amazon Machine Images)

  • Create an AMI from an Instance

      aws ec2 create-image --instance-id i-1234567890abcdef0 --name "My server" --no-reboot
    
  • List AMIs

      aws ec2 describe-images --owners self
    
  • Deregister an AMI

      aws ec2 deregister-image --image-id ami-0abcdef1234567890
    

Volumes

  • Create a New Volume

      aws ec2 create-volume --size 10 --region us-west-2 --availability-zone us-west-2a --volume-type gp2
    
  • Attach a Volume to an Instance

      aws ec2 attach-volume --volume-id vol-12345678 --instance-id i-1234567890abcdef0 --device /dev/sdf
    
  • Detach a Volume from an Instance

      aws ec2 detach-volume --volume-id vol-12345678
    
  • Delete a Volume

      aws ec2 delete-volume --volume-id vol-12345678
    

Snapshots

  • Create a Snapshot of a Volume

      aws ec2 create-snapshot --volume-id vol-12345678 --description "My snapshot"
    
  • List Snapshots

      aws ec2 describe-snapshots --owner-ids self
    
  • Delete a Snapshot

      aws ec2 delete-snapshot --snapshot-id snap-12345678
    

VPC (Virtual Private Cloud)

  • Create a VPC

      aws ec2 create-vpc --cidr-block 10.0.0.0/16
    
  • List VPCs

      aws ec2 describe-vpcs
    
  • Delete a VPC

      aws ec2 delete-vpc --vpc-id vpc-12345678
    

Useful Options

  • Specify a Region

      aws ec2 describe-instances --region us-west-2
    

This cheat sheet provides a quick reference to the most commonly used AWS EC2 commands. For more detailed information and additional commands, refer to the official AWS CLI documentation.

Did you find this article valuable?

Support Cloud Tuned by becoming a sponsor. Any amount is appreciated!