Website Hosting on Ec2 instance with SSL enabled using an Application Load Balancer and Route53.
Tags: aws, cloudcomputing, aec2, route53
Published: 2022-06-28 03:56:04

**Amazon EC2:** The Amazon Elastic Compute Cloud is a web service that helps you to run virtual machines in the cloud by configuring its capacity, security, and networking.

**EC2 instance:** A virtual server on Amazon’s Elastic Compute Cloud (EC2) to run your business software.

Create an EC2 instance and install a web server
First, you create an EC2 instance in the public subnet of your VPC. [Here we used a default public subnet and default VPC].

Choose EC2 Dashboard, and then choose Launch instance,
Choose the Amazon Linux 2 AMI.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z872m48m42wdb8nv20sl.png)
 

Choose the t2.micro instance type, as shown following, and then choose Next: Configure Instance Details.
On the Configure Instance Details page : You can select number of instances you need and Go with Your Default setup or [Choose you VPC if you created].
Choose Next: Add Storage.
On the Add Storage page, keep the default values and choose

Next: Add Tags.

Choose Next: Configure Security Group.

On the Configure Security Group page, shown following, choose Select an existing security group.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/od74nlotupuunh8okz3a.png)
 

Choose Review and Launch.
On the Select an existing key pair or create a new key pair
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mw6azvjgikd08jmkt0ds.png)
 
To launch your EC2 instance, choose Launch Instances.
Name those server as

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ek2nbipfwvzz4srxu7n.png)
 
 
Now connect these servers with xshell and so to root user.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nlln9bb2a8nxh1hvw7h9.png)
 
sudo su , cd [command for root user]

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ynlgsi6t60t1ikrc4h3k.png)
 

In-order to host a website in ec2 , First you need to download web server [ Here we are going to install apache server ].
yum install httpd , type yes if required

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6eh9qrg2r2crlsqwtyjm.png)
 
We can use Github commands to clone our website code from our repo , so You need to install git. yum install git -y

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tvi1x7knocbgvftai23s.png)
 

Now we have to clone that repo to our web server folder name [ html ] as follow

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6eq4l66hakx1kwpbhtzb.png)
 
cd /var/www/html [ path ].

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7uwrnfdnx7dbvgjze2wo.png)
 

We need to start Apache Server using command below
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7f0s6hllnkfaueupqtot.png)
 [In case if we stop and start our server , we need start our apache again , to avoid this we can simply enable that server]
Continue this process with 3 server [You can use single or multiple server ]
Paste public IP to browser , You can able to see your website

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ba8pwisamixqzntam0y.png)
 

**Some other ways to upload your website**

You can use s3 bucket to upload your website and using IAM we can access to this or using WinSCP application you can simply copy paste your code to that folder.

**What is a load balancer ?**
A load balancer serves as the single point of contact for clients. The load balancer distributes incoming application traffic across multiple targets, such as EC2 instances, in multiple Availability Zones.

This increases the availability of your application. You add one or more listeners to your load balancer.

A listener checks for connection requests from clients, using the protocol and port that you configure. The rules that you define for a listener determine how the load balancer routes requests to its registered targets.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ojexcti0fgtpswg0kydy.png)
 

Each target group routes requests to one or more registered targets, such as EC2 instances, using the protocol and port number that you specify.

Elastic Load Balancing supports the following load balancers: Application Load Balancers, Network Load Balancers, Gateway Load Balancers, and Classic Load Balancers.

**Create Load Balancer**

Search for load balancer and click on create.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ocxm3xxkxvu0am4eccgz.png)
 

Select Application load balancer.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pqdt7j5gp703jfxb1cap.png)
 
Name it as follow

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z5mrei3mxb72dcars4en.png)
 

Select VPC and subnet
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/htdf2qgh38t7nxga6vzi.png)
 

Create a Security group enable ssh,http,https.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mny03tabmxr7f28hkzc9.png)
 
In the navigation pane, under Load Balancing, choose Target Groups.
Choose Create target group.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5m8y219yu9177umdg7za.png)
 
Under Basic configuration, keep the Target type as instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4euesib015r7b7pamo3e.png)
 

For Target group name, enter a name for the new target group.
Keep the default protocol (HTTP) and port (80).
Select the VPC containing your instances. Keep the protocol version as HTTP1.
For Health checks, keep the default settings.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jcdqexmnesonu3zyqx6n.png)
 
Choose Next.

On the Register targets page, complete the following steps. This is an optional step for creating the load balancer. However, you must register this target if you want to test your load balancer and ensure that it is routing traffic to this target.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ptneczdhfet4cfe9o7nz.png)
 
For Available instances, select one or more instances.
Keep the default port 80, and choose Include as pending below.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hcwhr7nvkl35xfkgfanx.png)
 
Choose Create target group
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ruyxb022y4q4fftparo1.png) 
Leave default setup
Create load balancer

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/69d372zuak1ljolgxggy.png)
 

