Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon ELB in VPC

We're using Amazon EC2, and we want to put an ELB (load balancer) to 2 instances on a private subnet. If we just add the private subnet to the ELB, it will not get any connections, if we attach both subnets to the ELB then it can access the instances, but it often will get time-outs. Has anyone successfully implemented an ELB within the private subnet of their VPC? If so, could you perhaps explain the procedure to me?

Thanks

like image 592
Kevin Willock Avatar asked Feb 13 '12 08:02

Kevin Willock


People also ask

Is AWS ELB in VPC?

The popular AWS Elastic Load Balancing Feature is now available within the Virtual Private Cloud (VPC).

Is ELB VPC specific?

Yes, ELB is a software load balancer and when you create ELB object, a custom loadbalancing EC2 instance is put into the all subnets that you specified.

Can ELB span across VPC?

An ELB can only attach instances that are reachable by it. When you place an ELB in a VPC it's constrained there. VPC's are independent and can have overlapping subnet ranges.

Is ALB within VPC?

ALBs can span all subnets in VPC. They are not bound directly to subnets, but rather to TargetGroups which are themselves then bound (indirectly) to subnets. So a single ALB could theoretically route to both private and public subnets within a VPC.


2 Answers

My teammate and I just have implemented ELB in a VPC with 2 private subnets in different availability zones. The reason you get timeouts is that for each subnet you add to the load balancer, it gets one external IP address. (try 'dig elb-dns-name-here' and you will see several IP addresses). If one of these IP address maps a private subnet, it will timeout. The IP that maps into your public subnet will work. Because DNS may give you any one of the IP addresses, sometimes it works, sometimes it times out.

After some back and forth with amazon, we discovered that the ELB should only be placed in 'public' subnets, that is subnets that have a route out to the Internet Gateway. We wanted to keep our web servers in our private subnets but allow the ELB to talk to them. To solve this, we had to ensure that we had a corresponding public subnet for each availability zone in which we had private subnets. We then added to the ELB, the public subnets for each availability zone.

At first, this didn't seem to work, but after trying everything, we recreated the ELB and everything worked as it should. I think this is a bug, or the ELB was just in an odd state from so many changes.

Here is more or less what we did:

  1. WebServer-1 is running in PrivateSubnet-1 in availability zone us-east-1b with security group called web-server.
  2. WebServer-2 is running in PrivateSubnet-2 in availability zone us-east-1c with security group called web-server.
  3. Created a public subnet in zone us-east-1b, we'll call it PublicSubnet-1. We ensured that we associated the routing table that includes the route to the Internet Gateway (ig-xxxxx) with this new subnet. (If you used the wizard to create a public/private VPC, this route already exists.)
  4. Created a public subnet in zone us-east-1c, we'll call it PublicSubnet-2. We ensured that we associated the routing table that includes the route to the Internet Gateway (ig-xxxxx) with this new subnet. (If you used the wizard to create a public/private VPC, this route already exists.)
  5. Created a new ELB, adding to it PublicSubnet-1 and PublicSubnet-2 (not the PrivateSubnet-X). Also, picked the instances to run in the ELB, in this case WebServer-1 and WebServer-2. Made sure to assign a security group that allows incoming port 80 and 443. Lets call this group elb-group.
  6. In the web-server group, allow traffic from port 80 and 443 from the elb-group.

I hope that helps!

like image 79
Nathan Pahucki Avatar answered Oct 11 '22 22:10

Nathan Pahucki


The key here is understanding, that you are not "Adding subnets/availability zones" to ELB, but rather specifying what subnets to put ELB instances into.

Yes, ELB is a software load balancer and when you create ELB object, a custom loadbalancing EC2 instance is put into the all subnets that you specified. So for the ELB (its instances) to be accessible, they have to be put into the subnets that have default route configured via IGW (most likely you classified these subnets as public).

So as already was answered above, you have to specify "public" networks for ELB, and those networks should be from the AZs where your EC2 instances are running. In this case ELB instances will be able to reach your EC2 instances (as long as security groups are configured correctly)

like image 24
RSH Avatar answered Oct 11 '22 22:10

RSH