Hello,In this article I will discuss about how to set up an EC2 instance to expose a NodeJS app hosted. The application will be visible to the outside world as well and anyone should be able to browse the application from his or her system.
As a prerequisite I am assuming that the user has a AWS login account and knows about the AWS console. Lets get started a with a step by step guide on how to configure an EC2 instance for setting a web application server.
Amazon EC2
As per AWS documentation EC2 is “Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers. Amazon EC2’s simple web service interface allows you to obtain and configure capacity with minimal friction.”
With minimal efforts and some configuration experience we can have our own application hosted in cloud. EC2 provides us the flexibility to configure the software on the system as per our needs. We can configure the same application on EC2 instance to auto scale.
Without wasting more time lets get started with EC2 creation.
Step By Step guide for EC2 creation and Web Hosting
Creating A new VPC
- Let’s start by creating a new VPC. VPC is a virtual private cloud which can be used to isolate your resources from the outside world. It can be blocked by outside access using security groups and configuring IP address which can be allowed to access the resources of the same VPC. Select the VPC service under Services. You can find the service on the top left corner of the AWS console. Go to the Your VPC in the left navigation pane.
- The page you can can see is VPC dashboard. On the top left corner you can see a button named “Create VPC”. Click the button and a new window will appear as shown in the figure below. We can create a maximum of 5 VPC in a region and VPC creation is free in AWS. I have given the name “myVPC” and assigned the IPv4 CIDR block as 10.0.0.0/16. A VPC can span across multiple availability zones present in the region. To restrict the resource under one availability zone we need to restrict the resources by subnets.
Creating an Internet Gateway
- Now go to the Internet gateway item present in the Left list of the VPC dashboard as shown in the figure below. Internet gateway helps to access the internet from the resources present in the VPC. Once you are on the internet gateway page click on the “Create Internet Gateway” button present on the top left corner. Enter the gateway name as shown below in figure 5 and click “Yes create”.
- Once you are done with gateway creation. You need to attach the gateway to a VPC which you have created in the above section. In the VPC dashboard select the mygateway which you created in above step. Click on Attach VPC button and in the pop up select the VPC we just created as shown in the figure 6 below. Click “Yes Attach” button.
- Once attached you can see the attached VPC as shown in the figure 7 below.
Configuring Routes Table
- Go the routes tables under VPC dashboard. Select the route for the VPC we have created in above section. As shown in the figure below I have selected the route for “myVPC”. Select Routes tab present in the below half of the page. Click “Edit” and add a new route as shown in the figure below.
This route states that for all the outbound traffic from any of the resources should go through the internet gateway we created in above section. Once you enter into the target textbox you can see the gateway we just created. Once you are done, save the settings.
Creating a Subnet
- Now we need to create subnet to be associated with the VPC for a single availability zone. In the VPC dashboard click on the Subnets in the left navigation pane. And create a new subnet by clicking “Create Subnet” button. I have created a new subnet named “mySubnetA” and associated with my VPC. I have set the availability zone as “us-east-1a”. Refer the figure below for the same. After all the entries are made click “Yes Create” button.
Creating an EC2 Instance
- Go to the aws console and select the EC2 from the services tab available in Top left corner of the aws console.
- One EC2 is selected you are presented with the EC2 console page. On the same page click on the Launch button present on the left corner.
- Once you click Launch button you can see the Choose AMI page. Select windows server 2012 R2 base as shown in the figure below. This EC2 instance is Free tier eligible. Click on “Select”.
- Choose an instance type t2.micro as shown in the figure 2 below. Click “Next: Configure Instance Details” button present in the bottom right corner. And this will navigate you to the next page of configuration.
- Once you are done with the above steps you will be on the Configure Instance tab of the page as shown in the figure beloe. In this same tab, we can enter the VPC and subnet we created in the above sections. As we need only one instance for the time being that is why we entered only 1 in the number of instances. Select “Disable” in auto assign public IP and leave other settings as it is. Click “Next: Add Storage” present in bottom of page.
- In the Add Storage section leave everything as present by default and Click “Next:Add Tags”
- In add tags section I added the Name tag and “myVPC” as Value. Please don’t confuse it with the VPC name which I created in the first section. It can be anything you want. While writing the article and being in the flow I just renamed it as the same ass VPC. Click on the “Next: Configure Security Group” button in the bottom. Please refer figure 12 below.
- Under configure security group add the values as shown in the figure 13 below. As you can see in the figure the I have assigned a name to the security group i.e. “myVPCsg”. You can see a default rule added for RDP. One more rule I added for the HTTP protocol enabling port 80. This is the same port on which our web application will be browsed. Click review and Launch. In the next screen you can review the configuration settings and finally Launch the instance.
- When you Launch the instance you will be asked to Create a public key in a pop up as shown in the figure below. You can download the key after you enter a name for the key. This key will help you later to login to the instance. This is a .pem file and you need to store the file at secure location. Once you download the .pem file you should be able to launch the instance.
- Once we create the instance we can see our instance running in the EC2 dashboard with lots of information as shown in the figure below. In the figure, we can see lots of information. We can see the instance has a private IP but not a public IP. We can see the availability zone which we set up for the subnet. We can also the key associated with the instance.
Assigning a Public or Elastic IP to EC2
Our instance is ready but still we cannot access the instance from our local machine. To access the instance or do an RDP into the instance we need a public IP. Elastic IP help the resources to be accessible from the outside world. Once these IPs are created they can be associated with an instance. Once the instance is terminated the IP can be kept and again associate with different instance.
- In the left window pane in the EC2 dashboard, you can see Elastic IP. Click on Elastic IP and Allocate new Address as shown in the figure below.
- Click on the Allocate button in the next screen.Check the figure below.
- Once you are done with the above step you will get a new IP address in the elastic IP grid. Now select the IP in the grid and click on the action button and select associate address as shown in the figure below.
- In the next screen select the instance and the private IP. It is the same instance which we created in Creating an EC2 instance and other settings as shown in the figure below.
- Once you are done with the above steps go to the EC2 dashboard. Select the instance which we created and click “Connect”. In the prompt you will be asked to upload the .pem file when you click the Password button. This is the same .pem file which we downloaded in the previous section. Once we upload the .pem file we should get an password in the pop up. Using the public IP and the password we can connect to the instance remotely from our local machine using the “administrator” login account.
In my next part of the article I will show how we can set up a Node js server in the same EC2 instance and host a web application. This application can be browsed by anyone and from any location.
Leave a Reply