AWS CLI TASK2
credits to vimal daga sir

AWS CLI TASK2

In this article I will integrate various services of AWS : EC2, EBS, S3 and Cloud Front

I will launch a webserver using EC2 and data file will be stored in EBS , for object storage like images I will use S3 and for better availability and low latency I will use Cloud Front. Most of the setup will done using CLI.

Step1: Launching EC2 Instance

I will be using the generated secret key and security which were created during my previous article on AWS CLI

aws ec2 run-instances --image-id ami-04b1ddd35fd71475a 
                      --instance-type t2.micro --key-name MyKeyPair 
                      --subnet-id subnet-85a3aded --count 1
                      --security-group-ids sg-0ae53f599ea33dd12

This command will launch an AWS instance using the key MyKeyPair and the given Security Group ID.

No alt text provided for this image

Check using the below command

aws ec2 describe-instances

Checking in the WebUI

No alt text provided for this image

Step2: Creating a S3 bucket and uploading a object file

For creating a bucket we use the command s3api. Use the below command to create a bucket.

aws s3api create-bucket --bucket sfnam --region ap-south-1 --create-bucket-configuration LocationConstraint=ap-south-1

This will create a bucket named "sfnam" . Although S3 is a global service we need to specify the region (like ap-south-1) in order to create a bucket.

Note: The bucket name specified should be unique else the command will fail.

No alt text provided for this image

Checking in the console

No alt text provided for this image

Uploading a object like image in the bucket

aws s3 cp test.jpg s3://sfnam/

This command will upload test.jpg file into the bucket

No alt text provided for this image

Check the contents of the bucket in the console

No alt text provided for this image

Making Bucket and Object Public

AWS by default denies the public access of the bucket and its objects

hence we have to make the bucket and the object public

For making the Bucket public use the below command

aws s3api put-public-access-block --bucket sfnam --public-access-block-configuration "BlockPublicAcls=false,IgnorePublicAcls=false,BlockPublicPolicy=false,RestrictPublicBuckets=false"

No alt text provided for this image

Check the permissions in the aws console

No alt text provided for this image

Then we will make the object public

aws s3api put-object-acl --bucket sfnam --key test.png --grant-read uri=https://acs.amazonaws.com/groups/global/AllUsers
No alt text provided for this image

Check the permissions of the object in the console

No alt text provided for this image

Hurray!! we have make the object and bucket public

Lets check the object URL

No alt text provided for this image

Step3: Creating an EBS Volume and attaching it to the EC2 instance

Now we will make an EBS volume of 2 GiB and attach it to the running instance using the AWS CLI

Creating an EBS volume

aws ec2 create-volume --availability-zone ap-south-1a --volume-type gp2 --size 2
No alt text provided for this image

Check the volumes in EC2 dashboard

No alt text provided for this image

Now we have to attach it to the EC2 instance.For that we need the Instance Id

Run the below command

aws ec2 describe-instances
No alt text provided for this image

Now run the below command to attach the volume to EC2 instance

aws ec2 attach-volume --volume-id vol-07c0760cc578dde8a --instance-id i-0333a64332e1615a7 --device /dev/sdf
No alt text provided for this image

Step4: Connecting and Setting up Webserver

Connect to the EC2 instance using WebUI and with root power install apache webserver(httpd)

Run the below commands

sudo su

yum install httpd
No alt text provided for this image

Now we have to start the httpd services

use the below command

systemctl start httpd

this will start the httpd services

But we also want the services to be on forever

use the below command

systemctl enable httpd
No alt text provided for this image

Step5: Partitioning, Formatting and Mounting the Disk to the instance

Check the name of the disk

fdisk -l
No alt text provided for this image

We can see that /dev/xvdf is the name of the disk

Creating Partition

Use the below command to open up a menu for partitioning

fdisk /dev/xvdf

This will open up a configuration menu for partioning

Press "n" (for creating partition)

Press "p" (for primary partitioning)

Press "Enter" (3/4 times)

Press "w" (for exiting)

Follow the above steps and hence we have completed the partitioning

No alt text provided for this image

Formatting the disk

mkfs.ext4 /dev/xvdf
No alt text provided for this image

Mounting the disk

mount /dev/xvdf /var/www/html
No alt text provided for this image

And for checking whether the disk is mounted or not

run #df -h

Hurray!! we have attached and mounted the disk to the EC2 instance

NOTE: We have mounted the disk to the /var/www/html because we have installed Apache WebServer, hence the all the files like html, css, Js etc are stored in that folder. For us the code as well data is important. Hence using the EBS volumes helps in avoiding the data loss.

Step6: Code of the html document

With root powers , go to the /var/www/html directory and make a file in it called "index.html" and content is

No alt text provided for this image

Lets check the Public IP of our instance now

Public IP is (of mine): 13.126.115.186 (It is visible in the left corner of the webUI of instance) or can be found in the EC2 dashboard

No alt text provided for this image

Hence we have used S3 for object storage and EBS for data file storage

Suppose if my webserver is to used by members of other countries. if this same is followed then there will high latency and the site will be very slow. To avoid such kind we use CloudFront.

Amazon CloudFront is a fast content delivery network (CDN) service that securely delivers data, videos, applications, and APIs to customers globally with low latency, high transfer speeds, all within a developer-friendly environment. (from AWS documentation)

Step7:Using CloudFront in our WebServer

In CloudFront we have to create distributions

Use the below command to create it

aws cloudfront create-distribution --origin-domain-name sfnam.s3.amazonaws.com
No alt text provided for this image

Check in the console

No alt text provided for this image

Now after the distribution gets enabled the data cache will transferred to all the edge locations if anyone customer opens the site. We will have data cache stored in edge locations all over the world. and Hence reducing the latency and security problems

No alt text provided for this image

From the above pic copy the Domain name and open it in the browser

No alt text provided for this image

We can see that the object is as same as the S3 object. But the latency issue will be solved.It might take some time to load for the first time

Lets change the S3 object URL to Cloudfront URL

No alt text provided for this image

Now we will again open the Public IP of the instance. We can find that it take some time to load then but only for the first time

No alt text provided for this image

CloudFront also provides beautiful WeBUI to check the Cache statistics

Like no. of hits&misses, managing the traffic, common quality of majority people etc

No alt text provided for this image

Yay!!! we have successfully deplyed webserver on cloud using multiple AWS services like EC2 S3 CloudFront and EBS

Thanks for giving it a read

Any suggestions or queries are welcomed

Author: Syed Faheemuddin

Email Id: [email protected]

References : AWS Documentations


要查看或添加评论,请登录

Syed Faheemuddin的更多文章

社区洞察

其他会员也浏览了