Ansible : Deploying a Node.js App on EC2 Using Ansible
Tags: devops, ansible, node, aws
Published: 2023-03-03 13:14:40

## Deploying a Node.js App on EC2 Using Ansible

Ansible is a popular open-source tool used for automation, configuration management, and orchestration of IT infrastructure. It is widely used by system administrators, DevOps engineers, and developers to simplify the deployment process of various applications on different environments. In this blog post, we will explore how to use Ansible to deploy a Node.js application on an EC2 instance.

**Requirements**

1. An AWS account with an EC2 instance launched
2. Ansible installed on your local machine
3. Basic knowledge of Ansible

Let's dive into the steps involved in deploying a Node.js app on EC2 using Ansible.

**Step 1: Setting Up the Inventory File**

The inventory file contains the list of servers or hosts that Ansible will manage. In our case, we need to specify the IP address of our EC2 instance. Here is an example of how our inventory file should look like:

```
[webservers]
1.1.1.1 ansible_ssh_private_key_file=~/.ssh/id_rsa ansible_user=root

```
This file defines a single host, with the IP address 1.1.1.1. We also specify the SSH private key file location and the user to connect to the host.

**Step 2: Writing the Ansible Playbook**

The Ansible playbook contains the instructions for Ansible to execute on the remote hosts. In our case, we need to perform three tasks: install Node.js and npm, create a new Linux user, and deploy the Node.js application. Here's how our playbook should look like:

```
---
- name: Install node and npm    # A name to identify the playbook
  hosts: 1.1.1.1                 # The target host to execute the tasks on
  tasks:                         # List of tasks to be performed
  - name: Update apt repo and cache  
    apt: update_cache=yes force_apt_get=yes cache_valid_time=3600 # Update apt repository and cache
  - name: Install nodejs and npm 
    apt:                        # Install Node.js and NPM
      pkg:
      - nodejs
      - npm

- name: Create new linux user   
  hosts: 1.1.1.1                 # The target host to execute the tasks on
  tasks:                         # List of tasks to be performed
  - name: Create linux user     
    user:                       # Create a new Linux user
      name: arun
      comment: arun admin
      group: admin

- name: Deploy nodejs app       
  hosts: 1.1.1.1                 # The target host to execute the tasks on
  become: True                   # Switch to the root user for executing tasks
  become_user: arun              # Set the user as "arun" to perform tasks
  tasks:                         # List of tasks to be performed
  - name: unpack the nodejs file  
    unarchive:                  # Unpack the Node.js app
      src:
      dest: /home/arun
  - name: Install dependencies  
    npm:                        # Install app dependencies
      path: /home/arun/packages
  - name: Start the application 
    command:                    # Start the Node.js app
      chdir: /home/arun/packages/app
      cmd: node server
    async: 1000                 # Run the command asynchronously
    poll: 0                     # Do not wait for the command to finish
  - name: Ensure app is running  
    shell: ps aux | grep node   # Check if the app is running
    register: app_status        # Register the output of the command as a variable
  - debug: msg={{app_status.stdout_lines}} # Print the output of the previous task for debugging purposes
```

## In the above playbook, we define three plays.

**Play 1: Install node and npm**

This play installs the Node.js runtime and the Node Package Manager (npm) on the remote host. It uses the `apt` module to update the apt repository and cache, and then install the `nodejs` and `npm` packages.

**Play 2: Create new Linux user**

This play creates a new user on the remote host. It uses the `user` module to create a new user with the name "arun", a comment "arun admin", and assigns the user to the "admin" group.

**Play 3: Deploy nodejs app**

This play deploys the Node.js application on the remote host. It first switches to the user "arun" using the `become_user `and `become `parameters to execute subsequent tasks as that user. It then proceeds to perform the following tasks:

- Unpack the nodejs file using the `unarchive` module.
- Install dependencies using the `npm` module.
- Start the application using the `command `module, which executes the command to start the Node.js server and sets the working directory to the application directory.
- Check if the application is running using the `shell` module to run a command that lists all processes with the name "node".
- Debug the output of the previous task using the `debug `module to print the output of the previous command.

Each play is executed sequentially, with each play building on the previous one to ultimately deploy the Node.js application on the remote host.

**Step 3: Running the Ansible Playbook** 

To run the Ansible playbook, execute the following command:

```
ansible-playbook -i inventory playbook.yml

```
This command will execute the playbook.

**Conclusion**

_This project demonstrates how Ansible can be used to automate the deployment of a Node.js application on an EC2 instance. With this playbook, you can easily and quickly deploy your Node.js application on a new server without any manual configuration._