AWS CLI : Let’s learn more

Satyam Singh
6 min readNov 2, 2020
AWS CloudFront Logo

What is AWS CLI ?

The AWS Command Line Interface (CLI) is a unified tool for managing AWS services. With just one tool to download and configure, it enables controlling multiple AWS services from the command line and their automation through scripts.

Note : You can download it as per OS : https://aws.amazon.com/cli/

Content of this Blog

  • Creation of Key Pair
  • Creation of Security Group
  • Creation of EC2 Instance using the Key Pair and Security Group created
  • Creation of EBS Volume
  • Attachment of EBS Volume created to the EC2 Instance created
  • Creation of S3 bucket and uploading of object in the same
  • Creation of CloudFront distribution with S3 bucket as origin domain
  • Configuration of Webserver on EC2 Instance
  • Mounting of Document Root to EBS Volume
  • Creation of Webpage using HTML/CSS and placing the CloudFront URL of the static object

Prerequisites

First, check if AWS CLI has been successfully installed or not

Command :

aws --version
Indication that AWS CLI has been installed properly

Next, configuration using AWS Access Key, AWS Secret Access Key, Region Name and Output Format is performed

Command :

aws configure
AWS Configuration

Creation of Key Pair

Command :

aws ec2 create-key-pair --key-name task6-key --query 'KeyMaterial' --output text > task6-key.pem

Creates an EC2 Key Pair and stores in .pem file named task6-key.pem

Creation of .pem file

Creation of Security Group

Part 1 : Creation of Security Group

Command :

aws ec2 create-security-group  --description "Task 6 Security Group"     --group-name task6_sg

Creates a Security group named task6_sg

Creation of Security Group

Part 2 : Addition of Ingress Rule

Command :

aws ec2 authorize-security-group-ingress  --group-name task6-sg   --protocol tcp   --port 22-80    --cidr  103.245.106.174/32

It adds Ingress Rule to the security group created in the previous step and allows traffic from a specified CIDR only via port range of 22 to 80

Security Group in AWS Web Console

Creation of EC2 Instance using the Key Pair and Security Group created

Command :

aws ec2 run-instances  --image-id ami-0e306788ff2473ccb    --instance-type t2.micro    --count 1   --subnet-id subnet-057c3dcaa38c9263a    --security-group-ids sg-0a7bf6311cf963a63 --key-name task6-key

Creates an EC2 Instance using Image, Instance Type, Subnet , Security Group, Key and Tag specified.

Creation of EC2 Instance
EC2 Instance in AWS Web Console

Creation of EBS Volume

Command :

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

Creates an EBS Volume in the availability zone “ap-south-1a” and of size 1 GiB.

Note: The EBS Volume should be created in the same availability zone as of the EC2 Instance to which it would be attached as EBS is a Zonal Service.

Creation of EBS Volume

Attachment of EBS Volume created to the EC2 Instance created

Command:

aws ec2 attach-volume  --device /dev/xvdb  --instance-id i-0f9fc15cb5ffda  --volume-id  vol-03d187026f91937e2

Attaches the EBS Volume of ID specified to the EC2 Instance whose ID is specified (Both of them should be in same availability zone).

Attachment of EBS Volume to EC2 Instance
EBS Volume in AWS Web Console

Creation of S3 bucket and uploading of object in the same

Part 1 : Creation of S3 Bucket

Command:

aws s3api create-bucket  --bucket tbucket6  --create-bucket-configuration LocationConstraint=ap-south-1

Creates a S3 bucket with default ACL i.e., owner’s full control over the resource.

Creation of S3 Bucket

Part 2: Uploading Object to S3 Bucket

Command:

aws s3 cp /root/satyam.jpg  s3://tbucket6 --acl public-read

Uploads the specified objects to the specified S3 Bucket and the object could be document, image and many more with a public read access

Uploads Object to S3 Bucket
S3 Bucket in AWS Web Console

Creation of CloudFront distribution with S3 bucket as origin domain

Command:

aws cloudfront create-distribution --origin-domain-name tbucket6.s3.amazonaws.com

Creates a CloudFront Distribution with the S3 bucket created above as an origin domain.

CloudFront Distribution in AWS Console

Configuration of Webserver on EC2 Instance

Before configuring the webserver in the EC2 Instance created before, remote login using SSH protocol needs to be performed and the command for the same is

ssh -l ec2-user  -i task6-key.pem  13.232.20.55

In this command, the path to the key created before needs to be specified an the public IP of the EC2 Instance to which the remote login is being performed.

SSH Login to the EC2 Instance

First of all login to the root user and then httpd server needs to be installed and the command for the same is as follows

sudo su - rootyum install httpd -y
Installation of HTTPD Webserver

After the installation succeeds , the service needs to be started for it’s functioning, the command for the same is as follows:

systemctl start httpd

For enabling a service permanently, reducing the need to start it each time OS boots up, the command for the same is

systemctl enable httpd
HTTPD Services

Mounting of Document Root to EBS Volume

In case of httpd Server, the web pages needs to be stored in the location /var/www/html also known as Document Root.

So as to make Document Root persistent in nature, it is mounted to the disk created using EBS , but before mounting , the disk needs to be partitioned and formatted so as to use it for storage.

The commands for the process of partitioning, formatting and mounting are as follows

Partitioning of disk /dev/xvdb
Formatting of the partition created
Mounting of Disk to the Document Root, df -h command could be used to check if mounting was successful or not

Creation of Webpage using HTML/CSS and placing the CloudFront URL of the static object

In this case , a basic web page using HTML/CSS has been created for testing purpose, the static object in the web page is obtained using the CloudFront URL using origin domain as the S3 bucket that consist of that particular static object.

Advantage of using a CloudFront instead of directly using S3 is that workload gets reduced and latency reduces as the cache of the content is stored in the Edge Location closer to the client.

The code is :

HTML Code snippet, note that the CloudFront URL could be seen in the img tag inside the body tag

The output of the code is as follows :

The image here is obtained using CloudFront Distribution

--

--