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

Tools: πŸš€ 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. 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 provisioningAnsible β†’ Configuration & deploymentAWS (EC2 + RDS) β†’ HostingPM2 + 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 appRDS (MySQL) β†’ Stores data (private subnet)Nginx β†’ Reverse proxyPM2 β†’ Keeps Node.js runningβš™οΈ Step 1: Provision Infrastructure with Terraform First, I navigated into my Terraform directory: Then initialized Terraform: And applied the infrastructure: terraform apply⚠️ First Major ProblemError: 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 subnetsDifferent AZsβœ… Output ec2_public_ip = "13.x.x.x"rds_endpoint = "epicbook-db.xxxxx.amazonaws.com:3306"πŸ” Step 2: Connect to EC2ssh -i ~/.ssh/key.pem ubuntu@βš™οΈ Step 3: Install Ansiblesudo apt updatesudo apt install ansible-core -y ansible --version⚠️ Error I Hitansible-playbook: command not found πŸ’‘ Fix: Install Ansible (it’s not pre-installed). πŸ€– Step 4: Run Ansible Playbookcd ansibleansible-playbook -i inventory.ini site.yml Nginx setupApp deploymentDB configurationπŸ“¦ Step 5: Application Setup git clone npm installsudo 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 #1ECONNREFUSED 127.0.0.1:3306 πŸ’‘ Cause:App was trying to connect to localhost Passed environment variables:DB_HOST β†’ RDS endpointDB_NAME β†’ bookstoreβš™οΈ Step 7: Configure App Using Ansible template: template: src: config.json.j2 dest: /var/www/epicbook/config/config.json⚠️ Errorconfig.json.j2 not found πŸ’‘ Fix:Create the file here: roles/epicbook/templates/config.json.j2πŸš€ Step 8: Run App with PM2npm install -g pm2pm2 start server.js --name epicbookpm2 save⚠️ Issuepm2 delete epicbook β†’ not found ignore_errors: trueπŸ—„οΈ Step 9: Database Schema & Seeding This was the trickiest part. mysql -h -u admin -p bookstore < BuyTheBook_Schema.sql mysql -h -u admin -p bookstore < author_seed.sqlmysql -h -u admin -p bookstore < books_seed.sql⚠️ Issue #1Table 'books' doesn't exist πŸ’‘ Fix:Schema must run before seeding ⚠️ Issue #2Unknown database 'bookstore' Standardised DB name across:TerraformAnsibleSQL files⚠️ Issue #3Table already exists πŸ’‘ Fix:Make tasks idempotent: ignore_errors: yesπŸŽ‰ Final Result βœ… And finallyβ€¦πŸ“š Books were displaying from the database That moment? Worth every error. 🧠 What This Project Taught MeTerraform is for infrastructure, not configurationAnsible eliminates manual setup (when done right) DB β†’ Config β†’ App β†’ SeedDebugging is a core DevOps skillSmall misconfigurations (like DB name) can break everythingπŸ’‘ What I’d Improve NextUse Ansible MySQL modules instead of shellAdd Load Balancer (ALB)Implement Auto ScalingStore secrets in AWS Secrets ManagerAdd CI/CD pipelineπŸš€ Final Thoughts This wasn’t just a deployment project. It was a real-world DevOps experience: Broken configsDebugging under pressureFixing 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 Templates let you quickly answer FAQs or store snippets for re-use. as well , this person and/or