Deploying Web Server on AWS through ANSIBLE!!

Sanket Bendale
4 min readOct 21, 2021

--

Task:

♦️Provision EC2 instance through ansible.

♦️Retrieve the IP Address of instance using a dynamic inventory concept.

♦️Configure the webserver through ansible!

Amazon Web Services:

Amazon Web Services (AWS) is a subsidiary of Amazon providing on-demand cloud computing platforms and APIs to individuals, companies, and governments, on a metered pay-as-you-go basis. These cloud computing web services provide a variety of basic abstract technical infrastructure and distributed computing building blocks and tools. One of these services is Amazon Elastic Compute Cloud (EC2), which allows users to have at their disposal a virtual cluster of computers, available all the time, through the Internet.

Ansible:

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration. Ansible was written by Michael DeHaan and acquired by Red Hat in 2015.

First of all, we have to set up an Ansible environment in our system. For doing anything on the aws using the local system with the help of ansible then you have to install boto library of python.

# pip3 install boto3

Now first we have to create an ec2 instance on AWS by writing .yml code.

ec2.yml:

Create one more pass.yml file for store the my-access and my-secret keys.

  • # vi pass.yml
myaccess: "ACCESSKEY"
mysecret: "SECRETKEY"

After creating or save pass.yml file we have a vault for more secure for this we have used:

# ansible-vault encrypt pass.yml (File_Name)

For check the vault is working or not:

Now run the ansible-playbook to launch ec2 instance:

Now here we are using python code to find the IP of instance dynamically. So we download this code from GitHub in the directory /etc/ansible:

# wget https://raw.githubusercontent.com/sanket3122/Ansible_Task2/master/ec2.py# wget https://raw.githubusercontent.com/sanket3122/Ansible_Task2/master/ec2.ini

Now to make this files executable run following commands:

# chmod +x ec2.py# chmod +x ec2.ini

We need to initiate them, run the following commands, and Provide your aws credentials.

# export EC2_INI_PATH=path_of_ec2.ini_file
# export AWS_ACCESS_KEY_ID="aws_access_key"
# export AWS_SECRET_ACCESS_KEY="aws_secret_key"

Python Script:

Python script fetches the IP of aws ec2-instance, also will play the role of dynamic inventory. We need to configure inventory and add some other details.

Save the inventory file and run following command, we will get ec2-instance IP:

Now check that IP is properly pinging or not:

Now we have to configure webserver on the ec2 instance so we have to write web.yml code.

Save all the files and run ansible will get the IP of our running instance and configure it.

Now everything is done properly. We can login into ec2 instance and check the webserver is deployed or not.

Here our ec2-instance configure successfully, now check the web-browser.

Thanks for reading!!

--

--