Using AWS CLI Tool to create some resources on AWS EC2 service

In this Blog, we will be creating:

🔅 Create a key pair
🔅 Create a security group
🔅 Launch an instance using the above created key pair and security group.
🔅 Create an EBS volume of 1 GB.
🔅 The final step is to attach the above created EBS volume to the instance you created in the previous steps.

This is a task given to me in my AWS Training.

To Start with this task, we need to Install AWS CLI on our System and then a IAM Role, which I have already created.

→ Installing AWS CLI Tool on our System:

  • We can Download the Package using the following URL:

You will see the following Page and from right hand side, you can click on your desired OS, and hence Install it:

  • To Install AWS CLI Tool:
AWS CLI Setup
AWS CLI Setup

Install this Software and then copy the “bin” Location of this installed Software, In my case it is :

Now, add a Environmental Variable for this copied Location in Path, by following the below Steps:

Click on Environment Variables :

Click on Path, Then Click Edit :

Click on new and then Paste the location here.

To verify if AWS CLI is installed properly or not, we can use the Command “aws — version”,

→ To create a IAM Role, you can refer to the below URL:

  1. We will create a Key Pair using this command — “aws ec2 create-key-pair — key-name “awscli” ”
Key Pair Created
Key Pair Created

2. We will create a Security Group

For creating a security group, we need a default VPC ID. Either we can see from GUI, or from CLI:

VPC ID from GUI

VPC ID is “vpc-224d514a”, we can see the similar output from CLI also:

VPC ID from CLI

To create Security Group, we use the following command:
“aws ec2 create-security-group — description “This is the Security Group for our Arth Task-5 “ — group-name “Arth-task5-sg” — vpc-id vpc-224d514a”

Security Group Created
Security Group Created
Description of Security Group

To connect to Instance, we must have to attach some rules to it, otherwise we can’t use this instance.

Initially, no rule is there

As you can see, we dont have any rules pre-written, so we can’t do SSH also. So following is the command to attach some rules to it, here I want only SSH which uses “tcp” protocol and port number “22”, CIDR Range i am giving as “0.0.0.0/0”, as I want that from any IP, I can connect to it.

Command is : “aws ec2 authorize-security-group-ingress — group-name Arth-task5-sg — protocol tcp — port 22 — cidr 0.0.0.0/0”

Rule added

After we run the above command, we can see that rule is added.

3. Creating a Instance:

We want key name, using following we can get the key names:

Key Pair Name

To create a Instance:

“ aws ec2 run-instances — key-name “awscli” — instance-type “t2.micro” — count 2 — security-group-ids sg-06ea64703f36ae12f — image-id ami-08e0ca9924195beba ”

Instance 1
Instance 2
Instances Created
Description of Instance 1(a)
Description of Instance 1(b)
Description of Instance 2(a)
Description of Instance 2(b)

4. Creating a EBS Volume of 1 GB

We can create a Volume using the command: “ aws ec2 create-volume — size 1 — availability-zone “ap-south-1a” ”

Volume created
1 GB EBS Volume Created

5. Attaching EBS Volume to one of the Instance:

We use the following command:- “aws ec2 attach-volume — device /dev/xvdb — instance-id i-0f5402d77697e3efb — volume-id vol-07d20ffccd5259816”

Volume Attached
Volume Attached

In this command, we have to give ID of the Volume and Id of the Instance, either we can fetch ID’s from GUI or from CLI.

CLI command to see the Volumes

From the above command, we can fetch the Volume’s Details and see that which volume is free and can be attached.
As this service is regional, so we can’t attach volume, which is being created in other regions, compared to the region in which Instance is created.
In my case, region is “ap-south-1b”.

→ Connecting to the instance:

Instance Connected

I have used “Putty” Software, from which I have done SSH.

Initially, when no volume is attached
Device attached

As we can see that disk is attached.

“I have practiced and gained all knowledge of this project(task) under the mentorship of Mr. VIMAL DAGA Sir during the AWS Training by Linux World India.”

I hope this article is Informative and Explanatory. Hope you like it !!!

For any suggestions or if any reader find any flaw in this article, please email me to “akhileshjain9221@gmail.com”

Thank You Readers, for viewing this !!!

I am a student and persuing under graduation in computer science and engineering.