Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Amazon EC2 Cookbook

You're reading from   Amazon EC2 Cookbook Over 40 hands-on recipes to develop and deploy real-world applications using Amazon EC2

Arrow left icon
Product type Paperback
Published in Nov 2015
Publisher Packt
ISBN-13 9781785280047
Length 194 pages
Edition 1st Edition
Arrow right icon
Toc

Table of Contents (10) Chapters Close

Preface 1. Selecting and Configuring Amazon EC2 Instances FREE CHAPTER 2. Configuring and Securing a Virtual Private Cloud 3. Managing AWS Resources Using AWS CloudFormation 4. Securing Access to Amazon EC2 Instances 5. Monitoring Amazon EC2 Instances 6. Using AWS Data Services 7. Accessing Other AWS Services 8. Deploying AWS Applications Index

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, and SSLCertificateId.
  • [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 the aws 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 the aws 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 configuration

    Syntax:

    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
You have been reading a chapter from
Amazon EC2 Cookbook
Published in: Nov 2015
Publisher: Packt
ISBN-13: 9781785280047
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime