Reputation: 2527
Is it possible to create an aws dynamodb resource and attach it to a cloudformation stack post stack creation?
Use case: I have a dynamodb table that I want to wipe clean (delete all items). The two ways to do this are deleting and then recreating the table or deleting each item individually which is costly. As such, I would like to opt for deleting and recreating the whole table. However, the resource belongs to a cloudformation stack and I'd like to keep it that way.
Any ideas?
Upvotes: 0
Views: 412
Reputation: 16305
It's easy enough to remove the table from the stack resources, either by simply removing the resource from the template, or, a bit of a cleaner solution, use a Condition on the cloudformation resource to toggle the table on or off. you can then toggle off, deploy the stack ( removing the table), toggle on, and recreate the stack (recreating the table).
The real challenge with this technique is not the table itself, but all the references to that table in the CloudFormation stack. It's likely that you'll be referring to the table elsewhere - for example, as resources in your IAM Policies allowing access, in your application config to specify the table, etc. If this is the case, you'll have to change those places too, to use Fn::If
to control the creation of the reference with the same condition that creates the table. This ends up being rather complicated, but can be done with a combination of Fn::If
and {"Ref": "AWS::NoValue" }
.
I've done devops in AWS for quite a few years, and overall, I'd strongly recommend my developers to build an efficient script to clear dynamo tables and use that. It's not trivial to purge the table by deleting all items, but it's a lot simpler than conditionalizing the creation of all references to the table in your stack. At the end of the day, resetting the table data is an operational task distinct from infrastructure management, I'd suggest you keep it that way. What is the recommended way to delete a large number of items from DynamoDB? might get you started .
Upvotes: 2