High availability website with CloudFront
You may not have heard the name of CloudFront but surely you should have heard the name of CDN (Content Delivery Network). When you host a website in a place, though the whole world with an internet connection can visit your website, But not all can access your website as fast as the other. This is due to latency in the network. The places which are far from the hosting servers will be slower than the nearer ones. So what CloudFront is a CDN where the static files are cached in servers local to the country or parts of the country with the help of AWS edge locations which are small datacenters located. Thanks to Vimal Daga sir, in this article, I am going to create a high availability infrastructure with AWS CLI interface
Create an S3 bucket
we can create s3 bucket using the command:
aws s3 mb s3://smclibucket --region ap-south-1
Copy images to s3 bucket
To copy, AWS provides cp subcommand. But we also need to make sure that public read should be active for anyone on the internet can browse it
aws s3 cp --acl public-read awsclicover.jpg s3://smcclibucket/awsclicover.jpg
Create CloudFront and connect to S3
we create a CDN using the below command
aws cloudfront create-distribution --origin-domain-name smcclibucket.s3.amazonaws.com
This is the output we get after using the url domain name given in the JSON output
Creating ec2 instance
To create AWS EC2 instance, I have written a detailed blog. check this link for more information.
aws ec2 run-instances --image-id ami-0e306788ff2473ccb --instance-type t2.micro --count 1 --subnet-id subnet-08f36788b8dbb4452 --security-group-ids sg-040c0a171e0c5ec03 --key-name gpkey
Creating EBS Volume
aws ec2 create-volume --size 1 --availability-zone ap-south-1a
Attaching EBS Volume to instance
aws ec2 attach-volume --device /dev/sdb --instance-id i-0d394b94058283eb9 --volume-id vol-0a121c402765aef17
starting a webserver
first we need to mount the disk. For this we can just format the volume and then create a new partition table and mount it to /var/www/html folder
Then install httpd server and the index.html file that we want to display. After doing this, we get the output with cloudfront URL being used
Thanks for reading the article and hope you liked this article