Automating the Deployment of Web Application by Integrating Ansible with AWS Cloud

!! 𝗛𝗲𝗹𝗹𝗼 all!!

Welcome to my article based on TASK-2 of RH-294 -Linux Automation using Ansible (RHCE8) Training


🔹 Launch an AWS instance with the help of ansible.

🔹 Retrieve the public IP which is allocated to the launched instance.

🔹 With the help of the retrieved Public IP configure the web server in the launched instance.

In these task we are going to solve one use case based on configuring a web server on AWS cloud and launching one web application by automating all the setup using Integration of Ansible with AWS Cloud .


1) For AWS Cloud whoever come to them are considered to be a Client .We are going to use Ansible for launching an Instance on AWS Cloud but for that how Ansible will contact to AWS Cloud and Launch the Instance ? . As Ansible use Python libraries behind the scene to configure anything . Hence for AWS Cloud Python has one library called boto which connect the Ansible to contact with AWS API .

To install boto library use the command →

pip3 install boto
boto Installation

2) To launch an Instance on AWS cloud using Ansible , we use Ansible ec2 module by which Ansible going to launch ec2 instance on AWS . For AWS login we require two types of keys as public access key and secret access key . I make the use of IAM user to create security keys for authentication of AWS account .

3) For security perspective it is always good to store security credentials in separate files and make the use of variables in main playbook . Here the use of Ansible vault which encrypt the security credential file in which we keep our AWS security keys by specifying variables for it.

To encrypt the the file using Ansible vault use command →

ansible-vault encrypt file_name

4) By using ec2 module in the playbook Ansible can provision ec2 Instance on AWS cloud by using the file named as ansible.yml containing security keys which is encrypted using vault for authentication on AWS cloud .

We see in AWS console that ec2 instance is successfully created by Ansible .

AWS Console

5) Now , we have to configure a web server into launched ec2 instance .For that we have to do SSH login to go inside ec2 Instance .As all these things we going to do using Ansible which requires public ip of ec2 Instance and SSH Private key to login .Since for Ansible ec2 Instance is become a managed node . To contact to managed node Ansible using Inventory file which is provided in ansible.cfg file .But in these TASK we are going to add ec2 instance to the host group of Ansible dynamically using keyword add_host .

This add_host keyword used to add the hosts to ansible inventory file dynamically by creating host group . By using these the IP of ec2 has fetched dynamically and set as host for the Ansible .

Here the use of keyword with_items gives the output in JSON format and use of keyword wait_for that is to wait for ec2 Instance that they first start port no 22 for SSH login .

6) Next step is to login into ec2 Instance via SSH which requires SSH private key and Instance IP .For that the configuration file of ansible should be provided with the private key file .

[defaults] host_key_checking = FALSE private_key_file = /root/Ansible-tasks/key17.pem

One more thing is to change the permission of key file as it is by default restricted by some permissions under root user .By changing the permission using following command key file can be used for login by Ansible .

chmod 600 key17.pem

Using the private key file and retrieved public IP of ec2 Instance Ansible going to configure webserver on their manged node i.e ec2 Instance .

After successful completion of the Ansible tasks we can see the output by using public ip of ec2 instance whether the webserver is successfully configured or not .


We run the ansible playbook which done all the above tasks successfully by using following command .

ansible-playbook --ask-vault-pass playbook_file_name

One click the the entire set up is done by end to end automation .

In these way I successfully completed the TASK-2 of RH294 — Linux Automation using Ansible (RHCE8) Training .

I would like to thanks Mr.Vimal Daga Sir for giving such challenging task which helps to explore the core concepts of ansible about integrating it with other technologies .

!! Thanking you all for visiting my article !! If you find it resourceful please upvote it

💫 Keep Sharing Keep Learning 💫

Published By

Arnab Saha

#vimaldaga hashtag#rightmentor hashtag#righteducation hashtag#worldrecordholder hashtag#makingindiafutureready hashtag#RH294Ansible hashtag#automation hashtag#Redhat hashtag#RHCE8 hashtag#aws hashtag#cloudcomputing hashtag#linux hashtag#linuxworld hashtag#devops

Lifelong learning technologies

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