Created
October 4, 2024 17:46
-
-
Save izakfilmalter/a5921254f7d4046f369bd28e2b6053d3 to your computer and use it in GitHub Desktop.
Multi region rds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Config values | |
const primaryRegion = "us-east-1"; | |
const replicaRegion = "us-west-2"; | |
const dbName = "exampledb"; | |
const dbUser = "exampleuser"; | |
const dbPassword = "examplepassword"; | |
// VPC Configs for primary and replica regions | |
const primaryVpcId = "vpc-id-in-primary-region"; // Replace with your VPC ID | |
const replicaVpcId = "vpc-id-in-replica-region"; // Replace with your VPC ID | |
// Primary RDS Subnet Group | |
const primaryDbSubnetGroup = new aws.rds.SubnetGroup("primary-db-subnet-group", { | |
subnetIds: aws.ec2.getSubnetIds({ vpcId: primaryVpcId }).then(result => result.ids), | |
tags: { Name: "Primary DB subnet group" }, | |
}); | |
// Primary RDS Cluster in the primary region | |
const primaryDb = new aws.rds.Cluster("primary-rds-cluster", { | |
engine: "aurora-postgresql", | |
masterUsername: dbUser, | |
masterPassword: dbPassword, | |
dbSubnetGroupName: primaryDbSubnetGroup.name, | |
skipFinalSnapshot: true, | |
availabilityZones: ["us-east-1a", "us-east-1b"], | |
}); | |
// Primary region replica instance (us-east-1) | |
const primaryReplica = new aws.rds.ClusterInstance("primary-rds-replica", { | |
clusterIdentifier: primaryDb.id, | |
instanceClass: "db.r5.large", | |
engine: "aurora-postgresql", | |
engineVersion: "13.6", | |
availabilityZone: "us-east-1b", | |
dbSubnetGroupName: primaryDbSubnetGroup.name, | |
}); | |
// Multi-region setup: Replica in another region (us-west-2) | |
const replicaDbSubnetGroup = new aws.rds.SubnetGroup("replica-db-subnet-group", { | |
subnetIds: aws.ec2.getSubnetIds({ vpcId: replicaVpcId }).then(result => result.ids), | |
tags: { Name: "Replica DB subnet group" }, | |
}, { provider: new aws.Provider("aws-replica", { region: replicaRegion }) }); | |
const multiRegionReplica = new aws.rds.ClusterInstance("multi-region-rds-replica", { | |
clusterIdentifier: primaryDb.id, | |
instanceClass: "db.r5.large", | |
engine: "aurora-postgresql", | |
engineVersion: "13.6", | |
dbSubnetGroupName: replicaDbSubnetGroup.name, | |
availabilityZone: "us-west-2a", | |
}, { provider: new aws.Provider("aws-replica", { region: replicaRegion }) }); | |
// Route 53 Hosted Zone for your domain | |
const hostedZone = aws.route53.getZone({ name: "example.com" }); // Replace with your domain | |
// Latency-based DNS records for Route 53 | |
// Latency-based routing to the primary region replica (us-east-1) | |
const primaryReplicaRecord = new aws.route53.Record("primaryReplicaRecord", { | |
zoneId: hostedZone.then(zone => zone.zoneId), | |
name: "mydb.example.com", // Your custom domain | |
type: "CNAME", | |
ttl: 60, | |
records: [primaryReplica.endpoint], // Endpoint from primary region replica | |
setIdentifier: "us-east-1-latency", | |
latencyRoutingPolicy: { | |
region: "us-east-1", | |
}, | |
}); | |
// Latency-based routing to the multi-region replica (us-west-2) | |
const multiRegionReplicaRecord = new aws.route53.Record("multiRegionReplicaRecord", { | |
zoneId: hostedZone.then(zone => zone.zoneId), | |
name: "mydb.example.com", // Your custom domain | |
type: "CNAME", | |
ttl: 60, | |
records: [multiRegionReplica.endpoint], // Endpoint from multi-region replica | |
setIdentifier: "us-west-2-latency", | |
latencyRoutingPolicy: { | |
region: "us-west-2", | |
}, | |
}); | |
// Output the endpoints and DNS setup | |
export const primaryEndpoint = primaryDb.endpoint; | |
export const primaryReplicaEndpoint = primaryReplica.endpoint; | |
export const multiRegionReplicaEndpoint = multiRegionReplica.endpoint; | |
export const dnsName = "mydb.example.com"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment