Launch EC2 instance using AWS CLI

Launch a ec2 instance and add security groups and EBS using cli.

  • Create a key pair
  • Create a security group
  • Launch an instance using the above created key pair and security group
  • Create an EBS volume
  • The final step is to attach the above created EBS volume to the instance you created in the previous steps
  • AWS CLI should be installed in the OS
  • AWS CLI should be configured in the OS

Lets start…..

Configure the AWS user Profile in Aws Cli.

Profiles saved in AWS cli.
$ aws configure --profile <profile name>
Command to show profiles
  • This is my EC2 dashboard summary. In this you can see I have a single key-pair and security groups. Lets come to our topic now create a key-pair.

Now create a Key-pair

$ aws ec2 create-key-pair --key-name <keynmae>
this command is only for creating key-pair
$ aws ec2 create-key-pair --key-name <keynmae> --query KeyMaterial
--output text > filename.pem
this command is for saving data in a file.

Now Its time to create a Security Group.

$ aws ec2 describe-security-groups
Command to describe all security groups we have.
$ aws ec2 create-security-group --group-name <group name> --description <" any thing">
Command to create a SG
$ aws ec2 authorize-security-group-ingress --group-name <group name> --cidr 0.0.0.0/0
Command for adding inbound rule in SG

Lets Launch A EC2 Instance.

$ aws ec2 run-instances --image-id ami-06a0b4e3b7eb7a300 --count 1 --instance-type t2.micro --key-name tridha_keypair --security-group-ids sg-071b2cac2055d34af --subnet-id subnet-26b8494dCommand to launch a instance on AWS.

Create an EBS volume

$ aws ec2 create-volume --volume-type gp2  --size 2 --availability-zone ap-south-1a
command to create a EBS vloume
$ aws ec2 attach-volume --instance-id i-070f999452b4c9f58 --volume-id vol-080126e0751decde3 --device /dev/sdfCommand to attach volume to instance

For deleting all the SETUP

$ aws ec2 detach-volume --instance-id <id-> --volume-id <vol-id>
$ aws ec2 delete-volume --volume-id <vol-id>
$ aws ec2 terminate-instances --instance-id <id->
$ aws ec2 delete-security-group --group-name <SG_NAME>
$ aws ec2 delete-key-pair --key-name <key_name>

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store