Deploying Web Server on AWS through ANSIBLE!

Sanket Badjate
4 min readDec 16, 2020

Hello Everyone!!! In this article I have explained the 2nd task of ansible training i.e created ec2 instance on AWS and configured webserver on that by using automation tool Ansible.

Problem Statement:

Deploy Web Server on AWS through ANSIBLE!

♦️Provision EC2 instance through ansible.

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

♦️Configure the webserver through ansible!

♦️Create a role for the webserver to customize the Instance and deploy the webpage to the root directory.

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 boto

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

# launch ec2 instance
- name: Ansible test
hosts: localhost
gather_facts: False
vars_files:
- /root/mycode/secure.yml
tasks:
- name: launching AWS instance using Ansible
ec2:
aws_access_key: "{{aws_access_key}}"
aws_secret_key: "{{aws_secret_key}}"
key_name: key1
instance_type: t2.micro
image: ami-052c08d70def0ac62
region: ap-south-1
wait: yes
vpc_subnet_id: subnet-68056e24
count: 1
assign_public_ip: yes
instance_tags:
Name: Ansible_ec2
register: x

Now create the vault for access_key and secret_key (For security purpose). You can create the vault file by typing this command.

Now run the ansible playbook to launch ec2 instance

Now here we are using python code to find the ip of the instance dynamically. So we download this code from GitHub in the directory /mydb.

wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.pywget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini

Now to make this files executable run following commands:

chmod +x ec2.pychmod +x ec2.ini

You have to configure AWS_REGION, AWS_ACCESS_KEY_ID AND AWS_SECRET_ACCESS_KEY inside the ec2.ini file. After doing this you have to export these things.

export AWS_REGION='ap-south-1'export AWS_ACCESS_KEY_ID='XXXXXXX'export AWS_SECRET_ACCESS_KEY='XXXXXXXXX'

Now you can check ip by using ec.py code

./ec2.py --list

Now we get ip. So configure this ip in /etc/myhosts.txt file. To access ec2 instance by ssh we require key so copy that key in this system using Winscp.

Now check that ip is properly pinging or not.

Now we have to configure webserver on the ec2 instance so we have to write yaml code. For this we are creating one role.

What is Role?

Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules. In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse.

So now we are creating one role by using command:

ansible-galaxy init role_name

Here we can see different files are created where we can manage our code.

Now we have to configure ansible.cfg file and give the role_path.

Now in the tasks folder, we are creating YAML code in the main.yml file to configure the web server on the ec2 instance which we have launched.

- hosts: all
become: yes
become_user: root
vars:
- serv: "httpd"
tasks:
- name: Download httpd
package:
name: "{{ serv }}"
state: present
- name: copy code
copy:
dest: "/var/www/html/"
src: "/root/mycode/home.html"
- name: Start the service
service:
name: "{{ serv }}"
state: started
enabled: yes
register: x

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

Finally, we can check it through the browser :

Thanks for reading!!

--

--

Sanket Badjate

Tech enthusiast | AWS | Ansible | Docker | Kubernetes | Jenkins | ML | Python | JavaScript🤓