DEV Community

Cover image for 🚀 Automating Subdomains & Databases with AWS Route 53, FastAPI, and NGINX
Zed
Zed

Posted on

🚀 Automating Subdomains & Databases with AWS Route 53, FastAPI, and NGINX

Managing multi-tenant SaaS apps can get tricky.

Each tenant usually needs:

  • A unique subdomain
  • A dedicated database
  • A scalable backend to handle requests

Manually setting up subdomains and databases is painful. So… I automated it. 😎

In this guide, I’ll walk you through how I built an automatic subdomain + database provisioning system using:

  • AWS Route 53 → Manages subdomains
  • FastAPI → Handles tenant onboarding
  • MySQL → Stores tenant-specific data
  • NGINX → Routes traffic based on subdomain
  • Boto3 → Talks to AWS from Python

Let's dive in! 🏊‍♂️


🌐 Step 1 — Automate Subdomain Creation with Route 53

First, create a hosted zone in AWS Route 53 for your domain.

Install Boto3:

pip install boto3
Enter fullscreen mode Exit fullscreen mode

Python Function to Create Subdomain:

import boto3

def create_subdomain(subdomain: str):
    route53 = boto3.client('route53', region_name='ap-south-1')
    hosted_zone_id = "ZXXXXXXXXXXX"  # Replace with your Hosted Zone ID
    domain_name = "example.com"

    response = route53.change_resource_record_sets(
        HostedZoneId=hosted_zone_id,
        ChangeBatch={
            "Changes": [
                {
                    "Action": "CREATE",
                    "ResourceRecordSet": {
                        "Name": f"{subdomain}.{domain_name}",
                        "Type": "A",
                        "TTL": 300,
                        "ResourceRecords": [{"Value": "YOUR_SERVER_PUBLIC_IP"}]
                    }
                }
            ]
        }
    )
    return response
Enter fullscreen mode Exit fullscreen mode

This automatically registers a new subdomain inside Route 53.


🛢️ Step 2 — Create Tenant Databases Dynamically

Each tenant gets a separate MySQL database for better isolation.

import mysql.connector

def create_tenant_db(tenant_name: str):
    conn = mysql.connector.connect(
        host="localhost",
        user="root",
        password="your_password"
    )
    cursor = conn.cursor()
    cursor.execute(f"CREATE DATABASE IF NOT EXISTS {tenant_name}_db")
    conn.commit()
    conn.close()
Enter fullscreen mode Exit fullscreen mode

⚡ Step 3 — FastAPI Endpoint for Tenant Signup

Whenever someone registers, we:

  1. Create their database
  2. Create their subdomain
  3. Return their live URL 🚀
from fastapi import FastAPI

app = FastAPI()

@app.post("/register")
async def register_tenant(tenant: str):
    create_tenant_db(tenant)
    create_subdomain(tenant)
    return {"status": "success", "subdomain": f"{tenant}.example.com"}
Enter fullscreen mode Exit fullscreen mode

🌍 Step 4 — Configure NGINX for Subdomains

We want all subdomains like client1.example.com or client2.example.com to hit the same backend, but serve different data.

Add this to your Nginx config:

server {
    listen 80;
    server_name ~^(?<subdomain>.+)\.example\.com$;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
Enter fullscreen mode Exit fullscreen mode

Then reload NGINX:

sudo nginx -t
sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

🏗️ Final Architecture

Signup → FastAPI → Route 53 → NGINX → Tenant DB → Tenant-Specific App

This gives you:

  • ✅ Automatic subdomain creation
  • ✅ Automatic database provisioning
  • ✅ Scalable multi-tenant architecture

🎯 Conclusion

With AWS Route 53, FastAPI, MySQL, and NGINX, we built a SaaS-ready system that provisions subdomains and databases automatically.

This approach works great for multi-tenant apps, platforms, and SaaS dashboards — and saves hours of manual setup.


💡 Pro Tip: You can even take it further and integrate AWS ACM for automatic SSL certificates per subdomain.

What do you think? Would you like me to share a guide on auto SSL + HTTPS for all subdomains next? 🔐

Top comments (0)