πŸš€ 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 gu…


This content originally appeared on DEV Community and was authored by Zed

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

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

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()

⚑ 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"}

🌍 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;
    }
}

Then reload NGINX:

sudo nginx -t
sudo systemctl reload nginx

πŸ—οΈ 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? πŸ”


This content originally appeared on DEV Community and was authored by Zed


Print Share Comment Cite Upload Translate Updates
APA

Zed | Sciencx (2025-08-28T17:37:47+00:00) πŸš€ Automating Subdomains & Databases with AWS Route 53, FastAPI, and NGINX. Retrieved from https://www.scien.cx/2025/08/28/%f0%9f%9a%80-automating-subdomains-databases-with-aws-route-53-fastapi-and-nginx/

MLA
" » πŸš€ Automating Subdomains & Databases with AWS Route 53, FastAPI, and NGINX." Zed | Sciencx - Thursday August 28, 2025, https://www.scien.cx/2025/08/28/%f0%9f%9a%80-automating-subdomains-databases-with-aws-route-53-fastapi-and-nginx/
HARVARD
Zed | Sciencx Thursday August 28, 2025 » πŸš€ Automating Subdomains & Databases with AWS Route 53, FastAPI, and NGINX., viewed ,<https://www.scien.cx/2025/08/28/%f0%9f%9a%80-automating-subdomains-databases-with-aws-route-53-fastapi-and-nginx/>
VANCOUVER
Zed | Sciencx - » πŸš€ Automating Subdomains & Databases with AWS Route 53, FastAPI, and NGINX. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/08/28/%f0%9f%9a%80-automating-subdomains-databases-with-aws-route-53-fastapi-and-nginx/
CHICAGO
" » πŸš€ Automating Subdomains & Databases with AWS Route 53, FastAPI, and NGINX." Zed | Sciencx - Accessed . https://www.scien.cx/2025/08/28/%f0%9f%9a%80-automating-subdomains-databases-with-aws-route-53-fastapi-and-nginx/
IEEE
" » πŸš€ Automating Subdomains & Databases with AWS Route 53, FastAPI, and NGINX." Zed | Sciencx [Online]. Available: https://www.scien.cx/2025/08/28/%f0%9f%9a%80-automating-subdomains-databases-with-aws-route-53-fastapi-and-nginx/. [Accessed: ]
rf:citation
» πŸš€ Automating Subdomains & Databases with AWS Route 53, FastAPI, and NGINX | Zed | Sciencx | https://www.scien.cx/2025/08/28/%f0%9f%9a%80-automating-subdomains-databases-with-aws-route-53-fastapi-and-nginx/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.