AWS CLI Management

Hey everyone, Welcome to the Blog on Managing the AWS console using the Command Line Interface.
In this blog, we are going to do the below tasks using AWS CLI.
- Create a Key Pair
- Create a security group
- Launch EC2 instances using newly created Key pair and security group.
- Create EBS volume of 1GB
- Attach the EBS Volume to the newly launched instance.
Before proceeding further, check whether aws is installed or not(use aws — version). If it is not installed, refer to https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html
After the installation, type help along with aws
aws help
“help” is used to know all the commands that are available in AWS. You can also use “help” with sub commands to know the different options available in sub commands.
CREATING A KEY PAIR

You know that keypair comes under ec2 service in AWS so you should use help after the ec2. You will find create-key-pair option. And after knowing create-key-pair, again use help with create-key-pair to know the different options available.In my case I always use grep command to make it simpler ;). Here I am going to create a key-pair and giving it a name “myclikeypair” .
aws ec2 create-key-pair --key-name myclikeypair
This will generate a Keypair. You can check whether the keypair is created or not using AWS Console(GUI) or CLI.

CREATING A SECURITY GROUP
For creating security group, you should know that the security groups are like the firewalls that protects our instances. Security Groups comes under EC2 service. So you know that you need to use help after aws ec2 in the command line. From the options available, you will see create-security-group. And then again use help to know all the options available. You will see — group-name this is for creating a name for our security group and it comes with — description tag for the description you want to give. And this will give our Security Group ID as output. Refer it when you want to launch any instance using this security group.
aws ec2 create-security-group — group-name "MyCLISeGroup — description "My CLI Security Group"

Launching instance using new keypair & security group
You know that EC2 is the service for launching instances, use “aws ec2 help” and there you will see the option run-instances (This option is for creating new instance.) aws ec2 run-instances help will help you to get all the options that are available for launching instance. You will see — image-id , — count, — instance-type, — key-name, — security-group-ids. These are all the necessary options for launching our instance. You can use these options in any order. Use GUI/CLI to get image-id, security-group-ids, instance-type
aws ec2 run-instances --image-id ami-0e306788ff2473ccb --count 1 --instance-type t2.micro --key-name myclikeypair --security-group-ids sg-0f05c47cdf13c6a2e

Creating EBS Volume of 1GB
create-volume option in aws ec2 will help us to create volumes. Under this options you will see — availability-zone, — size, — volume-type. So our command would be :
aws ec2 create-volume --size 1 --volume-type gp2 --availability-zone ap-south-1b


Attaching EBS to Instance
For attaching EBS volume to the instance, your required sub command is attach-volume and options that are necessary under attach-volume are — instance-id, — volume-id, — device. You can always use help command to know this.
aws ec2 attach-volume --instance-id i-02ef6a3c944dd729d --volume-id vol-090b525e6df33adcf--device /dev/sdh

You can also verify the EBS using AWS Management Console. You will see the instance to which our volume is attached under Attachment Option in the console.

Thanks for reading my blog.
This blog is a part of my journey in ARTH — The School of Technologies, guided by the World record holder Mr. Vimal Daga sir.