Working with AWS CLI

Deepak Patra
6 min readOct 18, 2020

The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.

TASK DESCRIPTION:

1.Create a key pair

2. Create a security group

3. Launch an instance using the above created key pair and security group.

4. Create an EBS volume of 1 GB.

5. The final step is to attach the above created EBS volume to the instance you created in the previous steps.

In order to work with AWS CLI we need to first download and install the AWS CLI software.

Steps to download:

► Search on Google → “AWS CLI install” and click on the link.

►I am downloading AWS CLI software for my windows, you can use the software for other OS too.

► we can directly download from the link

https://awscli.amazonaws.com/AWSCLIV2-2.0.30.msi

► The installation is quite simple . By double click we can simply install the software. We can check in windows command prompt whether it is installed or not by typing the command:

aws --version

►Well, installation has been completed and we can move to AWS CLI.

► But before moving to CLI, we need to have an AWS account created in order to create am IAM user. Authentication of an user account is required in order to work with CLI.

► I have already created an IAM user successfully.

► After Signing into the root user and creating the IAM user successfully, we can use the user credentials like access key, secret key and region for authentication in AWS CLI by using the command:

aws configure

Hence, authentication is complete and we can head for the tasks.

» How to create a key pair?

► Before creating a new keypair let’s check how many keypair i have in my AWS Account in GUI.

There’s only one key pair now.

As we can see there is only one key pair present in my account which I had done before using the GUI interface .Now let’s create a new keypair by using CLI and see a new keypair is being created or not.

► We can find our approach by using the help command.

Command to create key pair aws ec2 create-key-pair --key-name <value>

aws ec2 create-key-pair --key-name awscli

► Now let’s check in GUI whether a new key pair named awscli is being created or not.

We can see that a new key pair named awscli is being added to the list that we have been created through CLI.

» How to create a security group?

A security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic. Inbound rules control the incoming traffic to your instance, and outbound rules control the outgoing traffic from your instance. When you launch an instance, you can specify one or more security groups. If you don’t specify a security group, Amazon EC2 uses the default security group.

► Before creating a security group lets check how many security groups i have at present.

► So, there are 6 security groups at present.

Command to create security group → aws ec2 create-security-group --group-name <value> --description <value>

aws ec2 create-security-group --description arth --group-name awscli-1

► So, finally a new security group is created with the provided group name and description name.

► We can check/ describe security group we created in AWS CLI by the command:

aws ec2 describe-security-groups --group-ids sg-08214b2e0de4c0a27

» How to launch an instance using the above created key pair and security group ?

To launch new instances in CLI the command is :

aws ec2 run-instances --image-id <value> --instance-type <value> --count 1 --subnet-id subnet <value> --security-group-ids <value> --key-name <value>

aws ec2 run-instances --image-id ami-0e306788ff2473ccb --security-group-ids sg-08214b2e0de4c0a27 --instance-type t2.micro --count 1 --key-name awscli

► We can also check in GUI :

Hence, the instance is created.

► Command to describe instance:

aws ec2 describe-instances --instance-ids i-00506d0586c573ba2

» How to create an EBS volume of 1 GB :

► To create an EBS Volume the command is : aws ec2 create-volume --volume-type <value> --size <value> --availability-zone <value>

aws ec2 create-volume --volume-type gp2 --size 1 --availability-zone ap-south-1b

As my instance is launched in ap-south-1b, I take the same availibility zone for EBS volume.

► We can see a new EBS volume of size 1GB created.

» How to attach the above created EBS volume of size 1GB to the AWS EC2 Instance :-

► Command to attach the ebs volume is : aws ec2 attach-volume --instance-id <value> --volume-id <value>--device <value>

aws ec2 attach-volume --volume-id vol-04bca24d064002666 --instance-id i-00506d0586c573ba2 --device /dev/xvdf

► Before running the command, we can see there is just 1 block device present named /dev/xvda.

► Attaching the EBS volume by the command:

aws ec2 attach-volume --volume-id <value> --instance-id <value> --device <value>

► We can see the new block device is attached.

► So, our requirements are fulfilled and now we can detach the volume and stop our instance.

► Command to detach my EBS volume:

aws ec2 detach-volume --instance-id i-00506d0586c573ba2 --volume-id vol-04bca24d064002666 --device /dev/xvdf

► EBS volume has been detached Successfully.

►Command to stop my instance:

aws ec2 stop-instances --instance-id i-00506d0586c573ba2

► Finally, our instance has been stopped successfully.

--

--