Last active
February 7, 2024 16:47
-
-
Save joshbalfour/385b0bbfa270e43078cda8468799b67a to your computer and use it in GitHub Desktop.
RDS Global Database Cluster CDK example
This file contains 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
import { DatabaseCluster, CfnGlobalCluster, DatabaseClusterProps, CfnDBCluster } from 'aws-cdk-lib/aws-rds' | |
import { Construct } from 'constructs' | |
export class GlobalDatabaseCluster extends DatabaseCluster { | |
public readonly globalCluster: CfnGlobalCluster | |
constructor(scope: Construct, id: string, props: DatabaseClusterProps) { | |
super(scope, id, props) | |
this.globalCluster = new CfnGlobalCluster(this, 'global-cluster', { | |
sourceDbClusterIdentifier: this.clusterIdentifier, | |
globalClusterIdentifier: `${scope.node.path}-${id}`, | |
}) | |
} | |
addRegionalCluster(scope: Construct, id: string, props: Omit<DatabaseClusterProps, 'credentials' | 'secret'>) { | |
const regionalCluster = new DatabaseCluster(scope, id, props) | |
const regionalClusterCfn = regionalCluster.node.defaultChild as CfnDBCluster | |
regionalClusterCfn.globalClusterIdentifier = this.globalCluster.globalClusterIdentifier | |
regionalClusterCfn.masterUsername = undefined | |
regionalClusterCfn.masterUserPassword = undefined | |
regionalClusterCfn.masterUserSecret = undefined | |
if (regionalCluster.secret) { | |
regionalCluster.secret.node.tryRemoveChild('Attachment') | |
regionalCluster.node.tryRemoveChild('Secret') | |
} | |
return regionalCluster | |
} | |
} |
This file contains 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
// app: cdk.App | |
// vpc: IVpc | |
// usProps: DatabaseClusterProps | |
// ukProps: Omit<DatabaseClusterProps, 'credentials' | 'secret'> | |
const stackUS = new cdk.Stack(app, 'us', { | |
region: 'us-east-1', | |
}) | |
const stackUK = new cdk.Stack(app, 'uk', { | |
region: 'eu-west-2', | |
}) | |
const globalDb = new GlobalDatabaseCluster(stackUS, 'global-db', usProps) | |
const regionalCluster = globalDb.addRegionalCluster(stackUK, 'uk-db', ukProps) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment