Dynamically Create Subdomains for SaaS Application
SaaS platform generally has multiple tenants. General practice is to provide a dedicated subdomain for each tenant. For eg.
<tenant-name>.<domain>.com. This article describes how it can be achieved via AWS Route 53 wildcard DNS entry.
Implementing Dynamic subdomain has several challenges
- How the new subdomain will be configured and published in DNS?
- How does the SSL certificate gets signed for every new subdomain?
- How the new subdomain is configured and published on Webserver?
- How does the SaaS web-tier know about the new subdomain?
wildcard DNS record. A wildcard DNS record matches any subdomain of that domain name.
*.onlinetutz.com -> 18.104.22.168
This means any subdomain for
onlinetutz.com will now resolve to
22.214.171.124. For eg:
Creating Wildcard DNS in AWS Route 53
- Create a Public/Private Hosted Zone.
- Create a recordset with
*in the above-hosted zone and map it to the server or ELB.
Testing the Setup
For testing the setup, below python script which is based on Flask framework can be used.
from flask import Flask app = Flask(__name__) app.config['SERVER_NAME'] = 'onlinetutz.com' @app.route('/', subdomain="<tenant>") def index(tenant): return "Tenant Name:" + tenant if __name__ == '__main__': app.run(host='0.0.0.0',port=80)
From the below output we can confirm that we are able to route to subdomain on-the-fly and also fetch the information regarding the subdomain.
[ubuntu@ip-12-11-52-34 ~]$ curl http://facebook.onlinetutz.com Tenant Name:facebook [ubuntu@ip-12-11-52-34 ~]$ curl http://google.onlinetutz.com Tenant Name:google [ubuntu@ip-12-11-52-34 ~]$ curl http://twitter.onlinetutz.com Tenant Name:twitter