- Newest
- Most votes
- Most comments
To implement a solution that conditionally creates a DynamoDB table only if it doesn't already exist, you can modify your approach slightly. Instead of relying on the getResponseField method, which throws an error when the table doesn't exist, you can use the getCapturedResponse method of the AwsCustomResource. This method returns the entire response, allowing you to check for the existence of the table more safely.
Here's an improved version of your code:
const transactionTableName = `MailPolicyUpdatesTable`; // Custom Resource to check if the table exists const tableExistenceCheck = new cr.AwsCustomResource(this, "TransactionTableExistenceCheck", { onUpdate: { service: "DynamoDB", action: "describeTable", parameters: { TableName: transactionTableName, }, physicalResourceId: cr.PhysicalResourceId.of(Date.now().toString()), ignoreErrorCodesMatching: "ResourceNotFoundException" }, policy: cr.AwsCustomResourcePolicy.fromSdkCalls({ resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE }), }); // Check if the table exists by examining the entire response const response = tableExistenceCheck.getCapturedResponse('onUpdate'); const tableExists = response && response.Table; // Conditionally create or reference the table const transactionTable = tableExists ? dynamodb.TableV2.fromTableName(this, "ExistingTransactionTable", transactionTableName) : new dynamodb.TableV2(this, "MailPolicyUpdatesTable", { tableName: transactionTableName, partitionKey: { name: "id", type: dynamodb.AttributeType.STRING }, removalPolicy: cdk.RemovalPolicy.RETAIN, pointInTimeRecovery: true, globalSecondaryIndexes: [ { indexName: "date_index", partitionKey: { name: "date", type: dynamodb.AttributeType.STRING }, }, ], });
In this updated version:
- We use
getCapturedResponse('onUpdate')to get the entire response from the DescribeTable API call. - We check if the response exists and has a 'Table' property, which indicates the table exists.
- Based on this check, we either reference the existing table or create a new one.
This approach avoids the error when the table doesn't exist and provides a more robust way to conditionally create or reference the DynamoDB table.
Remember that this method still has a small chance of race conditions in a distributed environment. If absolute consistency is required, you might need to implement additional error handling or use a more robust strategy.
Sources
CDK TypeScript: Conditional Creation of DynamoDB Table | AWS re:Post
class CfnTable (construct) · AWS CDK
answered a year ago
Relevant content
asked a year ago
asked 4 years ago
- AWS OFFICIALUpdated 9 months ago
- AWS OFFICIALUpdated 3 years ago
