AWS CloudFormation
DynamoDB
DeletionPolicy
Data Preservation
Infrastructure as Code

Why doesn't a Retain DeletionPolicy in CloudFormation preserve a changed DynamoDB table?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

CloudFormation is AWS's service for provisioning and managing resources in a repeatable and consistent way. It allows users to define their infrastructure as code using JSON or YAML templates. One of the many resources you can manage with CloudFormation is a DynamoDB table. However, when it comes to updating a DynamoDB table configuration—like changing its throughput settings or altering its schema—the use of a DeletionPolicy such as Retain might not behave as expected.

Understanding DeletionPolicy and Retain Behavior

When you define a resource in a CloudFormation template, you have the option to specify a DeletionPolicy. This policy controls what happens to the resource if it's removed from the template during a stack update or if the stack is deleted. The policy has three possible values:

  • Delete: Deletes the resource.
  • Retain: Keeps the resource.
  • Snapshot: For certain resources, creates a snapshot before deletion.

The Case with DynamoDB

A Retain DeletionPolicy makes sense in scenarios where you want to preserve data beyond the lifecycle of a CloudFormation stack. For example, in a production environment, you might not want to delete a DynamoDB table just because you're tidying up old infrastructure configurations.

However, a Retain policy is not effective when you change certain attributes of a DynamoDB table. For instance, suppose you have a table with a specific read/write throughput or a particular set of secondary indexes, and you update these properties in your CloudFormation template. In these scenarios, CloudFormation may need to replace the table entirely if the resource type does not allow direct updates.

Why Changes Might Not Be Retained

  • Immutable Properties: Some properties of a DynamoDB table are immutable and require resource replacement. For example, changing the key schema or switching between provisioned and on-demand capacity modes.
  • Schema Changes: Altering the schema of a DynamoDB table, such as primary or secondary key definitions, necessitates a full replacement of the table. Retain does not prevent this replacement.
  • Rolling Back Changes: When an update fails and CloudFormation rolls back the entire stack, the Retain policy only applies to a table being removed, not replaced or altered.
  • Operational Mechanics: Behind the scenes, the AWS CloudFormation service may perform a delete-and-replace cycle for updates that are non-trivial or not supported by direct modifications.

Technical Example: Schema Change

Consider a scenario where you have a DynamoDB table defined in a CloudFormation template. Originally, it has the following attributes:

yaml
1MyDynamoDBTable:
2  Type: "AWS::DynamoDB::Table"
3  Properties:
4    TableName: "UserTable"
5    AttributeDefinitions:
6      - AttributeName: "UserId"
7        AttributeType: "S"
8    KeySchema:
9      - AttributeName: "UserId"
10        KeyType: "HASH"
11    ProvisionedThroughput:
12      ReadCapacityUnits: 5
13      WriteCapacityUnits: 5
14  DeletionPolicy: Retain

If you change the KeySchema or add a new global secondary index, a replacement will occur despite the Retain policy. Even though CloudFormation itself won’t delete the original table during replacement, the retention of data depends on how AWS handles these change cycles: often requiring a backing up (exporting) of data before deploying a new table, which needs manual intervention beyond the Retain policy.

Summary Table

Key AspectDescription
DeletionPolicy: RetainDirective for CloudFormation to retain a resource instead of deleting it during stack removal or template update.
Immutable PropertiesFeatures of a resource that are unmodifiable and necessitate recreation upon change, e.g., key schema changes in a DynamoDB table.
Common Triggers for ReplacementSchema changes (primary key, secondary index), switching capacity modes.
Operational MechanicsCloudFormation’s internal handling might involve creation of new resources replacing the old ones entirely, bypassing retention for updates.
Manual InterventionRequires separate backup or data migration strategies outside of what's handled automatically by CloudFormation policies.

Additional Considerations

Backup Strategies

To ensure data is not lost when a DynamoDB table with key updates needs replacement, consider implementing AWS Backup or exporting data to S3.

Consistent State Management

Use AWS Systems Manager or custom Lambda functions to better automate transitions involving data preservation.

Understanding the limitations of CloudFormation update mechanics with respect to specific properties can save time and prevent data mishaps. This underscores the importance of carefully planning any changes to DynamoDB tables managed through CloudFormation, especially when employing Retain policies.


Course illustration
Course illustration

All Rights Reserved.