When I started this project, I thought it would be a straightforward deployment.
It wasnโt.
From database connection errors to broken Terraform configs and missing Ansible templates, this project pushed me to think like a real DevOps engineer โ not just follow tutorials.
In this article, Iโll walk you through how I deployed a Node.js application (EpicBook) using:
Terraform โ Infrastructure provisioning
Ansible โ Configuration & deployment
AWS (EC2 + RDS) โ Hosting
PM2 + Nginx โ Production runtime
And most importantlyโฆ
๐ Iโll show you the exact commands, mistakes, and fixes so you can replicate it yourself.
๐๏ธ Architecture Overview
Hereโs what weโre building:
EC2 (Ubuntu) โ Runs the app
RDS (MySQL) โ Stores data (private subnet)
Nginx โ Reverse proxy
PM2 โ Keeps Node.js running
โ๏ธ Step 1: Provision Infrastructure with Terraform
First, I navigated into my Terraform directory:
cd terraform/aws
Then initialized Terraform:
terraform init
Validated config:
terraform validate
And applied the infrastructure:
terraform apply
โ ๏ธ First Major Problem
Error: No configuration files
๐ก Fix: I was in the wrong directory. Always ensure you're inside the folder containing .tf files.
โ ๏ธ Second Problem (Very Important)
DBSubnetGroupDoesNotCoverEnoughAZs
๐ก Fix:
RDS requires at least 2 Availability Zones.
I updated my Terraform to include:
Multiple private subnets
Different AZs
โ
Output
After success:
ec2_public_ip = "13.x.x.x"
rds_endpoint = "epicbook-db.xxxxx.amazonaws.com:3306"
๐ Step 2: Connect to EC2
ssh -i ~/.ssh/key.pem ubuntu@
โ๏ธ Step 3: Install Ansible
sudo apt update
sudo apt install ansible-core -y
Verify:
ansible --version
โ ๏ธ Error I Hit
ansible-playbook: command not found
๐ก Fix: Install Ansible (itโs not pre-installed).
๐ค Step 4: Run Ansible Playbook
cd ansible
ansible-playbook -i inventory.ini site.yml
This automated:
Nginx setup
App deployment
DB configuration
๐ฆ Step 5: Application Setup
Ansible handled:
git clone
npm install
sudo apt install nodejs npm mysql-client -y
๐ข๏ธ Step 6: Database Setup (RDS)
I created the database:
mysql -h -u admin -p -e "CREATE DATABASE bookstore;"
โ ๏ธ Big Issue #1
ECONNREFUSED 127.0.0.1:3306
๐ก Cause:
App was trying to connect to localhost
๐ก Fix:
Passed environment variables:
DB_HOST โ RDS endpoint
DB_NAME โ bookstore
โ๏ธ Step 7: Configure App
Using Ansible template:
template:
src: config.json.j2
dest: /var/www/epicbook/config/config.json
โ ๏ธ Error
config.json.j2 not found
๐ก Fix:
Create the file here:
roles/epicbook/templates/config.json.j2
๐ Step 8: Run App with PM2
npm install -g pm2
pm2 start server.js --name epicbook
pm2 save
โ ๏ธ Issue
pm2 delete epicbook โ not found
๐ก Fix:
ignore_errors: true
๐๏ธ Step 9: Database Schema & Seeding
This was the trickiest part.
Run schema:
mysql -h -u admin -p bookstore < BuyTheBook_Schema.sql
Seed data:
mysql -h -u admin -p bookstore < author_seed.sql
mysql -h -u admin -p bookstore < books_seed.sql
โ ๏ธ Issue #1
Table 'books' doesn't exist
๐ก Fix:
Schema must run before seeding
โ ๏ธ Issue #2
Unknown database 'bookstore'
๐ก Fix:
Standardised DB name across:
Terraform
Ansible
SQL files
โ ๏ธ Issue #3
Table already exists
๐ก Fix:
Make tasks idempotent:
ignore_errors: yes
๐ Final Result
I opened the app:
http://
โ
And finallyโฆ
๐ Books were displaying from the database
That moment? Worth every error.
๐ง What This Project Taught Me
Terraform is for infrastructure, not configuration
Ansible eliminates manual setup (when done right)
Order matters:
DB โ Config โ App โ Seed
Debugging is a core DevOps skill
Small misconfigurations (like DB name) can break everything
๐ก What Iโd Improve Next
Use Ansible MySQL modules instead of shell
Add Load Balancer (ALB)
Implement Auto Scaling
Store secrets in AWS Secrets Manager
Add CI/CD pipeline
๐ Final Thoughts
This wasnโt just a deployment project.
It was a real-world DevOps experience:
Broken configs
Debugging under pressure
Fixing issues step by step
And in the endโฆ building something that actually works in production.
๐ If you're learning DevOps
Donโt just follow tutorials.
๐ Break things
๐ Fix them
๐ Understand why
Thatโs how you grow.













