Create AWS EC2 Instance using AWS CLI

Rahul Nath
4 min readFeb 17, 2021

What is AWS CLI?

AWS CLI gives you the ability to automate the entire process of controlling and managing AWS services through scripts. These scripts make it easy for users to fully automate cloud infrastructure. Prior to AWS CLI, users needed a dedicated CLI tool for just the EC2 service.

TASK DESCRIPTION:

  • Create a key pair
  • Create a security group
  • Launch an instance using the above created key pair and security group.
  • Create an EBS volume of 1 GB.
  • Attach the above created EBS volume to the instance you created in the previous steps.

Installation of AWS CLI

For installation of AWS CLI, you need to download the application from the below mentioned link and like we install any other application, just run the application and keep on clicking and it will be installed.

To download AWS CLI : https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html

installation of AWS CLI in windows

Check if it has installed properly using the commands :

aws

or

aws --version
checking if aws cli is properly installed or not.

Configuring AWS CLI

Before starting, we need AWS access key and secret key for configuration.

After logged in into the AWS account, go to IAM service

Then click on Users => Add User and create one user.

Then click on Programmatic access and if you read the description of this access you can see, it’s giving us “access key” and “secret key”.

Also, click on the AWS Management Console access =>which enables a password which allows users to sign-in to the AWS console, and in the console password, give your own custom password.

For reference follow the below mentioned screenshots… , and at last, click on Create user

Here, you can see the user has been created.

successful creation of user

NOTE : copy the Access key id and secret access key for the future use.

Now go to your command prompt, and do the steps as below :

Step 1 : Login to AWS Using security credentials of IAM User we created .

to log-in the AWS using CLI, we have the command :

aws configure

Step 2 : Create a key pair

For creating Key Pair we have command as :

aws ec2 create-key-pair --key-name your_key_name
key pair created.

Step 3 : Create Security group

To create Security group we have command as :

aws ec2 create-security-group --group-name group_name --description description
security group created.

Step 4 : Launch one EC2 Instance using above Key pair and Security Group

To launch ec2 instance AWS CLI we have command as :

aws ec2 run-instances --image-id your_image_id --instance-type your_instance --count no._of_counts --subnet-id subnet_id --security-group-ids your_security_group --key-name your_key
instance created.

Step 5 : Create one EBS Volume and attach it to the EC2 Instance we launched

To Create EBS Volume AWS CLI we have command as :

aws ec2 create-volume --volume-type type_of_volume --size size_of_volume --availability-zone your_zone
EBS volume created

Step 6 : Attach EBS Volume to EC2 Instance

To attach the EBS volume we just created, we have command :

aws ec2 attach-volume --instance-id your_instance_id --volume-id volume_id --device device_name
EBS volume attached to EC2 instance

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Sign up to discover human stories that deepen your understanding of the world.

--

--

No responses yet

Write a response