DEV Community

Atul Vishwakarma
Atul Vishwakarma

Posted on

Building a Production-Ready 2-Tier Architecture on AWS with Terraform

From Theory to Real-World Infrastructure πŸš€

As part of my 30 Days of AWS Terraform challenge, Day 22 was a major milestone where I moved beyond individual resources and built a production-style 2-tier architecture using Terraform.

This project was all about combining networking, security, compute, and database layers into a cohesive and secure system β€” just like real-world applications.


πŸ—οΈ The Goal: Build a Secure 2-Tier Architecture

The objective of this project was to design and deploy:

  • Web Tier (Public Layer) β†’ EC2 instances running a Flask application
  • Database Tier (Private Layer) β†’ MySQL RDS instance

These two layers communicate securely while maintaining strict isolation from public access.


πŸ” Architecture Overview

Web Tier (Public Subnet)

  • EC2 instance running Flask app
  • Accessible via Internet Gateway
  • Uses User Data for automated setup

Database Tier (Private Subnet)

  • Amazon RDS (MySQL)
  • No public access
  • Only accessible from Web Tier via Security Groups

βš™οΈ Core Infrastructure Components

To support this architecture, I provisioned:

🌐 Networking

  • Custom VPC
  • Public & Private subnets
  • Internet Gateway (for web tier)
  • NAT Gateway (for private subnet outbound access)

πŸ”’ Security

  • Security Groups with strict rules
  • Principle of Least Privilege enforced

πŸ”‘ Secrets Management

  • AWS Secrets Manager for DB credentials
  • No hardcoded sensitive data

πŸ–₯️ Compute

  • EC2 instance with Flask app
  • Automated setup via User Data

πŸ—„οΈ Database

  • RDS MySQL instance in private subnet

🧩 Modular Terraform Architecture

One of the biggest highlights of Day 22 was applying Terraform Modules in a real project.

Instead of one large configuration file, I structured my code into reusable modules:

Module Structure:

  • VPC Module β†’ Networking setup
  • Security Group Module β†’ Access control
  • RDS Module β†’ Database provisioning
  • Secrets Module β†’ Credential management
  • EC2 Module β†’ Web server deployment

πŸ”„ Data Flow Between Modules

Terraform modules don’t communicate directly β€” so I used:

  • outputs.tf β†’ to expose values
  • variables.tf β†’ to pass inputs

Example:

  • VPC module outputs subnet IDs
  • Root module passes them to EC2 & RDS modules
  • Secrets module outputs credentials used by EC2

This pattern ensures:

βœ”οΈ Loose coupling
βœ”οΈ High reusability
βœ”οΈ Clean architecture


πŸ§ͺ Testing the Application

After deployment, I validated the setup by hitting:

  • /health β†’ Application health check
  • /db/info β†’ Database connectivity

Seeing the Flask app successfully connect to the RDS instance was a huge win! πŸŽ‰


πŸ” Security Best Practices Implemented

This project reinforced critical security principles:

βœ”οΈ Private Database

  • RDS placed in private subnet
  • No direct internet exposure

βœ”οΈ Controlled Access

  • Only EC2 security group can access DB

βœ”οΈ Secrets Management

  • Credentials stored in AWS Secrets Manager

βœ”οΈ Least Privilege

  • Minimal permissions across components

πŸ’‘ Key Learnings from Day 22

  • Modules are essential for scalable Terraform projects
  • Networking design is critical for security
  • Secrets should never be hardcoded
  • Private subnets protect sensitive resources
  • Testing validates real-world readiness

🧠 Why This Project Matters

This was not just another Terraform lab β€” it was a real-world architecture simulation.

It taught me how to:

  • Design secure systems
  • Structure modular IaC
  • Connect application & database layers
  • Apply DevOps best practices

This is the kind of project that bridges the gap between learning and real engineering.


πŸš€ What’s Next?

With only 8 days remaining, I’m excited to explore:

  • CI/CD integration
  • Advanced security practices
  • Multi-environment deployments

🎯 Final Thoughts

Day 22 was one of the most practical and rewarding days of this challenge.

It reinforced that:

πŸ‘‰ Good infrastructure is not just functional β€” it’s secure, modular, and scalable.

If you're learning Terraform, I highly recommend building projects like this to truly understand how cloud systems work.

Top comments (0)