Launching AWS instance using AWS-CLI

Utkarsh Srivastava
3 min readMar 21, 2021

In this article, I am going to show how to launch ec2-instances using Command Line Interface(CLI).

Objective →

  1. Create a key pair.
  2. Create a security group.
  3. Launch an instance using the above-created security group.
  4. Create an EBS volume of 1 Gb.
  5. Attach the EBS volume with the instance.

Let's see how we do?

Creating a key pair.

The most important step is to create a key pair. This key pair must be kept safe and secure with the user so that the person can access the EC2 instance created using this key pair.

Now, create the key-pair using the below command:

aws ec2 create-key-pair --key-name AWS-Keypair --query "KeyMaterial"

Creating a Security Group

For security group, use the below commands:

aws ec2 create-security-group --group-name <security-group-name> --description "<description>"

Launching Instance using above Security Group and Key Pairs.

For running the EC2 Instance use the command as given below.

aws ec2 run-instances --image-id <ami-id> --count 1 --instance-type t2.micro --key-name <Keypair-name> --security-group-ids <SecurityGroupId> --subnet-id <SubnetId>
Instance Launched.

Creating an EBS volume of size 1 Gb.

Using the command below we can launch EBS storage.

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

Attaching the volume with our instance.

This could be done using the below command.

aws ec2 attach-volume --volume-id <volume-id> --instance-id <instance-id> --device /dev/sdf

→To use the attached storage further, format it and mount it.

Hence, our task is done. This is the way we use CLI to meet our requirements. We did the whole setup of the architecture using AWS- CLI.

Thank You!

#AWS #RedHat #Linux #CLI #LinuxWorld

--

--