Deploying Web Server on AWS by using ANSIBLE!!

Table of contents

Task:

  1. Provision EC2 instance through Ansible.

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

  3. 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 declarative language to describe system configuration.

Prerequisites:

  • Install the boto3 library of Python.

    • Command: pip3 install boto3

Steps:

  1. Create an ansible-playbook to launch the ec2 instance.

    No alt text provided for this image

  2. Create a variable file to store the access key and secrete key to log in to the AWS account.

  3. Secure the file using ansible vault.

    Command: ansible vault encrypt <file_name>

  4. Check whether the file is encrypted or not.

    No alt text provided for this image

  5. Run playbook to launch ec2 instance.

    Command: ansible-playbook --ask-vault-pass <file_name>

    No alt text provided for this image

  6. Go to the AWS console and check whether the instance was created or not

    No alt text provided for this image

  7. Fetch the instance IP dynamically using the dynamic inventory method in Ansible.

    • Steps:

      • Download the Python files script which fetches the IP dynamically.

        No alt text provided for this image

      • Make the file executable by providing executable permission

        Command: chmod +x ec2.py

      • Export AWS credentials so that the Python script gets access to the AWS console.

        No alt text provided for this image

      • Add the path of the inventory file into the ansible configuration file.

        No alt text provided for this image

      • Check the host list.

        Command: ansible all --list-hosts

  8. Create an Ansible role which configures the web server over the ec2.

    • Command to create the role: ansible-galaxy init <role_name>

      No alt text provided for this image

  9. Add yaml code into the main file from the tasks folder which configures the web server over the ec2 instance.

    No alt text provided for this image

  10. Now, create the ansible yaml file which gives the information about the role and hosts.

    No alt text provided for this image

  11. Finally, run the yaml file which provides information about the role and hosts.

    No alt text provided for this image

  12. Now, check whether the web server is configured or not.

    No alt text provided for this image

Result:

No alt text provided for this image

Thank You for reading!!

0
Subscribe to my newsletter

Read articles from Anushka Visapure directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anushka Visapure
Anushka Visapure