DEV Community

Cover image for Building Dynamic Subdomain Routing for User Signups in Your SaaS Product
sai parisheth
sai parisheth

Posted on

Building Dynamic Subdomain Routing for User Signups in Your SaaS Product

Recently, I had the opportunity to help a friend set up dynamic subdomain routing for his SaaS product. He already had a functioning frontend in Angular, a backend built on Node.js, and a MongoDB database. However, his goal was to enhance the user experience by assigning a unique subdomain to each user upon signup.

The existing resources were:

A domain registered with GoDaddy.
An AWS account with access to key services such as Route 53, CloudFront, EC2, and S3.
The Plan
The primary requirement was to configure dynamic subdomain routing, where each new user would receive their own subdomain, e.g., user1.example.com. This involved configuring AWS services, primarily Route 53 for DNS management, CloudFront for content distribution, and S3 for hosting the static Angular frontend.

Step 1: Setting up the Frontend
The Angular frontend was hosted on an S3 bucket, which was configured as a static website. We then used CloudFront as the content delivery network (CDN) to distribute the static assets globally.

To handle dynamic subdomains, we added a wildcard domain (*.example.com) to the CloudFront distribution. This wildcard ensures that any subdomain requests (such as user1.example.com, user2.example.com, etc.) would point to the same CloudFront distribution, allowing us to serve the same frontend while handling the unique subdomains at the DNS level.

Step 2: Configuring Route 53 for Subdomain Traffic
Next, we set up Route 53 to manage DNS records for the domain. The key here was the ability to dynamically create A or CNAME records for each user’s subdomain. For example, when a new user signs up, we programmatically added a new record such as user1.example.com pointing to the CloudFront distribution.

Each new subdomain required a corresponding DNS entry in Route 53, which would route the traffic to CloudFront. The wildcard configuration in CloudFront allowed any subdomain created in Route 53 to automatically route traffic to the frontend.

Step 3: Automating DNS Record Creation on Signup
The real challenge came with automating the creation of subdomains in Route 53 for each user. To achieve this, we integrated AWS SDK into the backend (Node.js) to programmatically create DNS records. Here’s how we approached it:

AWS Credentials: We generated an Access Key ID and Secret Access Key from AWS IAM with appropriate permissions to modify Route 53 records. These keys were securely stored in our backend system.
Automating Route 53 Updates: On every user signup, the backend would generate a unique subdomain for the user, e.g., user1.example.com. Using the AWS SDK, the backend automatically added a new DNS record in Route 53, pointing the subdomain to the CloudFront distribution. This automation eliminated manual DNS configuration, making the process scalable.
Backend Logic: The backend code handled the generation of subdomain names and ensured that they were unique. It then communicated with AWS to create the necessary DNS records in Route 53, using the secret keys we had configured.
Additional Insights
Scalability: This architecture ensures scalability. With the dynamic subdomain routing, the system could handle thousands of users, each with their own subdomain, without the need for manual DNS management. The integration of Route 53 and CloudFront provides a seamless flow of traffic and ensures optimal performance for users across different geographical regions.
Security: Using IAM roles with the least privilege principle ensured that the backend only had access to Route 53 for creating DNS records, limiting potential security risks.
Performance Optimization: The use of CloudFront with a globally distributed edge network significantly reduced latency and improved the overall performance of the application, providing a fast experience for users regardless of their location.
This project was a valuable learning experience, particularly in how to leverage AWS services like Route 53 and CloudFront for dynamic DNS management. It not only streamlined the user signup process but also ensured scalability and performance for the SaaS product as it continues to grow.

Top comments (0)