Recently, I set up a DynamoDB table in my CDK project to be used by lambdas within the same project. Everything was working fine until we needed to delete the stack with the retain resource option set to true for the table.
However, when attempting a fresh deployment, we encountered an error stating that the table already exists, leading to the entire stack rolling back. I am now looking for code that will create the table only if it does not already exist.
Here is a snippet of the basic table creation code. Despite searching extensively, I have been unable to find any documentation addressing this issue or detailing the specific exception that needs to be caught. All we see are logs on the AWS CloudFormation console.
const dynamoTable = new Table(this, "my-table", {
tableName: StackConfiguration.tableName,
partitionKey: { name: "id", type: AttributeType.STRING },
});