Deploy Web Server on AWS through ANSIBLE!

Hello viewers ,

So in this article I am going to Deploy a Apache HTTP Web server over AWS EC2 instance using Ansible. Full description of task noted below…

Description…

🔅Provision EC2 instance through ansible.

🔅Retrieve the IP Address of instance using dynamic inventory concept.

🔅Configure the web server through ansible!

🔅Create role for webserver to customize the Instance and deploy the webpage to root directory.

So here, I am assuming you all have little information like what is Ansible, AWS , Web servers and all that.

Lets do it..

Starting from Provision EC2 instance through ansible

So provisioning a EC2 instance through Ansible we have to setup a environment like…

  • Some python libraries like boto, boto3, botocore
  • Some files for dynamic inventory like:
  • ec2.py
  • ec2.ini
note: I already do all the prerequisite setup like all the steps i mention above before this task. I am just sharing this pics for your better understanding.

Firstly I installing these libraries .

pip3 install boto boto3 botocore 
Image of installing libraries.

Now I install these two files ec2.py and ec2.ini

Download these scripts in your default inventory folder of Ansible. With these two file Ansible dynamically fetch the IP’s of EC2 instances this concept in ansible called Dynamic Inventory.

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

After installing these libraries you have to make executable these scripts.by the command written

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

You can check your setup by ping command

ansible all -m ping 

If any instances is live this script fetch the ip.

So all dependencies downloaded perfectly. Now comes to the EC2 provisioning part for this we have to AWS account with atleast one user which has some administrative permissions.

So I have a AWS account and now i am going to create a new user with admin permissions, follow these steps to do same.

Login into account and Search for IAM service

Here, i am giving the administration access to this new user, which is equivalent to the root user so that this new user can create and manage EC2 instances and other services by its own.

Here you can see a new user created with the name kavin having administration power and to login with this user we have some access key id and secret key id. Do not share these credential to anyone.

Now a user created successfully, Comes to the our controller node. To launch a instance or ssh a instance we need a key, key is like a password. So you get that key from Aws but now in our controller node we have to copy that key and make it executable with read permission by the command and then save the key path in ansible config file.

chmod 400 keyname.pem
Ansible config file.

Now we also have to put our credential of AWS user like Access key id in .bashrc file. By doing this Ansible by default get the credential from this file.

vim /root/.bashrc

Now all set to launch a instance on AWS just need to write a play book for provisioning a instance.

So I am wrote a Ansible role to provision a EC2 instance

# tasks file for ec2_instance
- name: create file
file:
path: /ec2_idempotent_hacks
state: directory
notify:
- launching a ec2 instance on aws
- debug
- name: run handlers now
meta: flush_handlers
- name: refresh inventory fact
meta: refresh_inventory
- name: wait
command: sleep 60

First task is to create a Directory name as /ec2_idempotent_hacks, So here i do a small hack to make our ec2 module idempotent. By default ec2 module is not idempotent as many times you run a playbook ec2 module config a instance over AWS. So here i am create a Directory and on that i put a handler. So when this task named as create file run then our ec2 task named as launching a ec2 instance on Aws run. Handler task written in Handler file.

next task is meta: flush_handlers. It is for run handlers immediately not in the end of all task.

Next task is for refresh the Ansible inventory because new Configure node come up so Ansible have to update their inventory.

Next task is to sleep for 60 sec you can decrease or increase the time according in how much time your EC2 instance launch perfectly.

Now comes to the handler file..

# handlers file for ec2_instance
- name: launching a ec2 instance on aws
ec2:
key_name: "{{ key }}"
instance_type: "{{ instance_type }}"
image: "{{ os_image }}"
wait: yes
count: "{{ no_of_instances }}"
instance_tags:
name: "{{ instance_name_tag }}"
country: "{{ instance_country_tag }}"
region: "{{ instance_region_tag }}"
vpc_subnet_id: "{{ vpc_subnet }}"
region: "{{ region_name }}"
assign_public_ip: yes
state: present
group_id: "{{ security_group_id }}"
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
register: input
- name: debug
debug:
var: input

As I mention above I given 2 task to handler one is launch a instance if Directory named as /ec2_idempotent_hacks not created because if this directory created that means this playbook run one time and instance may be already exist.

Debug task is for display information about launched instance.

In this handler file you can see i use many variables all variables in Ansible roles store in vars file.

# vars file for ec2_instancekey: "your public key"
instance_type: "t2.micro"
os_image: "ami-0a9d27a9f4f5c0efc"
no_of_instances: "1"
instance_name_tag: "lab"
instance_country_tag: "IN"
instance_region_tag: "ap"
vpc_subnet: "subnet-0563d68cd56a901fb"
region_name: "ap-south-1"
security_group_id: "sg-0bea222c4ee2c82e5"
aws_access_key: "put your key"
aws_secret_key: "put your key"

In this photo You can see I hide my access key and secret key due to security reason.

Now comes to webserver for this I create a role named as apache_webserver

# tasks file for apache_webserver
- name: install packages
package:
name: httpd
state: present
- name: coping website file
copy:
src: /help/web.html
dest: /var/www/html/
- name: starting the httpd service
service:
name: httpd
state: started
enabled: yes

In this role I wrote few tasks like-

Firstly I installed httpd software,

Then copy our website code to httpd default sever path,

Then start the httpd service.

All set we create role for provisioning a EC2 instance and for web server now wrote a setup.yml playbook to run these role

So this our main playbook in this-

Firstly I run Ansible Role named as ec2_instance on localhost then by Dynamic Inventory concept as i told you above Fetch the EC2 instance ip by tag name and then on that host i run webserver role named as apache_weberver.

ansible-playbook setup.yml

This is the summary of aws ec2 console before running the playbook.

After running the playbook

So all done now time to see our result….

Visit the highlighted Public DNS URL in your web browser.

http://52.66.107.43/web.html

So here you see after done whole process we can launch a http web server in single click as many time you want. This is the beauty of Automation Via Ansible.

Thanks for reading and i hope you will like the Blog!!!

--

--

--

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

Recommended from Medium

Quick Start with Rancher Desktop

Connect Azure Pipelines to GCP

Against a Federal Registry of Genitals

6 Software House Services You Need to Know

Week 1: Path to becoming an AWS Cloud Practitioner

My Most Recent Learning Challenge

Series 3: Continuous Integration using Fastlane and Travis CI

Low code revolution in MVP development

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
Mukul Jeveriya

Mukul Jeveriya

More from Medium

AWS Lambda

Flask_mail Reset Password with Token

Laravel Development Services And eCommerce Daily Deals

Kodla web application and Telegram bot

Kodla web application and Telegram bot