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.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-16 07:49:35 -05:00
1 parent 32594a9dd5
commit 2d444801fd
2 files changed
+35 -4

No files matched your search

@@ -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<KeySchemaElement> keySchema = new ArrayList<>();
List<AttributeDefinition> attrDefs = new ArrayList<>();
@@ -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()))