Building a cloud infrastructure using AWS CLI

Piyush Mehta
4 min readNov 4, 2020

--

This is an introductory level task that includes the building of cloud infrastructure using AWS CLI instead of the Web User Interface.

AWS Logo

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.
đź”… The final step is to attach the above created EBS volume to the instance you created in the previous steps.

Prerequisites:

* AWS CLI SDK tool installed

aws --version

* AWS IAM user account

aws configure
Enter your IAM user credentials here

Creating a Key Pair

aws ec2 create-key-pair --key-name KeyPairCLi

The above command creates a new key pair named KeyPairCLi.
Since we might need our Key Pair to be stored as a file for future use, we pass a query along with the above command to retrive the “KeyMaterial” data and pipe the same data in a file and store it with a .pem extension.

aws ec2 create-key-pair --key-name KeyPairCli --query "KeyMaterial" > KeyPairCli.pem

Creating a Security Group

aws ec2 create-security-group --group-name MySgCLI --description=" Allow SSH Only " 

The above command creates a new Security Group with “MySgCLI” name and the provided description.

The security by default has no inbound rules, hence we can’t access the OS, to allow SSH access to the above security group we need to define some rules

aws ec2 authorize-security-group-ingress --group-id sg-08f3e74b6f2d3d013 --protocol tcp --port 22 --cidr 0.0.0.0/0

Launching an instance using the above created key pair and security group.

aws ec2 run-instances     --security-group-ids sg-08f3e74b6f2d3d013            --instance-type    t2.micro   --image-id ami-052c08d70def0ac62  --key-name KeyPairCLi  --count 1

The above command creates 1 new EC2 t2.mirco Instance of the provided AMI using the above created Security Group and Key Pair.

Creating an EBS Volume

aws ec2 create-volume --volume-type gp2 --size  1 --availability-zone ap-south-1a 
Availability Zone of our Instance

The above command creates a new EBS volume of 1 GB for us in the provided availability zone ( Note, AZ should be the same as that of our Instance )

Attaching the EBS to our EC2 Instance

aws ec2  attach-volume   --volume-id vol-01d4376e6cb9bcad6  --instance-id i-0f1270420e4153203  --device sdf

This command attaches our EBS volume to our EC2 Instance, as sdf.

TASK COMPLETED

It’s always good practice to destroy all the resources after use.

aws ec2  detach-volume  --volume-id  vol-01d4376e6cb9bcad6
aws ec2 terminate-instances --instance-ids  i-0f1270420e4153203

Done!!!

Thanks for your time, Hope you liked it :)

--

--

No responses yet