Select Language:
If you’re using AWS SAM and find that a resource like a DynamoDB table gets unexpectedly deleted during deployment, don’t worry—there are steps you can take to prevent this from happening in the future.
When you update your template.yaml and remove or omit a resource like a DynamoDB table, CloudFormation assumes you want it deleted unless you tell it otherwise. To keep that resource intact even if it’s not present in your new template, you need to mark the resource as “retained.” This means CloudFormation will ignore it during updates and won’t delete it automatically. You can do this by adding a DeletionPolicy attribute set to “Retain” in your resource definition.
Here’s a quick example:
yaml
Resources:
MyDynamoDBTable:
Type: AWS::DynamoDB::Table
DeletionPolicy: Retain
Properties:
TableName: my-shared-table
# other properties
Another reliable way is to import and manage shared resources outside of CloudFormation, then reference them as imported resources in your stacks. This approach helps avoid accidental deletion.
For shared resources like DynamoDB tables that multiple stacks rely on, the best practice is to manage them separately from your main application stacks. Use CloudFormation Stack Imports or AWS Resource Import capabilities to reference existing resources without risking deletion. This keeps your shared resources safe and ensures updates won’t delete what others depend on.
Now, about your lost data — unfortunately, without backup or point-in-time recovery enabled on your DynamoDB table, retrieving the lost data might not be possible. If the data is critical, contacting AWS Support could give some options, but often, without backups, the data is unrecoverable.
In the future, enabling Point-In-Time Recovery (PITR) and regular backups can save a lot of trouble if accidental deletions happen again.
Remember, managing shared resources carefully and marking important resources with Retain policies can prevent accidental deletions during updates. Always keep backups, especially for critical production data.




