Deploy a Load Balancer and multiple Web Servers on AWS instance through Ansible

Task Description :-

  1. Provision EC2 instances through ansible.
  2. Retrieve the IP Address of instances using the dynamic inventory concept.
  3. Configure the web servers through the ansible role.
  4. Configure the load balancer through the ansible role.
  5. The target nodes of the load balancer should auto-update as per the status of web servers.

Step 1: Let us create an Ansible configuration file for AWS.

In my case I have create ansible configuration file is in/etc/ansible/ansible.cfg folder location.

Step 2: Create Dynamic Inventory.

To create the dynamic inventory by fetching ec2 instance IP we will have to use and ec2.ini file.

So first we have to download these files from GitHub.

Inside file we will have to change the python version to 3

Also we have to file executable.

#chmod +x

Step 3: Install boto library and export environment variable.

To make an API request to AWS EC2 we have to install bolo library and also export the environmental variable needed for boto.

#pip3 install bolo

#export AWS_DEFAULT_REGION=’Enter the region name’

#export AWS_ACCESS_KEY_ID=’Enter AWS Access key’

#export AWS_SECRET_ACCESS_KEY=’Enter AWS Secret key’

Step 4: Create Ansible Roles for launching the instances, configuring webserver and configuring load balancer.

Launch instances for webserver and load balancer:

#ansible-galaxy init instance

Configure webserver:

#ansible-galaxy init webserver

Configure Load Balancer:

#ansible-galaxy init lbserver

Step 4: Lets write Ansible code of launching instance, configuring webserver and load balancer.

First let see instance role:

  1. tasks/main.yml

2. /vars/main.yml

Here we have encrypted the vars file.

Let see webserver role:

  1. tasks/main.yml

Let see lbserver role:

  1. tasks/main.yml

2. handlers/main.yml

3. templates/haproxy.cfg

Here the frontend is bind at port number 5000 and backend(webserver) is at port number 80. Here roundrobin algorithm for load balancing.

Step 5: Provision EC2 instances through ansible.

For this I am using task3.yml file.

Step 6: Configure the web servers and load balancer through the ansible role.

For this I am using setup.yml file.

Successfully our load balancer is configured. Let see the output.

Thank You !!




I'm passionate learner diving into the concepts of computing 💻

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How Positive Behavioral Metrics Can Boost Your Software Team

It’s okay to say you are blocked

Check Yourself Before You Wreck Yourself

Beyond Docker with Earthly

Automate AWS Infrastructure with Boto 3: AWS Health Checks

On Spark, Hive, and Small Files: An In-Depth Look at Spark Partitioning Strategies

Bedrock Pattern : A better way of sharing code ?

Apache is Still the Best General-Purpose Web Server

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
Aditya Raj

Aditya Raj

I'm passionate learner diving into the concepts of computing 💻

More from Medium

EC2 Instance Storage : AMI Overview

Deploy AWS ECS based Applications without DownTime Using Rollback Methodology

AWS: Auto-Scaling groups and EC2 Instances for High Availability (HA)

Installing Docker on an AWS EC2 instance

Docker engine on AWS EC2 architecture