Create a high availability Infrastructure using AWS CLI

Mrunali Gorde
4 min readMar 20, 2021

In this architecture we are going to use Amazon Services like S3,Cloudfront,EC2,EBS.

This architecture includes:-

  • Launching an AWS EC2 instance and configure it as web server
  • Making the document root persistent using EBS block device
  • Store static objects like images in S3 storage
  • Setting up a Content Delivery Network using Cloudfront and using the origin domain as an S3 bucket.
  • Using URL generated by Cloudfront to display the image in our website for security
  1. Launching an AWS EC2 instance and configure it as a web server:-

a) Launching AWS EC2 instance:- I have already explained in the blog link is given below.

https://mrunalgorde.medium.com/how-to-use-aws-cli-to-launch-an-ec2-instance-f68273a749ef

b) Configure a web server:- a) Start the launched ec2 instance. Use aws configure command and login to the instance. Check the status of the instance by aws ec2 describe-instances or visit management console.

First we have to login inside the instance. I am logging through the command line provided by AWS in management console. Use root login bysudo su — — root.

Now , for starting a webserver we require httpd software and will download that using yum commad yum install httpd .

2. Making the document root persistent using EBS block device:- The httpd service is configured, now we have to create a EBS volume for storing our data. Httpd bydefault selects /var/www/html directory to store its webpages.

We will create an EBS volume and attach to our instance using cli. Use command, aws ec2 create-volume — availibilty-zone us-east-1e — volume-type gp2-size 5

Attach the created volume, aws ec2 attach-volume --instance-id-<instance-id> --volume-id <volume-id> --device /dev/sdf

EBS is the block storage. For using it first we need to create the partition then mount it. We will do this using AWS CLI. Use these commands; fdisk /dev/xvdf , m for help , n for new partition, p for primary, enter 3 times(this will select the bydefault values) , w to save and alter.

fdisk -l will show all the volumes present and attached.

Now before mounting it is necssary to format the partition, format partition using mkfs.ext4 /dev/xvdf1

After formatting mount the partition to /var/www/html using :- mount /dev/xvdf1 /var/www/html

Now we can store our code in document root.

Start the httpd service. Use systemctl start httpd

3) Store static objects like images in S3 storage:-

Create a bucket from cli , aws s3api create-bucket --acl public-read --bucket <bucket-name>

Check on management console whether the bucket is created

Upload an image to the s3 bucket using cli.

aws s3api put-object --bucket <bucket-name> --key <image-name> --body <path of object>

4. Setting up a Content Delivery Network using CloudFront and using the origin domain as S3 bucket.

For creating setting up CloudFront use this command on CLI , aws cloudfront create-distribution --origin-domain-name <bucket-path> --default-root-object <object-name>

This will set the network. Initially you may face error like

“ An error occurred (AccessDenied) when calling the CreateDistribution operation: Your account must be verified before you can add new CloudFront resources. To verify your account, please contact AWS Support (https://console.aws.amazon.com/support/home#/) and include this error message.”

As I am using free tier student’s account there are some limitations. Soon I will take the help of support to enable this service so that we can continue the practical. Almost everything is done just we can to paste the generated url of browser that will show our index page and the image uploaded in s3.

--

--