AWS CDK : snapshot restore an existing RDS Aurora database cluster?

Viewed 890

I'd like to restore an Aurora PostgreSQL cluster from a snapshot. The cluster was created using the AWS CDK rds.DatabaseCluster construct.

The CDK version is 1.139.0 using Typescript and the db cluster is 11.9

For an existing cluster created with rds.DatabaseCluster there doesn't appear to be a way to specify a snapshot should you wish to trigger a snapshot restore of the cluster through CDK.

In the past I have restored clusters that have been deployed using CloudFormation (CF) by adding the snapshotIdentifier to the AWS::DB::Cluster resource in the CF template. This property can be seen in the CDK CfnDBCluster & CfnDBInstance resources.

I'm aware of the rds.DatabaseClusterFromSnapshot construct which offers the ability to create the database (and restore?) by specifying a snapshot name. However as mentioned the database cluster that I'd like to restore has already been created and is associated in CDK with the rds.DatabaseCluster constuct.

I'd rather not restore the database cluster outside of CDK (using console/cli) as the new cluster this results in would not be associated with the CDK stack.

Is it possible to perform a snapshot restore of a RDS Aurora PostgreSQL cluster in anyway purely from within the existing CDK stack/code? Specifically if the cluster was created using the rds.DatabaseCluster construct?

Thank you

1 Answers

You can access the underlying CloudFormation resource as an L1 construct.

const cluster = new rds.DatabaseCluster(this, 'Database', {
    engine: rds.DatabaseClusterEngine.AURORA,
    instanceProps: { vpc },
});
const cfnCluster = cluster.node.defaultChild as rds.CfnDBCluster;
cfnCluster.snapshotIdentifier = "arn:snapshot";
cfnCluster.masterUsername = undefined;
cfnCluster.masterUserPassword = undefined;

Updating this value would terminate your cluster and create a new one to replace it.

Parameter documentation: https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-rds.CfnDBCluster.html#snapshotidentifier

Edit: Updated to set masterUsername and masterUserPassword to undefined

Related