AWS Task 2

Peeyush Yadav
9 min readOct 30, 2020

The task follows the same track as my previous stories on AWS(Amazon Web Services). The following are the task descriptions:

— Create High Availability Architecture with AWS CLI

The architecture includes -

  • Webserver configured on EC2 instance.
  • Document Root(/var/www/html) made persistent by mounting on EBS Block device
  • Static objects used in code such as pictures stored in S3
  • Setting up a Content Delivery Network using CloudFront and using the origin domain as an S3 bucket.
  • Finally, place the CloudFront URL on the WebApp code for security and low latency.

Now before we start, to do some of these tasks, we need to refer to some of the concepts and commands from the previous tasks, as this is an extension of the same.

Log in to an IAM user with the given Access and Serial code.

Now that you have gained the access to the account. Let’s start by launching an instance. (Optional if you already have an instance ready to be used)

  • aws ec2 run-instances — image-id <image id of your AMI> — subnet-id <subnet of the specified region> — instance-type <instance type, let’s go with t2.micro for this exercise> — key-name <an available key pair name>— security-group-ids <group id of a security group> — count <no. of instances that you require, let’s create just 1 this time>

Note: Do not add ‘<>’ to your commands, replace those with the values that are specified within these angular brackets.

Upon successful creation and launch of an instance, the CLI screen would look something like this.

the instance is created successfully.

Assuming that you already have created a volume previously, you may use the same volume. If it’s not created yet or not attached to your instance already, you can the following code.

Creating a Volume(Optional)

  • aws ec2 create-volume — volume-type <the type of volume type you need, for example, gp2> — size <specify the size that you need (in GBs)> — availability-zone <Set your availability zone for the volume>

Note: Do not add ‘<>’ to your commands, replace those with the values that are specified within these angular brackets

Attaching a volume(Optional)

  • aws ec2 attach-volume — volume-id <volume id of the volume that is to be attached> — instance-id <instance id of the instance to which a volume is to be attached> — device <a disk type>

Note: Do not add ‘<>’ to your commands, replace those with the values that are specified within these angular brackets

Attaching a volume to the instance.

Once done, we proceed to create a partition in the same volume. You can either do the same while being connected to your instance, you do the same from your AWS CLI.

  • ssh -l ec2-user <public IPv4 address of your instance> -i <key pair name>.pem sudo fdisk /dev/<device type>

Note: Do not add ‘<>’ to your commands, replace those with the values that are specified within these angular brackets

Creating a partition in the instance volume

Now let’s install httpd to enable access over the webserver. To do so:

  • ssh -l ec2-user <IPv4 address of your instance> -i <key pair name>.pem sudo yum install httpd -y

Note: Do not add ‘<>’ to your commands, replace those with the values that are specified within these angular brackets

installing httpd in the instance

This will enable Apache webserver over your instance.

Now we need a file system. To do the same we need to perform a Format( in other words, formatting refers to creating a file system).

  • ssh -l ec2-user <Public IPv4 address of your instance> -i <key pair name>.pem sudo mkfs.ext4 /dev/<disk type>

Note: Do not add ‘<>’ to your commands, replace those with the values that are specified within these angular brackets

Now that you have formatted the disk, let’s start mounting the directory over it. This is done by:

  • ssh -l ec2-user <your IPv4 address of instance> -i <key pair name>.pem sudo mount /dev/xvdf /var/www/html

Note: Do not add ‘<>’ to your commands, replace those with the values that are specified within these angular brackets

Mounting a directory

Now, remember the /var/www/html is to be mounted to allow your data to be accessed from a web server. This in fact is a root directory for the same. Anything in this directory is accessible over a web server, provided you have made them public by changing certain settings.

Now let’s connect and login to our instance from hereby. (Note: The previous steps from installing the webserver to the mounting can be done even when you have been connected to your instance. The only difference is you have to use actual Linux commands to perform the same and not AWS CLI commands)

To connect :

  • ssh -l ec2-user <IPv4 address of your instance> -i <key pair name>.pem

Note: Do not add ‘<>’ to your commands, replace those with the values that are specified within these angular brackets

Now that you have connected to your instance from the CLI, it should resemble strongly an LINUX Terminal.

Now check the status of your httpd server:

  • systemctl status httpd

If your status appears as “active”, then you are good to go. Else you can restart the server by :

  • systemctl restart httpd

Now change to the /var/www/html the directory by going:

  • cd /var/www/html

Create a basic html webpage and save the same. To check if it’s working or not, go to your web browser in your system and type: <your instance address>/<name of your html file>.html

