DEV Community

Cover image for πŸš€ From Frustration to Production: Deploying a Full-Stack App with Terraform & Ansible on AWS
Chioma Nwosu
Chioma Nwosu

Posted on

πŸš€ From Frustration to Production: Deploying a Full-Stack App with Terraform & Ansible on AWS

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.

Top comments (0)