DEV Community

Cover image for Effortless Deployment of Project with Ansible & Nginx on AWS
Amash Ansari
Amash Ansari

Posted on

Effortless Deployment of Project with Ansible & Nginx on AWS

Prerequisite

  1. Before you start, you should already know how to write Ansible playbooks well.
  2. You should have some basic knowledge of Nginx.
  3. You should be comfortable with AWS EC2.

If you're new to AWS EC2, check out this beginner-friendly guide. For more information about Ansible, click here.

Create the EC2 Instances

Set up two EC2 instances for this project: one as the control (worker) node, managed by Ansible, and the other as the managed (worker) node. The control node will use playbooks to configure the managed node and automate the entire deployment process.

Image description

Let's write the playbook

What we're going to do is create a playbook that installs Nginx and starts the service on the worker node. This playbook will be set up and controlled by the control node. Here's the playbook:

-
  name: This is a simple HTML project
  hosts: servers             #Group name that will host this playbook
  become: yes                #Giving sudo privileges
  tasks:
    - name: nginx-install    #This will install the nginx
      apt:
        name: nginx
        state: latest

    - name: nginx-start
      service:                #This will start and enable the nginx service
        name: nginx
        state: started
        enabled: yes

    - name: deploy-app
      copy:
        src: ../index.html     #Give path to the file
        dest: /var/www/html/   #Nginx will serve our file from this specific location

Enter fullscreen mode Exit fullscreen mode

Our next task is straightforward: run this playbook on the control node using the terminal and wait for Ansible to configure and deploy an end-to-end application with a single click.

If you'd like to access my "index.html" file, just click here.

Execute the playbook

To run the playbook, use the following command:

ansible-playbook <playbook-name>
Enter fullscreen mode Exit fullscreen mode

You'll see some output similar to this:

Image description

Check the project by accessing

Finally, ensure the project is up and running by accessing it on the default Nginx port, which is 80.

Image description

Top comments (0)