Configuring Elastic Load Balancing
The Elastic Load Balancer (ELB) works within a single AWS region. You can scale both horizontally (adding more EC2 instances) and vertically (increasing EC2 instance size) within AWS, but it's best practice to scale horizontally. It can, however, load balance across several instances in multiple availability zones. If you don't want to load balance instances across multiple availability zones, then you can also disable it. If we want to load balance the instances across multiple regions, then we have to use Route 53 (instead of an ELB). ELB continuously checks the health of the instances, and only routes traffic to healthy instances. The health check frequency and the URL parameters are configurable.
If a healthy instance comes online, then the ELB recognizes the instance and routes traffic to it. ELB can be used to implement high-availability application architectures. If we use Route 53 with ELB, we can enable failover to a different region. ELB can also be configured with autoscaling, thereby enabling load balancing across new instances created by auto-scaling groups.
ELB can work with instances in EC2-Classic and VPC. There are two types of load balancers we can create internal or internet facing. We can't create internal load balancer without VPC. We can create both internal and internet facing load balancers within VPC. You can also enable sticky sessions on ELB using either application generated cookies or ELB generated cookies. In addition, you can assign security groups to ELBs. If you don't assign any security group while creating the ELB in VPC, it uses the default security group of the VPC. SSL termination is also supported in ELB, using this obviates the need to install SSL certificate on each and every EC2 instance.
How to do it…
Here, we list the commands for creating an ELB, configuring the same for performing health checks, and finally associating specific EC2 instances with it.
Creating an Internet-facing ELB with listeners
Run the following command to create an Internet-facing ELB. You will have to provide the listeners, subnet IDs, and security group IDs.
$ aws elb create-load-balancer --load-balancer-name [LoanBalancerName] --listeners [Listeners] --subnets [SubnetIds] --security-groups [SecurityGroups]
The parameters used in this command are described as follows:
[LoanBalancerName]
: This option provides the name of the load balancer.[Listeners]
: This parameter gives a list of the following tuples:Protocol
,LoadBalancerPort
,InstanceProtocol
,InstancePort
, andSSLCertificateId
.[SubnetIds]
: This option gives a list of subnet IDs in your VPC to attach to your load balancer. You can get a list of subnet IDs by running theaws ec2 describe-subnets
command.[SecurityGroups]
: This option provides the security groups to assign to your load balancer within your VPC. You can get security group ID by running theaws ec2 describe-security-groups
command. You should provide the security group name in the preceding command.
Run the following command to create an ELB that receives traffic on port 80, and the load balances across instances listening on port 8080:
$ aws elb create-load-balancer --load-balancer-name WebLoadBalancer --listeners Protocol=HTTP,LoadBalancerPort=80,InstanceProtocol=HTTP,InstancePort=8080 --subnets subnet-aed11acb --security-groups sg-c6b873a3
Configuring health checks on ELB
Run the following command to add health check configuration to an ELB. You have to provide the load balancer name and health check configuration:
$ aws elb configure-health-check --load-balancer-name [LoanBalancerName] --health-check [HealthCheckup]
The parameters used in this command are described as follows:
[LoanBalancerName]
: This option provides the name of the load balancer[HealthCheckup]
: This parameter provides the health check configurationSyntax:
Target=HTTP:8080/index.html,Interval=30,UnhealthyThreshold= 2,HealthyThreshold=2,Timeout=3
The following command will add the health check configuration to an ELB. The ELB checks the instance health at <URL>:8080/index.html
. ELB health check interval is set to 30 seconds. UnhealthyThreshold
specifies the number of consecutive unsuccessful URL probes before the ELB changes the instance health status to unhealthy. HealthyThreshold
specifies the number of consecutive successful URL probes before ELB changes the instance health status to healthy.
$ aws elb configure-health-check --load-balancer-name WebLoadBalancer --health-check Target=HTTP:8080/index.html,Interval=30,UnhealthyThreshold=2,HealthyThreshold=2,Timeout=3
Adding instances to the ELB
By running the following command, you can add instances to the ELB. You have to provide the ELB name and the list of instance IDs.
$ aws elb register-instances-with-load-balancer --load-balancer-name [LoanBalancerName] --instances [Instances]
The parameters used in this command are described as follows:
[LoanBalancerName]
: This option gives the name of the load balancer[Instances]
: This option gives a list of instances for the load balancer
The following command will add ELB to EC2 instances with IDs i-d3ff2c1e
and i-2e7dace3
.
$ aws elb register-instances-with-load-balancer --load-balancer-name WebLoadBalancer --instances i-d3ff2c1e i-2e7dace3