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:
- Create their database
- Create their subdomain
- 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

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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.