Creating a High Availability Architecture with AWS CLI
Priyanka Bharti
Software Engineer @ Samsung | C++ | Android Development | Kotlin | Linux
Hello Readers!!
Back with another article on AWS Command Line Interface whereby you'll find how to create a high availability architecture using AWS CLI, exploring multiple AWS services using command line like ec2 instance, EBS, S3 etc.
Before starting with this article, have a look to my previous article, on high level demonstration of interaction with AWS CLI to have some prerequisites ????
Let's continue our journey with AWS Command Line for creating a high availability architecture. The architecture includes
- Configuring webserver on EC2 Instance
- making its document root (/var/www/html) persistent by mounting on EBS Block Device.
- Static objects used in code such as pictures stored in S3
- Setting up Content Delivery Network using CloudFront and using the origin domain as S3 bucket.
- Finally placing the Cloud Front URL on the webapp code for security and low latency.
Let's jump to the practical directly!!!
Configuring webserver on ec2 instance
For this purpose, you need to launch an ec2 instance using AWS CLI and then configure it as a webserver.
??Hint??: You can get help with any command when using the AWS command line for the supported services,
To see help text, you can run: $ aws help $ aws <command> help $ aws <command> <subcommand> help
Let's launch an ec2 instance using existing key pair and security group. Complete command to launch a new ec2 instance:
aws ec2 run-instances --image-id <ami-image-id> --instance-type t2.micro --count <number_of_instances> --subnet-id <id> --security-group-ids <id> --key-name <keyname>
Now, you need to configure this instance as a webserver. For this, do remote login and run following commands:
To install apache httpd software and start and enable httpd service, #yum install httpd # systemctl enable httpd --now
Make document root (/var/www/html) of the webserver persistent by mounting on EBS Block Device
For this, let's create an EBS volume say, of capacity 5 GiB, that can be attached to the above created instance in the same Availability Zone. Command to do so:
aws ec2 create-volume --volume-type <volume_type> --size <size_in_GB> --availability-zone <availability_zone>
Now you need to attach the above created EBS volume to the instance created in the previous steps. The volume and instance must be in the same availability zone because EBS is a zonal service !!
Command to attach the EBS volume to the instance:
aws ec2 attach-volume --instance-id <instance_id> --volume-id <volume_id> --device <value>
You can verify from dashboard whether the EBS volume successfully attached to the instance or not.
Now you need to mount the above attached EBS volume to the document root (/var/www/html) to make the data stored in the directory persistent. Before mounting the volume, we need to create partition of volume and format the partition. Follow the commands below to do so:
Creating partition, #fdisk <device_name> n <--- to create new partition p <--- for primary 1 <--- partition number +2G <--- to create a partition of size 2GiB w Formatting partition, #mkfs.ext4 <device_name> Mounting of the partition, #mount <device_name> <directory>
Creating S3 to store static objects such as pictures
Let's create a S3 bucket using AWS CLI. By default, the bucket is created in the US East (N. Virginia) region. You can optionally specify a Region to optimize latency, minimize costs or address regulatory requirements.
? Regions outside of "us-east-1" require the appropriate "LocationConstraint" to be specified in order to create the bucket in the desired region.
aws s3api create-bucket --bucket <bucket_name> --region <region>
Now, let's put an object such as an image inside the above created object. Command to do so:
aws s3api put-object --bucket <bucket_name> --key <object_key_name> --body <object_local_path> --acl <access_control_list_for_object>
You can see the object added from management console as,
Setup Content Delivery Network using CloudFront and using the origin domain as S3 bucket
You can access the above uploaded image using the URL provided for the object by AWS. But as our bucket is in US East region, so there will be latency in access. One way to reduce the latency is, setting up Content Delivery Network using AWS CloudFront for the S3 bucket.
Creating a CloudFront distribution using AWS CLI:
aws cloudfront create-distribution --origin-domain-name <origin_domain_name>
You can see that CloudFront provides a unique URL to access the content of object that too with low latency and also giving security to the content.
Finally place the Cloud Front URL on the webapp code for security and low latency
Let's create a web page in our instance that we configured as webserver and put this object URL as source within it, as shown below:
Finally, the entire high availability setup is done!! You can access the web page from anywhere around the world using the public IP of the instance as I've done below.
So....that's it!!!
With step by step, this is how you can setup a high availability architecture exploiting different AWS resources as per requirements!!
Let's conclude our work here!
Thanks for your patience :)
I'll be keep coming up with more technical stuffs like this...until then, farewell!!!