Getting Started with AWS CLI : Part — 1
Beginner’s Guide To AWS CLI
Hey everyone,
In this article, I will gonna show you How you can manage AWS resources and services using AWS CLI. And for showing the working of AWS CLI, I will show you a simple practical in which we launch an EC2 instance and create an EBS volume and then attach the EBS volume with the instance.
Basically for using the services of any Cloud like AWS, GCP, Azure there are typically 3 ways that are
- Through the web portal of the cloud provider i.e., website (GUI)
- Through CLI i.e., the command line tool provided by the cloud provider
- Using SDK like by writing the code i.e., using some infrastructure as code tool like Terraform.
To know more about Terraform, you can check my previous article.
What is AWS CLI ?
The AWS Command Line Interface (AWS CLI) is an open-source tool that enables you to interact with AWS services using commands in your command-line shell. With minimal configuration, the AWS CLI enables you to start running commands that implement functionality equivalent to that provided by the browser-based AWS Management Console from the command prompt in your terminal program.
The AWS CLI provides direct access to the public APIs of AWS services. You can explore a service’s capabilities with the AWS CLI, and develop shell scripts to manage your resources. In addition to the low-level, API-equivalent commands, several AWS services provide customizations for the AWS CLI. Customizations can include higher-level commands that simplify using a service with a complex API.
For more information about AWS CLI, you can go here.
Let’s first start with the installation part of the AWS CLI.
Installation of AWS CLI
AWS CLI has 2 versions that are version 1 and version 2. And in this, we will use version 2.
We can install AWS CLI in Windows, Linux, and Mac but here we are installing it in windows. So first we have to download the AWS CLI installer which you can download from here. And after downloading just run it, it will automatically install the CLI tool and also updates the environmental variables in windows.
Now to confirm whether it is installed or not open the windows command prompt or PowerShell and type the command below.
aws --version
Configure AWS CLI.
For using the AWS CLI, we first have to configure our AWS account where we will launch the services and for this, we need an AWS IAM account although we can also configure our root AWS account but it is not good to configure root account as it has all the powers. So here we are configuring an AWS IAM account with some specific powers, you can give any power for the services you want to use or for all services give PowerUser Access.
To configure we need the secret key and access key of the IAM user. And for configuration, we have to use this command.
aws configure
We can also configure multiple profiles by adding — -profile
option in the above command like this
aws configure --profile (profile_name)
Now we can easily use AWS services using AWS CLI.
So let we first launch an EC2 instance through CLI. Using the AWS CLI is very simple, we just need to know the right options and sub-options for the service we want to use. Like here we want to launch an EC2 instance then we need the option/command related to the EC2 service and we can see all the options available in the aws command using help like this
aws help
So we don't need to learn any aws command we can take help every time we want to use some service. And to see the additional options for some specific service we can again use help like this
aws ec2 help
So in this way, we can use AWS CLI and write commands for any service of AWS.
aws help aws <command> help aws <command> <subcommand> help
Launch EC2 Instance
Now to launch an EC2 instance we can write the command like this
aws ec2 run-instances --image-id ami-08f63db601b82ff5f --instance-type t2.micro --key-name taskoskey
So it will launch an EC2 instance in the default VPC and also creates a security group for the instance.
In the above command, we can add more options like subnet id, security group, etc according to the need.
Create EBS Volume
Let’s create an EBS volume for our instance using the AWS CLI and the command for doing this is
aws ec2 create-volume --availability-zone ap-south-1b --size 1
The above command will create a volume of 1GB and we create the volume in the same availability zone in which our instance is running so that we can able to attach it.
Attach EBS Volume with the EC2 Instance
Now we have to attach the above created EBS volume with our EC2 instance launched above. And for doing this we just need to write this simple command
aws ec2 attach-volume --volume-id vol-033d739deb2afbc44 --instance-id i-07b51884e6908f67c --device /dev/sdf
Now the EBS volume has been attached to the EC2 instance and we can also verify from the web console of AWS.
The practical I have shown you is so easy, it’s not a big deal to do this. But here the important thing is how to do with CLI.
So that's how we can use AWS CLI to do anything on AWS Cloud. CLI is a good way to implement automation but for Clouds, the best approach is to use some Infrastructure as Code (IAC) tool like Terraform.
You can check my previous articles about Terraform on medium.
Thanks for Reading !!!
Connect with me on LinkedIn.