Skip to content

Instantly share code, notes, and snippets.

@joshbalfour
Last active February 7, 2024 16:47
Show Gist options
  • Save joshbalfour/385b0bbfa270e43078cda8468799b67a to your computer and use it in GitHub Desktop.
Save joshbalfour/385b0bbfa270e43078cda8468799b67a to your computer and use it in GitHub Desktop.
RDS Global Database Cluster CDK example
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
}
}
// 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