Reverse proxy On Aws With help of Ansible

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.

https://raw.githubusercontent.com/A-zish/Ansible_Ec2/master/ec2.pyhttps://raw.githubusercontent.com/A-zish/Ansible_Ec2/master/ec2.ini

Both files need to be in executable format:

chmod +x ec2.py
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

HAPPY CODING………

Github url — https://github.com/A-zish/Ansible_Ec2.git

--

--

--

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

Recommended from Medium

Website Detective: Python Flask, MongoDB and Celery

xMag Vol. 2 — Jan and Feb 2022

Hard and symbolic links on Linux

Flutter a Simple Todo App

Enabling secure and efficient Low Code / No Code platform customisation at scale: Why we invested…

The Agile Accounting

Stride Guide: 6 tips for agile teams

Send mail using sendgrid in laravel

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
Ashish Dwivedi

Ashish Dwivedi

More from Medium

AWS Devops tools combined with ECS to achieve automated Docker container deployment to ecs.

Easy way to add AWS profiles with a bash function

Run scripts on CloudFormation stack creation with Custom Resources.

AWS EC2 shutdown and still getting charged? Create a budget alert.