AWS CLI

AWS-9

What is AWS CLI ? How to use AWS CLI ?

Launch EC2 instance, create EBS and attach EBS volume to EC2 instance using aws CLI.

Introduction 🤓

AWS is a market leader and top innovator in the field of Cloud Computing. AWS has integrated building blocks that support any application architecture, regardless of scale, load, or complexity.
But AWS is more than beautiful and eye-catching AWS Web UI console. Let's discover the Amazon’s Command Line Interface — AWS CLI.

What is AWS CLI? ❤️

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.

We can achieve more speed and customization through AWS CLI than the AWS management console. It improves the convenience and productivity of DevOps engineers and developers.

Installing AWS CLI ‍💻

There are two versions of AWS CLI.

  • Version 1.x
    The previous version of the AWS CLI is available for backward compatibility.
  • Version 2.x
    It is the current version. It has more features than version 1

To install AWS CLI, there are multiple ways you can check that here. (Open). But I will show my favorite way to download and install AWS CLI.

#install awscli using pip tool
$ pip install awscli --upgrade --user
#check installation
$ aws --version

Note: If you don't have a pip tool then download get-pip.py from https://bootstrap.pypa.io/. and run this file using python.
$python get-pip.py
for more details (open)

Configure AWS CLI 🚀

1. Create an IAM user that has an EC2 creation policy.

I recommend you to create PowerAccessPolicy for Practice.

For more details, you can refer to this: How to create IAM user

2. You will get ACCESS KEY and SECRET KEY using that you can now configure to aws on CLI.

aws configure — profile “name”

How to use AWS CLI? 🧐

Using CLI you can achieve almost everything that you can do using AWS management console but with less efforts and clicks.

Lets try something using AWS 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 of 1 GB.
🔅 The final step is to attach the above created EBS volume to the instance you created in the previous steps.

Mostly I use the help command instead of remembering commands.
AWS CLI documentation is informative and well described with examples.

$ aws help

Create Key Pair

To create keypair we have to use ec2 service of AWS. by using
$ aws ec2 help
you will see all the subcommands under ec2.

To create key pair and save it in proper format(.pem), use below command.

Note: Windows users use PowerShell for above commands. To activate PowerShell on normal cmd. Enter $ PowerShell command. OR Win + R and enter PowerShell to open Powershell.

Create a Security Group

You may want to create a security group in a specific VPC. For that, we need vpc id. Let's find VPC id using AWS CLI.

above command will return the list of VPC’s and tags associated with VPC.
copy the vpc id you want to create SG in and save it somewhere.
Now lets create security group.

Dont forget to edit the command according to your need. You can change name, description and vpc id.

Create security group rules

we want to add rules in the security group that we have created above.
If you copy-paste the security group id then skip the below command..
Lets find the security group name and id.

the above command will give you a list of security group names and ids.
copy the id that we want to add new rule and paste it in below command.

the above command will create a new rule for ssh.. you can always customize as per your need.. you know that right?
You can check more examples for multiple rules here..

Check that rule added successfully or not using describe subcommand.

Create a new instance

You may want to launch an instance in a specific availability zone. let's see with AZ are available in the region you specified while configuring.

We want to attach one more ebs volume to our instance and for that both should in same availability zone. Fix your availability zone for that.
AWS CLI has very well described documentation for ec2.. lets take help of that. It will give more examples and necessary information. You always need something different than I used here.. so go and use
$ aws ec2 help

To launch an instance we need ami id of the image that we want to launch..

$ aws ec2 describe-images

I have used the key pair and security group that we have created above. I choose the ap-south-1a availability zone and amazon Linux ami to launch this instance.
You can to much more customization to this command. Check this aws Documentation.(open)
copy instance id and save it somewhere.. we will see later where we need.

Create EBS volume

Our goal is to create a new volume and attach to an instance that we have just created. So we need to create it in the same Availability Zone.

Update the above command as per your need. (size in GiBs).
copy volume id and save it somewhere.

Attach EBS volume to instance.

update the instance id and volume id in the above command that we have copied and save.. remember?

That's all, you may find that lots of commands we used. but you can create one script combining all the commands. You can modify and run whenever it is necessary.
If you are already using AWS using WebUI. I recommend you to give to try to AWS CLI. You will notice the time you save by using CLI.

Wrapping Up 🥳

So far we have created a new key pair, security group, add ssh rule into it, provision new instance, create new ebs volume and attach to the instance.
but… you can do much more than this.
Tell me did you find this article helpful and will you use aws cli tool?
in the comment section below.
Thank you..

DevOps Engineer at Mindbowser Inc| www.shubhamrasal.me

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