Paste that DNS name of your load balancer in browser you can see your website.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vcytqavoy9w446ku7ied.png)
 
**How to attach a domain name**

you require a free or purchased domain to fully complete all the steps. If you already have a domain, then awesome, if not, don't worry, you can get a free domain! You can visit the following site and get yourself a free domain. 
https://www.freenom.com/en/index.html?lang=en

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c9f4fas3sllgmu9545m7.png)
 
Once you sort out your domain, you should go back to the AWS console and navigate to the “Route 53” service. You should then navigate to “Hosted zones” and create a new hosted zone.
Amazon Route 53 is a highly available and scalable Domain Name System (DNS) web service. You can use Route 53 to perform three main functions in any combination: domain registration, DNS routing, and health checking.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zsgsy3yzpj8427gocdrg.png)
 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r0xa6b7xor2jphcci6dk.png)
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j82f3ogthb2y7czn4jss.png)
 

You have to make sure to enter the exact domain name and select “Public hosted zone” for the type when creating the new hosted zone.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8seiij49dout6jrca7xr.png)
 

Once you have created the hosted zone, it should contain two records, NS (Name Server) record and SOA (Start Of Authority) record. You will need to use the NS record in the next step!
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kqxegb9f67lluizxt3nk.png)
 

Next, you should head over to the admin panel of your domain provider, in my case it is freenom.com. You should find the section which enables you to configure the name servers for the domain! For different domain providers, this would look a bit different!
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w87hxmfcpu9os3g0mg35.png)
 
_- You should be aware that sometimes Nameservers takes a couple of hours to Sync in. So if your domain doesn't work at the end of this article, be patient and try again in a few hours _

You can also create record name , record type , record traffic
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rcltvio9km39dx4a18c4.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/616ccxdu58f9to0cry5p.png)
 
After that you can type www.yourDomainName.ml in browser , But its not secured , to make it secured we need to attach a SSL to it.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7xyk8krnqmky8tn8g5cw.png)
  

**How to attach a SSL to our domain**

AWS Certificate Manager is a service that lets you easily provision, manage, and deploy public and private Secure Sockets Layer/Transport Layer Security (SSL/TLS) certificates for use with AWS services and your internal connected resources

Search for Certificate Manager
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fqd8r64gluxfk86a36bn.png)
 
Request a public certificate
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2tnj92fllkes3f6sdmhn.png)
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1s44hsublgnksmuqqol.png)
 

Enter your domain name you can use * before your domain name for host based routing.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e05dt9gr98f9vmdfyw07.png)
 
Click on certificate and create records
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6k5sbubuvne2nv9t7an.png)
 

Create DNS records in Amazon Route 53.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vpkla5inylclsert3fn4.png)
 
A CNAME record is added to your record
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8tkpbswyk5hz94vlqvxq.png)
 

After that go to load balancer .. Click listener and Add Listener.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8kvmc0dfkmxno6rg46sn.png)
 

Add HTTPS Protocol 443 port , Select the target group .
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mdhhntwxa7wwvgxjvh5k.png)
 

In default SSL certificate part :- Add the certificate [ That certificate you have created in Certificate Manager ]
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j8dgc9mal4ndlll0438x.png)
 

Edit the HTTP part Click on add condition select Host add www.domanname.ml and redirect to HTTPS 443 port [save it] add domanname.ml and redirect to HTTPS 443 port [save it]
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tkve3b7fykjtmsv1xfth.png)
 
Now it will be redirect to HTTPS protocol,
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/udor1275hosb3kjrbb7f.png)
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vfnot1ndq45fmlzq44su.png)
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/971m4t2a5lk3fykya5xr.png)
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/provzbqx6k33b7d3yw14.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aq5cmww2heold53h7mvp.png)
  

**Connection Draining
**
When Connection Draining is enabled and configured, the process of deregistering an instance from an Elastic Load Balancer gains an additional step. For the duration of the configured timeout, the load balancer will allow existing, in-flight requests made to an instance to complete, but it will not send any new requests to the instance. During this time, the API will report the status of the instance as in Service, along with a message stating that “Instance deregistration currently in progress.” Once the timeout is reached, any remaining connections will be forcibly closed.

**Stickyness**

Suppose two separate web browsers each request three separate web pages in turn. Each request can go to any of the EC2 instances behind the load balancer, like this:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o0maysa0b3rn6xu1soj8.png)
 
When a particular request reaches a given EC2 instance, the instance must retrieve information about the user from state data that must be stored globally. There’s no opportunity for the instance to cache any data since the odds that several requests from the same user / browser will go down as more instances are added to the load balancer.
With the new sticky session feature, it is possible to instruct the load balancer to route repeated requests to the same EC2 instance whenever possible.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozb7gclcls2jx8zmlcdy.png)
 

In this case, the instances can cache user data locally for better performance.
A series of requests from the user will be routed to the same EC2 instance if possible.
If the instance has been terminated or has failed a recent health check, the load balancer will route the request to another instance.