AWS using CLI

Using CLI to access AWS

So, what is AWS?

AWS (Amazon Web Services) is a comprehensive, evolving cloud computing platform provided by Amazon that includes a mixture of infrastructure as a service (IaaS), platform as a service (PaaS) and packaged software as a service (SaaS) offerings. AWS services can offer an organization tools such as compute power, database storage and content delivery services.

AWS launched in 2006 from the internal infrastructure that Amazon.com built to handle its online retail operations. AWS was one of the first companies to introduce a pay-as-you-go cloud computing model that scales to provide users with compute, storage or throughput as needed.

AWS offers many different tools and solutions for enterprises and software developers that can be used in data centers in up to 190 countries. Groups such as government agencies, education institutions, nonprofits and private organizations can use AWS services.

AWS can be accessed in 3 ways:

  1. Using WEB UI(Web Interface (Console)).
  2. Automation (Application Programming Interface).
  3. Using CLI (Command Line Interface).

AWS USING COMMAND LINE INTERFACE:

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.

The AWS CLI v2 offers several new features including improved installers, new configuration options such as AWS Single Sign-On (SSO), and various interactive features. 


Why use CLI when WEBUI is available?

In the WEB UI, everything needs to be done manually and the CLI is mostly used to automate the things and make things happen faster or agile. Even in the companies use CLI to do the tasks.


If we want to launch 5 instances, we need to launch instances for 5 times whereas in CLI, we can launch them at a time. 


To access AWS from CLI, we need to install AWS CLI 2.


By clicking on the For the latest version of AWS CLI:https://awscli.amazonaws.com/AWSCLIV2.msi

we can download the file and then install it.

Then go to Command prompt using Win+R and type aws --version to make sure the AWS CLI is installed.


In order to access the AWS services from the CLI, we need to set-up an IAM user with appropriate permissions.

STEP 1:From the AWS dashboard, search for the “IAM” service as shown below, and select it.

STEP 2: Now, you will be seeing something like shown below, from there select the “Add user” icon under the “Users”.

STEP 3: On this screen, provide the details for the IAM user:

  • User name =AWS_CLI
  • Access type = Programmatic access



STEP 4: Set Permissions 

Go to Attach Existing Policies Directly.

Now select  POWER USER ACCESS

Create User

We did not create any security group or any key pair while creating this IAM user because  want to create them using CLI.

STEP 5: Download “credentials.csv”

On this screen, you will be only prompted to download the “credentials.csv” file only once, so make sure you download them to your computer at this point.


Go to Cmd and type aws configure :
Enter the details from the file we downloaded as follows:

Now, we are done with the configuration/login part.
The use the command to describe instances as:
aws ec2 describe-instances


Creating a new Key-Pair in our AWS account?


We can get help with any command when using the AWS Command Line Interface (AWS CLI). To do so, simply type help at the end of a command name.

For example, by typing aws ec2 help, it provides all the information or data about ec2.

Now type aws ec2 create-key-pair help



Then type aws ec2 create-key-pair --key-name <value>
Here, I created a keypair with the name 'keyaws' , so I type the command as:
aws ec2 create-key-pair --key-name keyaws



Go to the management console of the AWS to check if the keypair is created.



As we see, a keypair named keyaws is present which means the keypair is successfully created using CLI.

Creating a security group in our AWS using CLI:


Type aws ec2 create-security-group help


Then type aws ec2 create-security-group --group-name <value> --description Help
Here, I am creating security group name with 'my_security'
So I type the command as :
aws ec2 create-security-group --group-name my_security --description Help



Now go to management console of AWS to make sure the security is created.


Here, we can see a security group is created with the name my_security.

Creating an EC2 instance from above created Security Group and Key pair using CLI:


 The above created keypair and security will be used for launching of the EC2 instance.



Now, go to the AWS management console to check if the Instance is created.

The Instance with the name '-' is the one which we created using CLI.

Creating an EBS volume from CLI:


Type aws ec2 create-volume --volume-type gp2 --size <value>  --availability-zone <value.
Here, I created a size of 1gb and zone is ap-south-1b
So, I type the command as
aws ec2 create-volume --volume-type gp2 --size  1 --availability-zone ap-south-1b


Go the AWS management console and check if the EBS volume is created.


Here, we can see a volume of 1gb is created.

Attaching the EBS volume we created to the instance we launched from CLI:


Now, we need to attach the volume to the ec2 instance.
 
Type aws ec2 attach-volume --volume-id <value> --instance-id <value> --device <value>


Here, we can see that the volume is attached to the instance.
Now, go to the AWS management console to check if this EBS volume is connected to the instance.

We can clearly see that the Volume is being attached to the instance.

Detaching the EBS volume from CLI:

aws ec2 detach-volume --volume-id <value> will detach the volume from the instance.

Terminating the AWS instance using CLI:

aws ec2 terminate-instances --instance-id <value> will terminate the instance using CLI.

Comments

Popular posts from this blog

AWS SQS