How to Setup AWS Application Load Balancer
This blog will show how to create an AWS application load balancer with custom VPC's EC2 instances.
Setup Custom VPC
Go to VPC and click create VPC, enter the VPC field as below example.
- Name tag: enter your VPC name
- IPv4 CIDR block: enter your CIDR block IP range
- IPv6 CIDR block: choose IPv6
- Network Border Group
- Tenancy: Default (you can choose if you want to single-tenant, dedicated hardware or not)
We'll need 2 subnets to create the Application Load Balancer.
- Name tag: enter any name
- VPC: choose the VPC just created
- Availability Zone: choose the availability zone
- IPv4 CIDR block: 18.104.22.168/24
- IPv6 CIDR block: don't assign IPv6 (don't need IPv6 for this blog)
Create the second subnet, for creating Application Load balancer, you'll need at least 2 subnets.
set auto-assign IP, so EC2 instance will get the public IP address.
Create Internet Gateway
Create an internet gateway and attach the custom VPC.
Setup Route Table
Create a route table for the subnet
associate subnet to the route table
set route table to use the internet gateway for internet, enter the routes as following
Destination: 0.0.0.0/0 (for IPv4 Access)
Target: choose the Internet gateway
Now, VPC setup is done, we can use this custom VPC to create your own EC2.
We'll set up EC2 with simple Php code, let's leave everything as a default setting and create 2 EC2 Instance.
- Choose AMI (For example, choose Amazon AMI)
- Choose General Purpose: t2.micro
- Configure Instance Details
- Network: choose the VPC just created
- Subnet: choose the subnet just created
- Leave everything as default
- Put following to User Data field, this will start PHP simple site.
#!/bin/bash yum update -y yum install httpd -y chkconfig httpd on service httpd start cd /var/www/html echo "hi, server-01" > index.html
- Configure Security Group
- add port 80 so it will be accessible from the browser.
Now, let's create another EC2 instance. Setup everything as the first one, except the User data, change to server-02 as following
#!/bin/bash yum update -y yum install httpd -y chkconfig httpd on service httpd start cd /var/www/html echo "hi, server-02" > index.html
Create Target Groups
Let's create a target group for Application Load Balancer, this blog only trying to set up HTTP only.
- Target type choose Instances
- Enter any name for the target group name
- Protocol - Http - 80
- Choose the VPC
- Health checks
- Enter the path
- Choose available instances
- Click Create Target Group
- Creating a target is done and associated with a list of instances.
Create Registered targets
Click registered targets and add EC2 instance by choose 2 EC2 instance and click include as pending
Create Application Load Balancer
- Click Create Load Balancer
- Choose Application Load Balancer
- Enter Name
- Use default Http - 80
- Choose VPC
- Choose subnet (here you'll need at least 2 subnet)
- Choose Security Groups
- Configure Routing (here, select the target group created earlier)
- Now, Load Balancer is creating
- Should be able to see application load between 2 instances
After Application Load Balancer become active, copy the dns name to the browser you should see the site will change between the two server.Now, this is how you can simple provision application load balancer with AWS.