AWS Command Line Interface (CLI)

Task Description

  1. Create a key pair

AWS CLI

The AWS Command Line Interface (CLI) is a tool to manage your AWS services. We can control multiple AWS services from the command line and automate them through scripts.

To use AWS CLI, we need to download and install it on our system. AWS CLI is available in two versions :

  • Version 2.x

For our practical we are going to use Version 2. After installing we can check its version to verify it is successfully installed or not.

As we have successfully installed aws-cli software now, we can use all the services with just single command. But before this we have to configure the settings that the AWS Command Line Interface (AWS CLI) uses to interact with AWS.

We can use aws configure command to setup the AWS CLI. We need to provide Access key and Secret key of IAM user for authentication also we have to mention the region name and output format. For this practical I am using mumbai region(ap-south-1).

We don’t need to remember the commands we can always refer to the help. For example we can list all the options available for EC2 with help.

We can easily see all the commands available for EC2. Same approach I will follow for all the steps.

Step 1: Create a key pair

First collect the information required for creating the key pair.

Name: clikey

Here we only need to specify the name of the key. Now we can create the command for creating key pair.

aws ec2 create-key-pair

— key-name clikey

Step 2: Create a security group

Collect the information required for creating the Security group.

Name: cli-sg
Description: CLI security group
VPC ID: vpc-61829f09

Command for creating security group.

aws ec2 create-security-group

— group-name cli-sg — description “CLI security group”

— vpc-id vpc-61829f09

Now we need to add inbound rules.

Inbound rules:
Type: SSH
Protocol: TCP
Port range: 22
Source: 0.0.0.0/0

Command for adding inbound rules.

aws ec2 authorize-security-group-ingress

— group-name cli-sg

— protocol tcp

— port 22

— cidr 0.0.0.0/0

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

Collect the information required to launch an instance.

AMI: ami-0e306788ff2473ccb
Instance type: t2.micro
Count: 1
Subnet: subnet-0a420fc4eb459022a
Tags:
Name: CLI-Instance
Security Group: sg-0ef10e78b6ca565cd
Tags:
Name: CLI-Instance
Key pair: clikey

Command to launch an instance.

aws ec2 run-instances

— image-id ami-0e306788ff2473ccb

— instance-type t2.micro

— key-name clikey

— security-group-ids sg-0ef10e78b6ca565cd

— subnet-id subnet-0a420fc4eb459022a

— count 1

— tag-specifications=ResourceType=instance,Tags=[{Key=Name,Value=CLI-Instance}]

Step 4: Create an EBS volume of 1 GiB.

Collect the information required to create EBS volume of 1Gib.

Volume Type: General Purpose SSD(gp2)
Size: 1GiB
Availability Zone: ap-south-1a
Tag:
Name: ebs-cli

Command to create EBS Volume.

aws ec2 create-volume

— availability-zone ap-south-1a

— size 1

— volume-type gp2

— tag-specifications=ResourceType=volume,Tags=[{Key=Name,Value=EBS-CLI}]

Step 5: Attach the above created EBS volume to the instance.

Collect the information required to attach the EBS volume to the instance.

volume Id: vol-055341a7f8c5f9142
Instance: i-0d90e5539f15f7097
Device: /dev/sdf

Command to attach the EBS volume to the instance.

aws ec2 attach-volume

— volume-id vol-055341a7f8c5f9142

— instance-id i-0d90e5539f15f7097

— device /dev/sdf

We can also verify this by checking the disks attached to the instance.

THANK YOU !!