Using AWS CLI(command line interface)

Harshil Shah
5 min readOct 17, 2020

What will be covered in this article:

β†’ Prerequisites

β†’ Will create a key pair .

β†’ Create a security group.

β†’ Launch an instance using the above created key pair and security group.

β†’ Create an EBS (Elastic Block Storage) volume of 1 GB.

β†’ Lastly, attach the above created EBS volume to the instance created in above steps.

Note: All of the above steps can be achieved via gui as well. But the main reason for doing all this using cli is that we get more flexibility.

Prerequisites

πŸ‘‰Before we start using cli to launch instances and create key pair and all, we need to first install aws cli to our laptop. Below is the link from where you can download aws cli v-2

πŸ‘‰ link to download : AWSCLIV2.msi

πŸ‘‰ Once downloaded you can check the version of aws installed :

aws ~~version

check version of AWS cli installed

πŸ‘‰Now you need to log in to aws IAM user account and than we can implement any of the above mentioned steps.

πŸ‘‰ In order to use AWScli, while creating IAM user we need to check the box that says Programmatic access. This will allow us to use AWS services for this following IAM user through cli.

Creating IAM user

πŸ‘‰ In the next step attach following policy .

Attach policy to give access to use resources.

πŸ‘‰ Once user is created, you will find this page where you will be given access key and secret access key. This are very important and are not supposed to be given to anyone as with this two key, we can login to IAM user account through cli.

IAM user account’s access key and private key (very crucial)

πŸ‘‰ Now from the command line we can login to the IAM user account we created. For that we use following command

β€˜aws configure’

logged in to IAM user account via cli

πŸ‘‰Now you can go on with bellow steps .

βœ” Step : 1

Create key pair

Here in the image, the command highlighted in yellow is used to create a new key pair. The key here is not saved to your local device and if you want to do ssh login, than you need private key pair. So we will copy block of code from β€œkeyMaterial”, that is inside red bracket, and save that file as .pem file . Than to use it to do remote login using putty, we will convert it to .ppk file.

Note: After copying KeyMaterial, we need to remove β€œ\n” wherever written and provide a new line as it is mentioning. Without doing this the key won’t be converted to .ppk file

β€œclikey-1” is the new key created

β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€”

βœ” Step : 2

create a security group.

We create or specify a security group so that two or more instances can communicate effectively and share information based on the inbound/outbound rules we set .

Here i have created security group named β€œcli-sg” and provided some description as in to identify what that security group is doing or for which instance it is attached to . The output here shows the groupId of the security group created.

β€œcli-sg” security group is created

β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€”

βœ” Step : 3

launching new instance

Things required for launching a new instance :

1. Image-id (ami image) : here we have used Red-hat linux image.

2. Count : count defines how many instances you want to launch at the same time

3. Instance type : here β€œt2.micro” for free tier users.

4. Key-name : here we use key name and not key-id . we have attached β€œclikey-1” that we previously created. A key can only be used in same region only but zone can be any.

5. Security Group Id : Id for the β€œcli-sg” security group we created before .

6. Subnet-id : subnet is important. As you need to specify which zone of which region you like to launch new instance. Here β€œsubnet-7022318” is subnet-id for mumbai region zone-a.

Launching a new instance.

β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€”

βœ”Step : 4

Create 1 GB size of EBS volume.

To create EBS volume we need three information.

  1. Volume-type : here we have used β€˜gp2’ volume type.

2. Size of Volume: Here 1 GB of EBS volume .

3. Availability zone : here we have created volume in β€œap-south-1a” i.e. Mumbai region zone-1a.

EBS volume created

β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€”

βœ” Step : 5

Attaching the volume to instance created before.

  1. While attaching the volume we have to provide volume’s device name . In this case β€œ/dev/sdf”
  2. Instance Id : Instance to which the volume is attaching. Here the instance we previously created
  3. Volume-Id : the id of the EBS volume we created before.
Attached the volume created in step:4 to the instance created in step:3

--

--