How to Deploy a Web App Using Ansible on EC2 Instances : A Complete Guide for Beginners

How to Deploy a Web App Using Ansible on EC2 Instances : A Complete Guide for Beginners


What is Ansible?

Ansible is an open-source automation tool that helps you manage servers, configure software, and automate repetitive tasks. It’s widely used by system administrators and DevOps engineers to make their lives easier. Instead of manually configuring each server one by one, Ansible allows you to automate those tasks through simple text-based scripts called playbooks.

What are EC2 Instances?

In AWS (Amazon Web Services), an EC2 instance is a virtual machine that runs in the cloud. You can think of it like a computer or a server that is hosted on AWS's infrastructure. EC2 instances are scalable, meaning you can easily create, configure, and scale these instances based on your needs.

Why Use Ansible for EC2 Instances?

When working with multiple EC2 instances, configuring them manually one by one can be time-consuming and error-prone. Ansible automates this process, allowing you to install software, configure settings, and even deploy applications with just a few commands.


Step-by-Step Solution : Deploying a Web App using Ansible on EC2 Instances

Step 1 : Setting Up EC2 Instances

Before we can use Ansible to automate our tasks, we need to create some EC2 instances where our web app will run. Follow these steps to create 3 EC2 instances in AWS:

  1. Login to AWS Console: Go to AWS Console and log in to your account.

  2. Launch EC2 Instances: From the EC2 dashboard, click on Launch Instance to create new virtual servers.

  3. Select an AMI (Amazon Machine Image): Choose an Ubuntu AMI (or another Linux-based image) since it’s widely used and easy to set up.

  4. Choose Instance Type: For this project, you can choose a t2.micro instance (eligible for the AWS Free Tier if you’re using a new account).

  5. Configure Instance: You can leave most of the default settings, but ensure your instances are on the same VPC (Virtual Private Cloud) and Security Group. The security group should allow SSH access (port 22) so you can connect to your EC2 instances.

  6. Key Pair: During setup, create or select an existing Key Pair. This key will be used to securely SSH into your EC2 instances later. Download the private key and keep it safe.

  7. Repeat these steps to create 3 EC2 instances.

Once your instances are running, note down their public IP addresses. We’ll need these to connect via SSH and configure them using Ansible.


Step 2 : Install Ansible on Your Host Server

In this project, you'll use Ansible from a host server to manage the EC2 instances. Here’s how to set it up:

  1. SSH into your host server (this can be your local machine or a cloud server) where Ansible will be installed. Use the following command:

     ssh -i /path/to/your/key.pem ubuntu@your-host-ip
    
  2. Install Ansible:

    • Update the package manager:

        sudo apt update
      
    • Install Ansible:

        sudo apt install ansible
      
  3. Verify Installation: Once installed, check the Ansible version to confirm it was successfully installed:

     ansible --version
    

Now, Ansible is ready to manage your EC2 instances.


Step 3 : Transfer the Private Key to the Host Server

To allow Ansible to connect to your EC2 instances, you need to provide the private key (which you downloaded during EC2 instance creation). Here’s how to do it:

  1. Copy the private key (the .pem file) from your local machine to the host server. You can use the scp (secure copy) command:

     scp -i /path/to/your/key.pem your-private-key.pem ubuntu@your-host-server:/home/ubuntu/.ssh/
    

This will allow Ansible to use the private key to securely connect to your EC2 instances.


Step 4 : Configure Ansible Inventory File

Ansible needs to know which servers it should manage, and this is done through the inventory file. The default inventory file is located at /etc/ansible/hosts.

  1. Open the inventory file with sudo:

     sudo vim /etc/ansible/hosts
    
  2. Add the public IP addresses of your three EC2 instances, grouped under a label like [web_servers]. For example:

     [web_servers]
     ec2-instance-1-ip
     ec2-instance-2-ip
     ec2-instance-3-ip
    
  3. Save and close the file. Ansible will now know which servers to connect to for the tasks.


Step 5 : Write an Ansible Playbook to Install Nginx

Ansible uses playbooks, which are YAML files that define the tasks to be automated. In this step, we’ll create a playbook to install Nginx (a popular web server) on all three EC2 instances.

  1. Create a new file called install_nginx.yml:

     vim install_nginx.yml
    
  2. Write the following content in the playbook:

     ---
     - name: Install Nginx on Web Servers
       hosts: web_servers
       become: yes
       tasks:
         - name: Update apt cache
           apt:
             update_cache: yes
    
         - name: Install Nginx
           apt:
             name: nginx
             state: present
    
         - name: Ensure Nginx is running
           service:
             name: nginx
             state: started
             enabled: yes
    

Explanation:

  • hosts: web_servers: This targets the group web_servers from the inventory file, i.e., your EC2 instances.

  • become: yes: This means that tasks will be run with administrative (root) privileges.

  • Tasks:

    • Update apt cache: Ensures the package manager is updated.

    • Install Nginx: Installs the Nginx web server.

    • Ensure Nginx is running: Starts Nginx and ensures it runs after a reboot.


Step 6 : Run the Ansible Playbook

Now that we’ve created the playbook, it’s time to run it!

Use the following command to run the playbook and install Nginx on your EC2 instances:

ansible-playbook -i /etc/ansible/hosts install_nginx.yml

Ansible will connect to each EC2 instance listed in the inventory file and execute the tasks in the playbook. It will install Nginx and ensure it’s running.


Step 7 : Deploy a Simple Web Page

Once Nginx is installed, we can deploy a simple HTML page to test that everything is working.

  1. Create a sample HTML file:

     <html>
     <head><title>Welcome to My Web App</title></head>
     <body><h1>Hello, World!</h1></body>
     </html>
    
  2. Add a task to the playbook to copy this HTML file to the /var/www/html directory on all the EC2 instances.

    Add this task to your playbook:

     - name: Deploy Sample Web Page
       copy:
         content: "<html><head><title>Welcome to My Web App</title></head><body><h1>Hello, World!</h1></body></html>"
         dest: /var/www/html/index.html
    
  3. Run the playbook again:

     ansible-playbook -i /etc/ansible/hosts install_nginx.yml
    

Now, if you go to the public IP address of any EC2 instance in a web browser, you should see your "Hello, World!" page!


Conclusion : Congratulations!

You’ve just completed a hands-on project where you:

  • Created 3 EC2 instances on AWS.

  • Installed Ansible to automate tasks.

  • Wrote an Ansible playbook to install Nginx.

  • Deployed a sample web page on all your EC2 instances using Ansible.

This process has given you practical experience with DevOps automation, a key skill for managing servers and applications efficiently. Keep experimenting with different playbooks and tasks to further your knowledge in automation and cloud computing.

Happy learning! 🚀