From 2d444801fd84cae4c0376a3dabbdaf8cf8227259 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Wed, 16 Sep 2026 07:49:35 -0500 Subject: [PATCH] fix(cloudformation): keep a generated DynamoDB table name across updates (#3721) A table without an explicit TableName got a fresh generated name on every UpdateStack, which created a second table and re-pointed Ref at it. TableName is create-only, so the provisioner now resolves the name through ProvisionContext.stablePhysicalName: the template's name, else the name the resource already has, and only failing both a generated one. This is the same fix earlier batches applied to other types, split out of #3699 at the reviewers' request so that refactor stayed behaviour-preserving. --- .../provisioners/DynamoDbCfnProvisioner.java | 9 +++--- .../DynamoDbCfnProvisionerTest.java | 30 +++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java index f555540726..4724ecbb83 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java @@ -99,10 +99,11 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner { private void provisionTable(StackResource r, JsonNode props, ProvisionContext ctx) { CloudFormationTemplateEngine engine = ctx.engine(); String region = ctx.region(); - String tableName = ctx.resolveOptional(props, "TableName"); - if (tableName == null || tableName.isBlank()) { - tableName = ctx.generatePhysicalName(r.getLogicalId(), TABLE_NAME_MAX_LENGTH, false); - } + // TableName is create-only, so an update without one must keep the name generated on + // create rather than mint another: a fresh name here created a second table on every + // UpdateStack and re-pointed Ref at it. + String tableName = ctx.stablePhysicalName(ctx.resolveOptional(props, "TableName"), + r.getLogicalId(), TABLE_NAME_MAX_LENGTH, false); List keySchema = new ArrayList<>(); List attrDefs = new ArrayList<>(); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisionerTest.java index 5726d7cc19..570051fedc 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisionerTest.java @@ -54,6 +54,13 @@ class DynamoDbCfnProvisionerTest { return new ProvisionContext(engine, "us-east-1", "000000000000", "my-stack"); } + /** The update path: the same engine plus the physical id the previous provision assigned. */ + private ProvisionContext updateCtx(String priorPhysicalId) { + ProvisionContext create = ctx(); + return new ProvisionContext(create.engine(), create.region(), create.accountId(), + create.stackName(), priorPhysicalId); + } + private StackResource resource(String type, String logicalId) { StackResource r = new StackResource(); r.setLogicalId(logicalId); @@ -228,6 +235,29 @@ class DynamoDbCfnProvisionerTest { verifyNoInteractions(dynamoDb); } + /** + * A table without an explicit TableName keeps the name generated on create: regenerating one + * on every UpdateStack created a second table and re-pointed Ref at it. + */ + @Test + void updateWithoutExplicitNameReusesTheGeneratedName() throws Exception { + String priorName = "my-stack-Orders-0123456789ab"; + TableDefinition existing = table(false); + existing.setTableName(priorName); + when(dynamoDb.createTable(eq(priorName), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString())) + .thenThrow(new AwsException("ResourceInUseException", "Table already exists", 400)); + when(dynamoDb.describeTable(priorName, "us-east-1")).thenReturn(existing); + StackResource r = resource("AWS::DynamoDB::Table", "Orders"); + r.setPhysicalId(priorName); + + provisioner.provision(r, props("{}"), updateCtx(priorName)); + + assertEquals(priorName, r.getPhysicalId()); + verify(dynamoDb).createTable(eq(priorName), anyList(), anyList(), any(), any(), anyList(), anyList(), + eq("us-east-1")); + verify(dynamoDb).describeTable(priorName, "us-east-1"); + } + @Test void tagsAreAppliedAndStaleOnesRemoved() throws Exception { when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))