Thought leadership from the most innovative tech companies, all in one place.

How to Create an AWS Application Load Balancer for Your EC2 Instances

A load balancer will make it possible to distribute the workload across multiple EC2 instances. A client application will connect to the load balancer without knowing which EC2 instance will handle…

A load balancer will make it possible to distribute the workload across multiple EC2 instances. A client application will connect to the load balancer without knowing which EC2 instance will handle the request. Because of this, EC2 instances can come and go without impacting your client requests. It is transparent because of the load balancer. It is easy to scale out and thus add more EC2 instances when traffic increases or scale in and reduce the number of EC2 instances to reduce costs when traffic gets low. This article will describe how to create an AWS Application Load Balancer (ALB) for your EC2 instances running a Spring Boot application. image

Source: aws.amazon.com

EC2 instances can come and go without impacting your client requests. It is transparent because of the load balancer. Therefore, it is easy to scale out and thus add more EC2 instances when traffic increases or scale in and reduce the number of EC2 instances to reduce costs when traffic gets low.

Sample Application

We use the following sample application that will be the basis for creating the EC2 instances and the ALB.

@RestControllerpublic class HelloController {@GetMapping(“/hello”)public String hello() {String message = “Hello AWS!”;try {InetAddress ip = InetAddress.getLocalHost();message += “ From host: “ + ip;} catch (UnknownHostException e) {e.printStackTrace();}return message;}}
Create the EC2 Instances

Create 2 EC2 instances in two different availability zones.

#!/bin/bashyum -y install java-11-amazon-corretto-headlesswget <https://github.com/mydeveloperplanet/MyAWSPlanet/releases/download/v0.0.1-alpha/MyAWSPlanet-0.0.1-SNAPSHOT.jar>java -jar MyAWSPlanet-0.0.1-SNAPSHOT.jar

During the startup of the EC2 instance, Java 11 is downloaded and installed, the jar file is downloaded and the jar file is started. Create a new Security Group and leave the default SSH access inbound rule for now. Name the Security Group ec2-sg. Now launch the EC2 instances.

Create the ALB

In the left menu, navigate to Load Balancers in the Load Balancing section and click the Create Load Balancer button. Here you can choose the type of load balancer you want to use. Choose Application Load Balancer by clicking the Create button. image In Step 1, you give the load balancer the name MyFirstLoadBalancer. image Set the listener to port 8080. image You also enable the availability zones for the load balancer. Check in which availability zones your EC2 instances are running and enable the same availability zones. Click the Next: Configure Security Settings button. image In Step 2, just click the Next: Configure Security Groups button. In Step 3, create a new Security Group alb-sg for your ALB allowing HTTP traffic to port 8080. Click the Next: Configure Routing button. image In Step 4, you need to create a Target Group. A target group can be a number of EC2 instances the ALB will send traffic to. Name the target group MyFirstTargetGroup, set the port to 8080, and set the health check path to /actuator/health. Click the Next: Register Targets button. image In Step 5, you need to add the EC2 instances you want to include in the target group. Select both EC2 instances and click the Add to registered button. Click the Next: Review button. image In the end, you are able to review all the settings and click the Create button. You need to wait sometime before the ALB is active. image Try to invoke the URL with the DNS Name of the load balancer. The time-out is expected. The EC2 instances only allow SSH traffic and no HTTP traffic. In the left menu, navigate to Security Groups in the Network & Security section. Select the ec2-sg security group and click the Edit inbound rules button of the Inbound rules tab. image Add a rule which allows HTTP traffic over port 8080. As a source, you choose the security group alb-sg of your ALB. This means that your EC2 instances cannot be reached directly over HTTP but only via the load balancer. Click the Save rules button. image Try to invoke the URL again and now the Hello message is returned. It will be more or less equally divided between the two machines when you invoke it several times. Navigate to the Target Groups in the Load Balancing section and take a look at the health of the instances. Here you can see that both EC2 instances are healthy based on the configured health check. image In order to clean up everything, you need to delete the load balancer, the target group, terminate the EC2 instances, delete the EC2 security group and finally delete the ALB security group.

Final thoughts

In this article, you learned how to create an ALB as a single point of access for your EC2 instances. The problem with this setup was that you needed to manually add or remove instances when traffic got high or low. The ASG will solve this problem. It will launch or terminate EC2 instances based on several scaling policies.




Continue Learning