1

Here's the scenario - I have a CloudFormation Stack which contains a web app. The Stack has an RDS instance and an ECS Cluster & Service which rely on it. I'm working with the Python form of the CDK.

In the production environment, if I'm creating the Stack, I'd want the RDS instance to start itself from a snapshot. That's fine and I can use rds.DatabaseInstanceFromSnapshot. But on subsequent deploys to that Stack, I don't want the DB to re-sync to a snapshot - since it's the production environment, it should now be the most up-to-date data I have and I wouldn't want it to be rolled back. However, I do still want to have that DB instance be able to be referred to and managed in the code. For example, if I want to change whether minor updates are automatically allowed, I want to be able to change that flag on the instance and deploy the Stack and have that picked up.

I'm uncertain how to achieve this. I tried putting a flag I'd pass in during updates so that the code used rds.DatabaseInstanceFromSnapshot on creation and then rds.DatabaseInstance on updates. However, the CDK seems to see these as distinct resources even if they are created with the same ID and ends up creating a new instance on updates.

I know I can use DatabaseInstanceBase.from_database_instance_attributes to look up an existing DB but my understanding is that returned instance would be read-only. That puts me in a paradigm in which the CDK can no longer manage the RDS instance after the initial deployment from a snapshot, which is not desired.

How can I tell the CDK "if creating, load the DB from a snapshot, otherwise behave like a normal RDS instance" while maintaining the ability to control that existing instance? I also flirted with making a CustomResource but that seemed to introduce new complexities (and the RestoreDBInstanceFromDBSnapshotCommand didn't seem to accept all the parameters that the CDK's RDS instances normally do).

1 Answer 1

0

One way that I found to accomplish this is using rds.DatabaseInstanceFromSnapshot. As long as the instance_identifier and snapshot_identifier don't change, CloudFormation won't alter the DB (it won't try to re-restore the DB on subsequent deploys).

That means, of course, that once you deploy your DB once from a snapshot, you need to save that snapshot identifier somewhere and continue to use it on subsequent deploys.

It seems like an awkward way to go about to me but it does work.

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.