Note: Do not add ‘<>’ to your commands, replace those with the values that are specified within these angular brackets

It should look something like this:

Creating a simple, basic html page to view over the web
Viewing the web page over the web browser.

Further, let’s proceed to create a bucket. This may be required for the times when you often utilize the components that may be present in the buckets of your cloud service. The bucket provides scalable and flexible means of storage, thanks to Cloud services.

To create an S3 bucket.

  • aws s3api create-bucket — bucket <give a unique bucket name> — region <the region for your bucket> — create-bucket-configuration LocationConstraint=<set this value to where your bucket will reside, for example, ap-south-1>

Note: Do not add ‘<>’ to your commands, replace those with the values that are specified within these angular brackets

creating an S3 bucket using CLI
GUI view when a bucket is created

Now, to access the contents of this bucket (or any other bucket) other than within your cloud services, we need to make it public. To make a bucket public:

  • aws s3api put-bucket-acl — acl public-read — bucket <your bucket name>

Note: Do not add ‘<>’ to your commands, replace those with the values that are specified within these angular brackets

making the bucket public

Over the GUI(web page), you would notice a Public is written under the “Access” tab of your bucket. This would indicate that the task has been successful.

The “Public” access indication.

Note: Making the bucket doesn’t mean that anyone with access can scrutinize the content of this bucket. If you notice the command clearly, you would see some permissions were passed too. This permission grants the users (who access the bucket) either to only read, write, or read-write. This limits access from unauthorized activity to the data of the bucket.

Now let’s put some content or a file in the bucket to put it into good use. Let’s start small and put up an image of your choice from your local disk this time.

Uploaded a set of images to the bucket, that would be used later.

Now we make these objects public too. This enables them to be read or downloaded, i.e put into use besides ourselves. The user would be download and then perform the task. This is more than enough for us to use in our tasks for now.

A “Success” banner pop-ups when the object has been successfully made public

Now head back to your html page in the instance. We can use these images to be displayed on our page. Let’s keep it simple, as our main goal to show the working here.

Copy the link of the image and head to html file. Open the file and edit the same.

Adding the image as a link to be used in the webpage.

Save the changes in the file. Now view the same on the web browser.

The image was successfully added to the page. Here I’ve made it as a background.

Since the contents of this webpage were very minuscule and small in size, it probably must’ve felt that the webpage has loaded pretty instant. At lightning speed actually. You may not have even felt the delay, as it was quite fast. But here’s the deal. When we have loaded a lot of data or add-ons on the WebApp it would take a certain time to load them completely, no matter how fast your internet is.

This gets even worse when the source location of these data is located in a different region or location. Suppose you are in the US, trying to access this small website that I created in my nearest region (Mumbai). It may take a few more moments than it takes to load up a Google image, but the delay is very minor to be noticed. The same can be very devastating(to us humans) when we try to access something gigantic over the net, over and over again.

AWS has the solution to this. CloudFront. CloudFront is a fast content delivery system, which makes use of certain AWS edge locations. These help to access the data or content stored with very high speed and low latency. Security is also assured and one can start using these in just moments.

So the same data can be accessed in a much faster way repeatedly, no matter how many times the user wants to access it.

Now, to make the same deployed into our object, we must make use of CloudFront distributions. To set up the same:

  • aws cloudfront create-distribution — origin-domain-name <your bucket name>.s3.amazonaws.com

Note: Do not add ‘<>’ to your commands, replace those with the values that are specified within these angular brackets

Setting up the CloudFront distribution

Now check the same over the GUI. It may take up to 3–5 minutes for the process to be complete. Once done it may look something like this:

Successful deployment of CloudFront distribution

Once the distribution has been successfully deployed, copy the Domain name. We can use the same for the next step.

Go back to your CLI, and your html code. In the code, instead of giving your S3 bucket in the link, replace the same with the copied Domain Name of your CloudFront distribution.

The code should look something like this:

html code to access a CloudFront object(image) in this case

Save the changes.

The CloudFront URL will make use of its edge locations, wherein the content that you retrieve the first time from this URL will be requested and stored in your nearest Edge Locations. So the next time you access the same content, instead of redirecting you to the source, the CloudFront will redirect to your edge locations, which can make the transaction significantly faster.

Finally, access the html file from your web browser.

Webpage, wherein the image has been loaded from the CloudFront URL.

That’s it. This completes the task.

This task was a part of my training of AWS: CSA 2020, under the guidance of Vimal Daga.

--

--