Launch EC2 Instance using AWS CLI

What is AWS CLI ?

Let’s start….

Now, we need to download and install AWS CLI tool in our OS. I will use Version 2.1.17

Before starting, We have to create IAM user to get Access key and Secret key. So, we have to go to our AWS account from AWS Web Console. Then go to “IAM” service then click on “Users” then click on “Add User” and create one user. Then click on “Programmatic access” and then it’ll be giving us “Access key” and “Secret key”.

Now, we have to run “aws configure” command to setup AWS CLI. And we have to give it Access key and Secret key of IAM user (in my case “Sunil” IAM user), and mention region name and output name.

Now, we can start and if we don’t know command then we can take help in our cmd.

Step-1 :- Create key pair

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

You can choose any name for key pair.

Step-2 :- Create security group

aws ec2 create-security-group --group-name <name of security group> --description "<write anything>" --vpc-id <your vpc id>

You can choose any name for security group name. You can find your VPC id here : — go to “Networking & Content Delivery” service then go to “VPC” and then go to “Your VPCs”. Here you will find your VPC id.

Now, we have to add inbound rule.

aws ec2 authorize-security-group-ingress --group-name <security group name> --protocol tcp --port 22 --cidr

Step-3 :- Launch an Instance.

aws ec2 run-instances --image-id ami-038f1ca1bd58a5790 --instance-type t2.micro --key-name <key name> --security-group-ids <security group id> --subnet-id <subnet id> --count 1 --tag-specifications=ResourceType=instance,Tags=[{Key=<key name>,Value=<value>}]

Our instance is launched. Now, we’ll create EBS volume of 1GiB.

Step-4 :- Create EBS volume of 1GiB.

aws ec2 create-volume --volume-type gp2 --size 1 --availability-zone <instance zone> --tag-specifications=ResourceType=volume,Tags=[{Key=<key name>,Value=<value>}]

Step-5 :- Attach the above EBS volume to our instance.

aws ec2 attach-volume  --volume-id <volume id>  --instance-id <instance id>  --device /dev/sdf

Verify : -

Step-1: Key pair is created.

Step-2: Security group is created.

Step-3: Instance is created and in running state.

Step-4: EBS volume of 1GiB is created and currently is in used (mounted on instance).

Step-5: EBS volume is attached to our instance.

Thank you for reading….