Creating a Load Balancer on top of AWS using Ansible.

Dileepkumar
3 min readDec 21, 2020

Task Description:

Use Ansible playbook in AWS to Configure Reverse Proxy i.e. Haproxy and update it’s configuration file automatically on each time new Managed node (Configured With Apache Webserver) join the inventory.

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning. Automation is crucial these days, with IT environments that are too complex and often need to scale too quickly for system administrators and developers to keep up if they had to do everything manually.

HAProxy is free, open source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spreads requests across multiple servers. It is written in C and has a reputation for being fast and efficient.

Lets Start →

Firstly launch the instances in the AWS.In my case i launched 2 webservers and 1 Load Balancer

Next lets update our ANSIBLE configuration file.. for AWS instance

Here we have to give key location

Then create Inventory File in Controller Node.Here [awsWebapp] contains the ip of webserver instance and [awsLoadbalancer] contain the ip of Loadbalancer instance in AWS.

Inventory File

Create a Ansible Playbook

Ansible Playbook

To make our code dynamic and automatically configure the Haproxy configuration file add the below code in haproxy configuration file in controller node.

Now letsRun the Playbook,

Now lets check in Loadbalancer.. cmd: vi /etc/haproxy/haproxy.cfg

Updated Haproxy configuration file

php code created in Controller Node

Php code (my.php)

Now when we provide the loadbalancer ip in the web browser we get the content from the backend web server (WEBAPP)

Here we can see when we refresh the page we can the source from the alternative Webserver this is possible bcoz of Load Balancer.

Finally we configured Haproxy on Top of AWS and Update it’s Configuration file automatically on each time new Managed Node join the inventory.

Github url: https://github.com/dileep-hub/Create-LoadBalancer-on-AWS-using-Ansible---12

Thanks for Reading…… 🙂

--

--