EC2 instance

This post describes the steps to create an EC2 instance in AWS using command line interface(CLI).

  • Describe an existing security group (else, create one)
    aws ec2 describe-security-groups
    
  • Get the configured region
    aws configure get region
    
  • Create a Key Pair for EC2
    aws ec2 create-key-pair help
    aws ec2 create-key-pair --key-name MyKeyPair --query 'KeyMaterial' --output text > MyKeyPair.pem
    aws ec2 describe-key-pairs --key-name MyKeyPair
    
  • Modify accessibility of Key pair to read only for owner
    chmod 400 MyKeyPair.pem
    ls -ltr
    
  • Authorize the security group for SSH access
    aws ec2 authorize-security-group-ingress --group-id sg-91a26ee0 --protocol tcp --port 22 --cidr 0.0.0.0/0 --region us-east-1
    
  • Create EC2 instance
    aws ec2 run-instances --image-id ami-0b542c1d --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-91a26ee0 --region us-east-1\n
    
  • SSH into the new EC2 using Key pair created
    ssh -i MyKeyPair.pem ubuntu@ec2-ipaddress.compute-1.amazonaws.com
    
  • Exit from the EC2 instance
    exit 
    
  • Stop the instance
    aws ec2 describe-instances
    aws ec2 stop-instances --instance-ids <instance id>
    
Written on June 19, 2017