AWS Architecture with CLI Script

Aman Jhagrolia
6 min readDec 3, 2020

In this article, I have created a complete Highly Available AWS Architecture with AWS CLI Commands. I've created a Script file of AWS CLI Commands which will automatically create complete AWS Architecture with no need for human intervention in between. Here the architecture includes AWS EC2, EBS, S3 and CloudFront. A web server is configured in EC2 instance and a website is hosted on it, This website includes some images which are coming from CloudFront with S3 origin.

Here is the small video demonstrating the complete work

Here is my GitHub Repository form where you can get CLI Scripts used

Explanation of Script:-

Here is my AWS CLI Script which creates complete AWS High Available Architure on run.

  1. In the above script, first of all, variables for AMI ID, VPC ID, Subnet ID and Bucket name are declared. Here we are fetching default VPC and Subnet ID from AWS account and set them in respective variables.
  2. Then, it will create a key pair and after creating that it will also save its PEM file locally.
  3. After that, it will create a Security group for our instance that will be launched later. In Security Group port TCP/80 is allowed so that clients can connect to our website. Also, we need to connect to our instance over SSH so port TCP/22 is allowed.
  4. Now it will create an S3 bucket for our static content like images, videos etc. Here one image is uploaded in our bucket using put-object subcommand in aws s3api.
  5. After creating a bucket and uploading items in it a content delivery network i.e. CloudFront distribution is created with S3 bucket as an origin. And the URL of the CloudFront domain is stored in cf_domain variable.
  6. Now it will move to launch an instance. The below script is provided as user data while launching the instance. This script will create a webpage.html file and setup.sh script file in /home/ec2-user/ directory of the instance. This setup.sh script file will be executed later over SSH to setup compete for webserver in the instance.

7. Data is the most important thing for us. We don’t want to lose data in any condition. So here to protect our data i.e. webpages an extra EBS volume is attached to our instance. As we need to protect our webpages and webpages are present in /var/www/html/ directory, So this EBS volume is mounted on this directory only.

8. After that the URL of CloudFront distribution that is created is replaced in our webpages using Linux ‘sed’ command.

9. Now the setup.sh file that is created using user data while launching the instance is executed over SSH. And this will configure the complete webserver in the instance. This script is also used for formatting and mounting the EBS volume.

10. Finally, chrome will automatically start in our local system opening our deployed website using Public IP of EC2 Instance.

11. That's All, Our Highly Available AWS Architecture is Ready!!

Running the Script:-

After writing the script, we have to run it and as it is a bash script so we use “bash aws-architecture.sh” command. Here are the screenshots of the output of the script.

Output of the Script

The Created Architecture:-

Now we have the complete AWS Architecture created by the script and we can also go to our AWS Console to check it.

EC2 Dashboard - We can clearly see that Before running the script we don't have any Running Instance, Key Pairs, Security Groups and Volumes but after we run the script we have one running instance, a key pair, a security group and two volumes.

EC2 — Before and After

EC2 Instances - One instance is running on which the webserver is configured.

EC2 Instance

Security Groups - One security group is also created which is attached to our instance. This security group have two inbound rules one for HTTP which allows public clients to connect to our web server and another rule is for SSH which allows us to connect to our instance over SSH.

Security Groups

Key Pairs - A key pair is created for our instance. After the creation of key-pair, it's PEM is also stored locally in webkey.pem file.

Key Pair

Volumes — Here we have two volumes, one is root volume in which instance is installed and booted but the other volume is extra EBS volume which is attached to the instance to store our webpages persistently. This extra EBS Volume is mounted on /var/www/html/ directory in instance.

Volumes

Inside the Instance - While launching the instance user-data.sh script is provided as a user data which will create a webpage.html file and another script named as setup.sh inside /home/ec2-user/ directory. This setup.sh script will do multiple things for us, first, it will configure web server in the instance and then create partitions, format and mount the EBS volume which is attached.

setup.sh Script

Volume Mounted - Here we can see that the EBS volume that we have attached to the instance is now mounted on /var/www/html/ directory where we put our webpages.

Volume Mounted

S3 Bucket - One S3 bucket is also created and which have one object also. This S3 bucket is created to store the Static content like images, videos, docs of our website and This S3 bucket is the origin for our CDN CloudFront.

S3 Bucket with Object

CloudFront - A Content Delivery Network is deployed using AWS CloudFront Service with S3 bucket as an origin. Static content of website reaches to the client by CloudFront so that latency can be reduced. The Domain URL of this CloudFront is used in our website HTML code.

CloudFront

Webpage with CloudFront URL - After the complete deployment of CDN CloudFront with S3 origin its URL is replaced in our website HTML code automatically.

Website HTML Code

Website Deployed and Connected - Finally, we are at the end, our website is deployed on Highly Available AWS Infrastructure and clients from the public world can connect to it.

Website Connected

Thanks for reaching out here, Hope this is helpful to you!!🙂

--

--