Launch EC2 Instance — AWS CLI

Sakshi Arya
6 min readJan 9, 2021

Hey Reader 👋🤗

Today, my blog is on AWS CLI💻. So now, let’s see what AWS CLI is?

🔰 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.

AWS CLI is more powerful than the Web UI because by using Web UI we can’t do so much configuration according to our need. Let’s consider a scenario, so If we want to launch two instances of the same AMI (iso image) but in different subnets together. Then, it’s not possible by using Web UI but we can do many configurations like this by using AWS CLI.✨

Today, we do the following tasks with the help of AWS CLI:-

🔑Create a Key Pair

🔑Create a Security Group

🔑Launch an Instance by using the above created key pair and security group

🔑Create an EBS volume of 1 GB

🔑Attach the EBS with the above launched instance.

But before this we have to install aws cli software in our system. We can install aws cli through this link:- aws cli download according to our Operating System.😁 But here I’m downloading AWS CLI for Windows.

For checking whether the aws cli is installed or not. So for checking we have to use the below command 👇

Now before doing our task we have to login to our aws account by using below command.👇

But here we see that we need an Access key and Secret Access key for login to AWS Account in CLI . And we get these keys when we create an IAM user . So, firstly we have to create an IAM user.

For creating IAM User we have to follow the steps mentioned below:-👇

Step 1 :- Select IAM service .

Step 2 :- Click on Users.

Step 3 : Click on Add User.

Step 4 : Give the name of IAM User.

Step 5 : Attach Policies.

Step 6 : Click Next.

Step 7 : Click on Create User.

Now , IAM User is successfully created😍. And now we can use access key id and secret access key id to login to AWS Account through CLI.

Now, we copy these keys and paste in CLI.

Hurray, we successfully login to our AWS Account by using CLI.🤩

Now, we start our task.😎🤝

🎯 Create a key pair.

For creating key pair we have to use the below command👇. And we have to give a name to our key pair. So , here I’m giving my key pair name as “CLI_KEY”

aws ec2 create-key-pair -–key-name [KEY_NAME]

And we can also check from the Web UI that our key is created or not.

Here, we see that our Key Pair is successfully created.😎

But if we want to see the details of our key pair. What should we do ?🤔

So we have to use the below command to check the details of a key pair.👇

aws ec2 describe-key-pairs --key-name [KEY_NAME]

🎯Now, we have to create a Security Group. 🔐 Here, I’m giving my security group name as “MyGroup” and giving description as “Security Group From CLI”

aws ec2 create-security-group --group-name [GROUP_NAME] --description [GROUP_DESCRIPTION]

And, we use the below command to check the details of security group.👇

aws ec2 describe-security-groups --group-id [GROUP_ID]

We can give inbound /ingress rule of Security Group by using below command:-👇

aws ec2 authorize-security-group-ingress  --group-name [GROUP_NAME ]   --protocol  all --cidr 0.0.0.0/0

Now , we check in the Web UI whether the security group is created or not.

🎯Now, we launch an Instance by using the above created key pair and security group.

We have to use the below👇 command to launch an Instance. Here, I’m using Amazon Linux 2 AMI to launch my Instance.

aws ec2 run-instances  --image-id ami-0e306788ff2473ccb   --count 1 --subnet-id subnet-f63a339e  --instance-type t2.micro --key-name [KEY_NAME]  --security-group-ids [SECURITY_GROUP_ID]

To check details of existing instance we use the command mentioned below.👇

aws ec2  describe-instance-status --instance-ids [INSTANCE_ID]

Now, we check from the Web UI.

We successfully launched our ec2 instance.🤘😍

🎯Now we have to create an EBS volume of 1 GB

And, for creating EBS Volume of 1 GB we have to use the below command.👇

aws ec2 create-volume --volume-type gp2 --size 1 --availability-zone ap-south-1a

Now, we are checking from Web UI.

And, for checking the details of EBS Volume we have to use the below command.👇

aws ec2 describe-volumes --volume-id [VOLUME_ID]

🎯Attach the EBS Volume with the above launched instance.

And for attaching EBS Volume with Instance then we have to use the below command.👇

aws ec2 attach-volume --volume-id [VOLUME_ID] --instance-id  [INSTANCE_ID] --device /dev/sdf

We can check from the Web UI that our EBS Volume connected to which instance.

Here, we see that our EBS Volume is connected to the instance successfully.🤘😍

🎯Stop the instance.

We use the below command to stop the instance.👇

aws ec2 stop-instances --instance-ids [INSTANCE_ID]

Our instance is successfully stopped.🚫

Hurray, we successfully completed our task.🤩🤝

Lastly, I want to thank Mr. Vimal Daga sir and Preeti Chandak mam because of their hard work today I’m able to complete this task.😇💙

And thanks to you for reading my article.🖤

--

--