Deploying and Configuring Web Servers on AWS EC2 Instances with Ansible.

Deploying and Configuring Web Servers on AWS EC2 Instances with Ansible.

Introduction

In this blog, we will walk through the process of deploying and configuring an Apache web server on AWS EC2 instances using Ansible. This automation tool allows us to define infrastructure as code, making it easier to manage and scale. You can find the codes for this project on my Github,Link Below. Let's Start-
github.com/prashant235913/ansiblelearning.git

Project Structure

The project has the following structure:

.
├── inventory.ini
├── playbooks
│   └── main.yml
├── roles
│   └── software
│       ├── handlers
│       │   └── main.yml
│       └── tasks
│           └── main.yml
└── success.html

1. inventory.ini

This file defines the inventory of hosts, specifying the target EC2 instances:

[ec2-instances]
34.220.163.76 ansible_user=ubuntu ansible_ssh_private_key_file=/home/prashant/.ssh/id_rsa.pem

Replace the IP address and key file path with your own EC2 instance details.

2. playbooks/main.yml

This playbook orchestrates the deployment and configuration tasks:

---
- hosts: ec2-instances
  become: yes
  roles:
    - ./roles/software
  vars:
    ansible_ssh_private_key_file: "~/.ssh/id_rsa.pem"

This playbook targets the hosts in the "ec2-instances" group, uses privilege escalation (become: yes), includes the "software" role, and sets the SSH private key file path.

3. roles/software/tasks/main.yml

This file within the "software" role contains tasks to install Apache, copy website files, and start the Apache service:

---
- name: Install Apache web server
  package:
    name: apache2
    state: present

- name: Copy website files
  copy:
    src: /home/prashant/ansiblelearning/success.html
    dest: /var/www/html

- name: Copy success page
  copy:
    src: /home/prashant/ansiblelearning/success.html
    dest: /var/www/html
  notify:
    - Restart Apache service

- name: Start Apache service
  systemd:
    name: apache2
    state: started
    enabled: yes

These tasks install Apache, copy a success page to the web directory, and start the Apache service. A notification is set to restart the Apache service when the success page is copied.

4. roles/software/handlers/main.yml

This handler file restarts the Apache service:

---
- name: Restart Apache service
  systemd:
    name: apache2
    state: restarted
    enabled: yes

- name: Show Website URL
  debug:
    msg: "Website URL: http://{{ ansible_host }}/success.html"

This file helps you in restaring the webserver when it is called i.e. in the previous block 'notify:' under the 'Copy Success Page' . After that, 'name: show website url ' will help you in giving a output link that you can use to access the website.

5. success.html

This HTML file is the success page that will be served by the Apache web server.

!DOCTYPE html>

<html lang="en">


<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My DevOps Project</title></head>
<body>
 <h1>Welcome to My DevOps Project!</h1>
 <!-- Animated Pikachu Emoji --> <img src="https://media.tenor.com/b87Ur_ijFF0AAAAi/char.gif" alt="Animated Pikachu Emoji">
 <p>Deployment and configuration were successful!</p>
</body>
</html>

Commands and Execution

  1. Run the Ansible Playbook:

     ansible-playbook -i inventory.ini playbooks/main.yml
    

    This command executes the main playbook, deploying and configuring Apache on the specified EC2 instances.After Applying the above command ,it'll show some output like this and that means you're successful in your journey of migrating a web server to aws-ec2-instance using Ansible.For seeing your website you can use the next step.

  2. Access the Website:

    After successful execution, you can access the deployed website by visiting:

    http://<your-instance-ip>/success.html .

    Replace <your-instance-ip> with the actual IP address of your EC2 instance

Help Needed

As you can see, in this file - roles/software/handlers/main.yml.

i.e. Number 4 in the blog ,I tried to print the website accessing link but in the output you can see it's not printed .If you helped me in solving this problem,I'll give you a shoutout in any of my blog.Thanks.

Conclusion

With Ansible, deploying and configuring a web server on multiple instances becomes a streamlined process. I,ve learnt alot while making this project and looking forward to grasp some more challenging topics like this.