Using aws route53 with docdb
TypeScriptAWS Route 53 is a scalable and highly available Domain Name System (DNS) web service, while Amazon DocumentDB (with MongoDB compatibility) is a scalable, fully-managed NoSQL database service. To use AWS Route 53 with Amazon DocumentDB, you generally want to create a Route 53 DNS record that points to the DocumentDB instance or cluster endpoint. This allows applications to use a human-readable domain name to connect to the database.
Below, I'm going to guide you through how to use Pulumi to create a Route 53 record for a DocumentDB cluster. We will set up a DocumentDB cluster and then create a Route 53 hosted zone (if you don't already have one), and finally add an
A
record pointing to the DocumentDB cluster's endpoint.First, we will define the necessary components:
- Amazon DocumentDB cluster.
- Route 53 hosted zone.
- An
A
record within the hosted zone pointing to the DocumentDB cluster endpoint.
Here is the TypeScript code to accomplish this:
import * as pulumi from "@pulumi/pulumi"; import * as aws from "@pulumi/aws"; // Create a DocumentDB cluster const docdbCluster = new aws.docdb.Cluster("myDocdbCluster", { clusterIdentifier: "my-docdb-cluster", engine: "docdb", masterUsername: "myusername", masterPassword: "mypassword", skipFinalSnapshot: true, }); // Register a new domain to be managed by Route53. // If you have a domain name already, you can skip this step. const exampleZone = new aws.route53.Zone("exampleZone", { name: "example.com", }); // We must wait for the DocumentDB Cluster to be available before we can get its endpoint. // This is an output property that will only be available once the cluster has been provisioned. const docdbClusterEndpoint = docdbCluster.endpoint.apply(endpoint => endpoint); // Create a DNS record set to point to the DocumentDB cluster endpoint using the Route 53 Zone's name servers. const docdbDnsRecord = new aws.route53.Record("docdbDnsRecord", { zoneId: exampleZone.zoneId, name: "docdb.example.com", type: "CNAME", ttl: 300, records: [docdbClusterEndpoint], // We use the actual endpoint of the DocumentDB cluster here. }); // Export the names of the resources export const docdbClusterIdentifier = docdbCluster.clusterIdentifier; export const dnsName = docdbDnsRecord.name; export const hostedZoneId = exampleZone.zoneId;
This program does the following:
- Creates a new Amazon DocumentDB cluster with a specified identifier, username, and password.
- Creates a new hosted zone with Route 53, responsible for managing the DNS records for a specified domain. Replace
"example.com"
with your own domain. - Waits for the DocumentDB cluster to provide its endpoint.
- Creates a DNS
CNAME
record in the hosted zone that points to the DocumentDB cluster's endpoint. This record enables the use of a friendly domain name instead of an opaque endpoint URL.
After running this script with Pulumi, you will have a Route 53 DNS record (
docdb.example.com
) that you can use to connect to your DocumentDB instance. Remember to replace the placeholder values such as"example.com"
,"myusername"
, and"mypassword"
with real values that are relevant to your use case.Keep in mind that hardcoding passwords, as shown in this example, is not recommended for production scenarios. Instead, consider using a secret management system like AWS Secrets Manager or Pulumi secrets.
The
exports
at the end of the script provide outputs that you can use to reference the created resources' properties outside of Pulumi. For example, you might need thehostedZoneId
to configure domain registrars or elsewhere in your infrastructure.