Host a website on Ec2 instance with SSL enabled using an Application Load Balancer & add a custom domain managed by Route53.
Published: 2022-02-10 07:14:49

**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].

_
**If you want To create Your own VPC and Subnet then follow** :- https://dev.to/shankarsurya035/vpc-virtual-private-cloud-setup-vpc-endpoints-5feo
_

> - 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/0fvdo5bv3b2lypy51of2.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/wt15kp31tesht6wyepe4.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/b5mg9skb2rbmc8qrgzmz.png)

> - To launch your EC2 instance, choose Launch Instances.

> - Name those server as 

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

> - Now connect these servers with xshell and so to root user.

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

> - sudo su , cd [command for root user]

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

> - In-order to host a website in ec2 , Ist 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/c0cbdrrhhx0j2aw5hyo3.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/xc32ekmaj42ebjujt238.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/nqkvfdlovmvi987bfqq0.png)
> cd /var/www/html [ path ].
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhxzfda3dzokpcuenzx2.png)
> - We need to start Apache Server using command below
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1s9a08s58xy73ilvjask.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/fezh4srcqtggkno6fsj5.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/9g7krvfvsdsyrgtnxi0s.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/t7dgp7oefawfo1leh2ze.png)

> - Select Application load balancer.

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

> - Name it as follow

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

> - Select VPC and subnet 

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

> - Create a Security group enable ssh,http,https.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ir6bi7apgj8z468a4uix.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/upw1hcv3gjzkoh4f0lj0.png)

> - Under Basic configuration, keep the Target type as instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7psb4u67z9xihbqk4p02.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/85fjxlfhs3qjs8nn3wts.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/p5qqan2wqime2ll5kux6.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/2tzkiv73k5hl4ej0ob3n.png)

> - Choose Create target group
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1hbwxo4on3ucgxu34jw7.png)
> - Leave default setup
> - Create load balancer

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/imsjo5k6uihz43c2olih.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/cfd09krb6d8nrq0aeqmp.png)

> - Demo

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wmvrnv4ob4kx5i6grmkp.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/kxoegtyjcedja4mhvtgr.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/yx5quzh4rgxkblk1smf2.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k35zb3s8yabpzhrz8cr5.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/npi3sxoei1bejwpnld47.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/f7x6jia9p4zd4rl35qw0.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/vxrvomf3bdvm62huza5w.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/8apjhvhhv9ksjhdydppc.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzqtvxeqmd789qduzrq4.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/7clncnxn28bil2hfj55s.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/trq6xulqdjjp6lauhuaw.png)

> - Request a public certificate

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0pt775ikuevl89mbzc4s.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/1xhav04aitf0fpuctmt9.png)

> - Click on certificate and create records

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

> - Create DNS records in Amazon Route 53.

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

> - A CNAME record is added to your record

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

> - After that go to load balancer .. Click listener and Add Listener.

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

> - Add HTTPS Protocol 443 port , Select the target group .

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k8080lqwtsa2xbjqduk3.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/yawe67t47nd9anbyardh.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/gdmdrcpniwzvyxh6574f.png)

> - Now it will be redirect to HTTPS protocol,

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

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

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

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

## **Some Other Concepts**

## 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 InService, 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/1ogt89cxo68gry3ddaa2.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/todt7x4gawmmyhp492g5.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.**