Reverse proxy On Aws With help of Ansible

Ashish Dwivedi
4 min readMay 30, 2021


In this blog we will see how we can create a load balancer setup or reverseproxy setup on Aws cloud with the help of ansible and with help on ansible we will retrive ip of ec2 instance dynamically and automate whole process.

For this we have to setup ansible to launch ec2 instance with dynamic inventory.

# TASKS TO PERFORM FOR Dynamic Inventory

-Provision EC2 instance through ansible.

-Retrieve the IP Address of instance using the dynamic inventory concept

STEP1 — Launching Ec2 instance

~ As Ansible is built on top of python, a Python Software Development Kit (SDK) is required that enables the configuration of AWS services. The package is an object-oriented API named boto3.

pip3 install boto3   //assuming python3 is installed
  • Here, the RedHat system itself calls the API for configuration on AWS, and this procedure is done on the local machine that’s why the host is supposed to be localhost.
  • For authentication to the AWS account, create one IAM user that has less privileged than the root account. The AWS_ACCESS_KEY and AWS_SECRET key are passed explicitly through an Ansible so this keys either we can create a file with .boto extension we can provide key here or we can provide a var file and save our keys there .
vi ~/.botto

or we can create file and we will provide file file variable in ansible playbook

vi keys.yml

This ansible playbook will help us to launch ec2 instance and it will retrive access key and id from vars_files .

STEP 2 — Dynamic Inventory

The instance has been launched!

We need to dump the IP address of this instance into the inventory file and do the further procedure! So we will use concept of dynamic inventory.

~Ansible dynamic inventory is a concept that contains scripts that work as external APIs and pulls the information(facts) of a particular provider.

~ The gathered facts will be dynamically dumped into the host file and further, we can create groups of these hosts according to requirement.

~ Copy the following files into the controller node to enable dynamic inventory.

Both files need to be in executable format:

chmod +x
chmod +x ec2.ini

Also, for account authentication, pass AWS_ACCESS_KEY and AWS_SECRET_KEY in the ec2.ini file. This will contact to AWS on our behalf and retrieve the information of the ec2 instance.

Now run command to see that our ansible is connected to aws or not.

ansible all --list-hosts

STEP3 — Configuring apache and haproxy on Insatnces

This ansible code will configure web sever and haproxy.cfg file with new ip of our instance.

Instead of runing both playbook diffrent we either we can make role of the file or another way is this -

Things to be noticed all the files should be in same directory and key with which we launch ec2 can be precreated or we can create with ansible her i already had key in .pem format and in ansible cfg file the key path and name should be specified.

vim /etc/ansible/ansible.cfg

now run final ansible playbook

ansible-playboook final.yml

As we can see our web server and haproxy is configured


Github url —