Host Your Sample App In AWS Instance Using EC2 Service

This article will show you how you can host your sample application to EC2 Instance in AWS.

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute capacity in the cloud. It allows you to obtain and configure capacity with minimal friction. Amazon EC2 provides developers the tools to build failure resilient applications and isolate them from common failure scenarios.
 
In this article, we will learn how to,
  • Create an EC2 Instance with CentOS 7
  • Install httpd service to handle the HTTP requests.
  • Set up Inbound rules for EC2 Instance
  • SSH to EC2 Instance
  • Create a Security Group and Key Pair
  • Finally, host the application on EC2 Instance.
I assume that we have an IAM user already created which we will use to create an Instance. So as I log in and go to the EC2 Dashboard, I don’t see any running instances.
 
Host Your Sample App in AWS Instance Using EC2 Service
 
I highly recommend to first create the security group and key pair before creating the Instance. Though we can directly create them during the creation of the instance, it is a good practice to have both of them beforehand available.
 
Security group acts as a virtual firewall which controls the traffic for one or more instances.
 
And Amazon EC2 uses a key pair to allow you to log in to the instance.
 
Let’s get started with the steps.
 
Go to the "Network & Security" section and click on "Security Groups" and then, click on "Create Security Group".
 
Host Your Sample App in AWS Instance Using EC2 Service
 
I have given Earth-sg as we are going to use one of the templates called Earth and this Security Group belongs to that application. Finally, click on "Create".
 
Go to "Network & Security" section and click on Key Pair and then click on "Create Key Pair".
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Finally, click on "Create".
 
Now, go back to the EC2 Dashboard and click on "Launch Instance". Then, go to the AWS marketplace on the left side and search for CentOS and select the one which shows 0.0$/hr.
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Select this and click on "Continue".
 
Then, in the "Instance Type" tab, let the default selection of t2.micro instance to be present as it is free. Click "Next".
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Click "Next": Add Storage and select Delete on the Termination checkbox as this will also delete the volume associated with the instance once you terminate the instance.
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Click on "Next": Add Tags. Tags are basically key-value pairs which are really helpful for managing, searching your resources and add some tags.
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Click on Next: Configure Security Group. Here click on "Select an existing security group" as we already created one and select the one which we created for Earth Project.
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Click on "Review and Launch". Here, you can review all of your details, Security Group, etc. for your instance that is soon going to be launched.
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Finally, click on Launch and select the Key Pair that we created earlier.
 
A key pair consists of a public key that AWS stores, and a private key file that you store. Together, they allow you to connect to your instance securely. 
 
Host Your Sample App in AWS Instance Using EC2 Service
 
And then hit Launch Instances and this will show us the Launch Status.
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Now, go back to EC2 Dashboard and there you will see your 1 instance running.
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Click on this link and we will be redirected to the Instances page.
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Select this instance and click on "Connect". This will open a pop up saying how you can connect.
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Now, we have to connect to this instance and for this purpose, just open the git bash on your machine. By default, it will not allow you to log in with the root user so just change the above command to as shown below. I have this key pair download in my downloads folder, thus I just changed the path.
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Now, we are in our recently created EC2 Instance. Then just switch to root user to install some packages like httpd, wget and unzip.
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Once it is complete, just go to https://www.tooplate.com/ and select the Earth template download URL. (https://www.tooplate.com/zip-templates/2113_earth.zip).
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Now we can see that it has been downloaded successfully.
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Next we have to unzip this zip and copy this to the destination folder.
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Host Your Sample App in AWS Instance Using EC2 Service
 
And we can see that all the files have been copied to /var/www/html successfully.
 
Next is we have to restart the httpd service which will handle the http requests.
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Finally, go to the Instances tab on the left and copy the public IP from the description after selecting the instance.
 
Host Your Sample App in AWS Instance Using EC2 Service
 
Next is to simply paste this IP on the address bar of your browser and see your sample application running.
 
Host Your Sample App in AWS Instance Using EC2 Service
 

Summary

 
So here we discussed the different steps that are required to create the EC2 Instance. Creating the instance is really easy but to create it in a better way is important. That is the reason we have created Security Group and Key Pair in advance and simply assigned them later.
 
We also saw how one can install different packages on CentOS and how can one finally deploy the application and see it running on the EC2 instance in AWS.
 
I hope you find this article helpful. Stay tuned for more … Cheers!!