Creating subdomain records for multi-tenant applications.
TypeScriptCreating subdomain records is a common requirement for multi-tenant applications, where each tenant has a dedicated subdomain. Pulumi allows you to manage DNS records programmatically, making it a suitable infrastructure as code tool for such a task.
We will be using Pulumi with the AWS provider to create subdomain records. The following program demonstrates how to create a DNS zone if one doesn't exist and then create a subdomain record within that zone for a multi-tenant application.
We will use two main resources:
aws.route53.Zone
: Represents a DNS zone that contains the DNS records for a domain. Here, we will create or select a pre-existing zone for the main domain.aws.route53.Record
: Represents a DNS record within a zone, which can be used to create a subdomain that points to an IP address or another DNS record (like a CNAME).
Here's the TypeScript program to set up a subdomain record:
import * as pulumi from "@pulumi/pulumi"; import * as aws from "@pulumi/aws"; // Configurations for the new subdomain. const config = new pulumi.Config(); const subdomainPrefix = config.require("subdomainPrefix"); // e.g., "tenant1" const rootDomainName = config.require("rootDomainName"); // e.g., "example.com" // Optionally, you can import an existing hosted zone using `aws.route53.getZone` if it's already set up. // For this example, we will create a new zone. const parentZone = new aws.route53.Zone("parent-zone", { name: rootDomainName, }); // Define the A record for the subdomain, pointing it to an IP address. // Here, we would typically point to the IP of the load balancer or a web server for that tenant. const subdomainRecord = new aws.route53.Record(`${subdomainPrefix}-record`, { zoneId: parentZone.id, name: `${subdomainPrefix}.${rootDomainName}`, // Subdomain name. type: "A", // Can be "A", "AAAA", "CNAME", "TXT", etc. ttl: 300, // Time to live for the DNS record in seconds. records: ["203.0.113.123"], // Replace with the IP address of the tenant's application. }); // Export the subdomain URL. export const subdomainUrl = pulumi.interpolate`http://${subdomainRecord.name}`;
In the above program, replace
"203.0.113.123"
with the correct IP address to which the subdomain should point.Here are the steps you need to follow to use the code:
- Ensure you have Pulumi installed and AWS credentials configured on your machine.
- Replace the dummy values of
subdomainPrefix
androotDomainName
with the actual values you intend to use. - Create a new Pulumi project if you haven't already done so, and place this code in the
index.ts
file. - Run
pulumi up
from the command line within the directory containing your Pulumi project. This command will provision the resources as coded.
Pulumi will output the full subdomain URL if the setup is successful. You can use this to verify that your domain is correctly pointing to the given IP. You can repeat the process for each tenant by creating a new instance of
aws.route53.Record
resource with differentsubdomainPrefix
values.