3

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 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the suggestion Kaustubh. Unfortunately providing the snapshot identifier by means of the L1 construct results in an error and rollback of the stack. There is conflict we the properties set by the higher level L2 construct used to create the database originally.</br> The L2 rds.DatabaseCluster is setting the MasterUsername property for the cluster. MasterUsername is incompatible with SnapshotIdentifier.
In the CF template created by the L2 construct you can see that MasterUsername is set using a CDK generated value. When setting the snapshot identifier using the L1 construct as you suggest the CDK diff looks good and shows that the cluster will be replaced as expected with resources that use the CF replacement strategy. Sadly when I deploy the stack it errors because of the incompatibility between properties described above. "Both SnapshotIdentifier and MasterUsername cannot be specified"
@mStack I updated my answer to set both fields to undefined. Since RDS clusters are somewhat expensive and take time to launch and destroy, I cannot actually test my answer beyond doing cdk synth. Hopefully with the latest changes there are no more issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.