- Newest
- Most votes
- Most comments
When upgrading CDK versions, especially with significant version jumps like yours (from 2.95.0 to 2.179.0), you may encounter issues with resource replacement, particularly for stateful resources like DynamoDB tables.
The main reason your DynamoDB table is being replaced is likely due to one of these factors:
-
TableV2 construct changes: The
TableV2construct might have had internal implementation changes between CDK versions that affect how CloudFormation interprets the resource definition. -
Table name not explicitly specified: In your code snippet, you're not providing a
tableNameproperty. Without an explicit table name, CDK generates a unique physical ID for the table. When you upgrade CDK versions, this generated ID might change, causing CloudFormation to see it as a new resource. -
L1 to L2 construct changes: If there were changes in how the L2 construct (
TableV2) maps to the underlying L1 construct (CfnTable), this could trigger a replacement.
To prevent this issue in future upgrades:
- Specify an explicit table name:
const transactionTable = new dynamodb.TableV2(this, 'MailPolicyUpdateTable', { tableName: 'MailPolicyUpdateTable', // Add this line partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING }, // other properties... });
-
Use the CDK Refactor feature: For future structural changes, consider using CDK's refactor capability which is designed to safely reorganize resources without replacement.
-
Split stateful and stateless resources: Consider separating your DynamoDB tables into their own stack, separate from frequently changing components like Lambda functions. This isolation helps prevent unintended replacements of stateful resources.
-
Test upgrades in non-production environments: Always test CDK upgrades in development environments first to catch potential resource replacements before they affect production data.
For your current situation with the unreferenced historical data, you'll need to:
- Migrate the data from the old table to the new one
- Or update your application to reference the old table (you'll need to identify it in the AWS Console)
- Consider using the
fromTableNamemethod to reference the existing table instead of creating a new one if the table already exists
Remember to always set removalPolicy: cdk.RemovalPolicy.RETAIN for important tables in production to prevent accidental deletion, as you've already done.
Sources
Conditional Creation of DynamoDB | AWS re:Post
CDK TypeScript: Conditional Creation of DynamoDB Table | AWS re:Post
AWS Cloud Development Kit (CDK) Launches Refactor | AWS DevOps & Developer Productivity Blog
class CfnTable (construct) · AWS CDK
Relevant content
asked 4 years ago
asked a year ago
