Configure LoadBalancer(HAProxy) and WebServer(Apache) on AWS using Ansible.

This article will teach you in-depth about how to configure a Loadbalancer using HAProxy software on the AWS EC2 instance and configure webserver using Ansible.

Ansible is a configuration management tool and HAProxy is a software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spread requests across multiple servers.


  1. Basic knowledge of AWS and Ansible.
  2. Basic knowledge of the Load Balancer.

We will launch some EC2 instances and make one of the instances an load balancer and tag it as a load balancer. And all of the other instances as a webserver. After that, we will dynamically fetch the IP address of these instances and group them according to the tags. All of the webserver IPs are grouped to form one group of webservers and the IPs of loadbalacers are grouped to form a group of loadbalancers. These groups will dynamically form an inventory for Ansible.

If you don’t know about how to set up the dynamic inventory and how to create the ROLE then do refer to this article where I had explained how to set up the dynamic inventory and how to create the ROLE.

Link for the article that explained dynamic inventory and ROLE

Let's start…

Step 1: Create an HAProxy ROLE

Go to the default location that we had set up for the Ansible ROLE in the previous article and create the haproxy ROLE there.

$ cd /home/cankush/AnsibleAutomation/ansible_galaxy/

Create an haproxy ROLE using the following command:

$ ansible-galaxy init haproxy
Create an haproxy ROLE

As this role is created by the ansible-galaxy command, it has the pre-created directory structure for the management purpose. The directory structure of this ROLE will look like this-

Let’s start writing the code.

Go to the haproxy/task/main.yml file. This file will contain the code for installing, configuring, and starting the haproxy service.

But, as we are changing the configurations multiple times, it is required to restart the service every time the configuration is get updated/changed.
For this, we have to add the handler to our ROLE.

Go to the haproxy/handlers/main.yml and add the following code. This code will restart the haproxy service every time the configuration file is updated.

We want to run our loadbalancer service on the port 8080. But it may be possible that we can change the port in the feature. For dealing with such dynamic data, ROLE provides one file where we can define the variables and later use these variables wherever we want in the ROLE.

This file is located at haproxy/vars/main.yml. We will add one variable here that will store the port number of the haproxy loadbalancer service.

Now, the last thing we required to do is the haproxy configuration file.

In this file two main things we have to provide, one is the haproxy loadbalancer port number and another is the IP address of the servers where our application is running.

On line no 44, we are providing the haproxy loadbalancer port number that will be taken from a variable defined in the haproxy/vars/main.yml file.

Now, we have to provide the details of the backend that is the IP addresses of the servers where our application is running. But we have multiple servers and we have to load balance all of them. Here, we are grouping the server running with the application that we have to load balance. For this, we will tag all of these servers with the same tag name that is ‘tag_Name_Auto_webserver’.

For using all of these IP addresses, we will be using Jinja templating(line no 63 to 65) that will go through the list of all IP addresses and provide them as the backend. Our server with the application is running on the port 8123.

Finally, we have created the haproxy ROLE.

Step 2: Create an ansible-playbook

This ansible-playbook will create three EC2 instances for launching the webserver and one EC2 instance for the haproxy loadbalancer. We had tagged the webserver instances with the tag name “Auto_webserver” and the haproxy loadbalancer instance with the tag name “Auto_loadbalancer”.

Finally, we will configure the instances tagged with the tag name Auto_webserver using the webserver ROLE which we had created previously(refer to the link given earlier in this article). And the instances tagged with the tag name “Auto_loadbalancer” using the haproxy ROLE that we had created just before.

But for launching these instances on the AWS, it is required to provide the AWS access credentials to the playbook. We are providing these credentials using the secret.yml file on the line no 3 which is an ansible vault file.
The secret.yml file contains two variables that have the AWS_ACCESS_KEY and AWS_SECRET_ACCESS_KEY.

aws_access: "YOUR_AWS_ACESS_KEY"

Secure this file by encrypting it using the command

$ ansible-vault encrypt secret.yml

Step 3: Running the ansible-playbook

At last we will run the ansible-playbook and it will automatically provision entire infrastructure and configure the haproxy loadbalancer and the webservers on the EC2 instances.

$ ansible-playbook --vault-password-file=secret.yml haproxy.yml

Step 4: Accessing the WebApp

We simply have to go to the public IP of the loadbalancer on the port 8080.

The URL will be LOAD_BALANCER_PUBLIC_IP:8080/home.html

The loadbalancer will balance the load among the webserver using the round robin algorithm. That is the first request will be sent to the first webserver, second request will be sent to the second webserver and so on. And after sending the request to the last webserver, the loadbalancer will again send the next request to the first webserver and so on.

Thanks for reading!

If you liked this article, please applaud it.
You can also follow me on Twitter at @cankush625 or find me on LinkedIn.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store