Skip to content

fix(cfn): generate a per-stack physical name when a template omits one (#560) - #587

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/560-generated-physical-names
Aug 6, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/560-generated-physical-names

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

A CloudFormation stack in substrate could not be deployed twice. A resource whose
template omits its physical name got the logical ID verbatim — unique only
within a stack, while an IAM role name, bucket, table or log group is unique
across an account or a Region. Omitting the name is the recommended practice, so
the templates most likely written correctly were exactly the ones that collided.

Measured on main, deploying one 7-type template as two stacks:

Type Second stack
IAM::Role EntityAlreadyExists
IAM::InstanceProfile EntityAlreadyExists
S3::Bucket BucketAlreadyExists
Logs::LogGroup ResourceAlreadyExistsException
DynamoDB::Table ResourceInUseException
SQS::Queue CREATE_COMPLETE — sharing the first stack's queue
SNS::Topic CREATE_COMPLETE — sharing the first stack's topic

The last two are the worse half, and the issue does not name them: those creates are
idempotent, so both stacks reported success while pointing at one resource.
Deleting either then destroyed the other's — measured QueueDoesNotExist while the
surviving stack still reported CREATE_COMPLETE. No error was reported to anyone.

The fix

An omitted name becomes {stack}-{logicalID}-{suffix}, the shape CloudFormation
documents for its own generated physical IDs (MyStack-MyBucket-abcdefghijk1),
fitted to each service's own length and case limits. Explicit names are untouched:
that is the existing resolveStringProp contract, and a template that sets a name is
asking for the un-repeatable behavior AWS also gives it.

The suffix is derived, not random, and that is the load-bearing decision.
UpdateStack in substrate is a re-Deploy of the whole template —
clearUnchangedRedeploys exists precisely because every unchanged resource is
created again. A name regenerated per deploy would mint a fresh resource on every
update and leak the one it replaced. So it is FNV-64a(account/region/stack/logicalID)
in base36: unique per stack, opaque, and stable, which keeps an unchanged update a
no-op and the emulator deterministic by construction. TestCFNGeneratedName_UpdateDoesNotMoveAPhysicalID
is that gate; a crypto/rand suffix fails it (verified as a mutant).

A table of nine types, not a blanket rewrite. Only 9 of the 68
resolveStringProp(…, logicalID, …) sites are account/Region-unique names.
PathPart (an API Gateway URL segment unique only within its parent), Domain,
SecretId, ClusterId and ReplicationGroupId are deliberately excluded, with a
comment saying why: generating those would change URLs and identifiers a template
legitimately controls.

Verification

Full gate green: gofmt, go build, go vet, golangci-lint (0 issues),
make test (race), make docs-reference-check docs-versions, the docs site build,
and test/e2e vet + test.

Live against substrate server --address :4671 with the real AWS CLI
(AWS_MAX_ATTEMPTS=1), the same template as two stacks:

repro-a: CREATE_COMPLETE          repro-b: CREATE_COMPLETE
roles     repro-a-MyRole-4ucgrorqmrlq      repro-b-MyRole-qsvc60t041v1
profiles  repro-a-MyProfile-f78mredbfz7f   repro-b-MyProfile-1r5g9bx3l07m
buckets   repro-a-mybucket-z3ipqnirsdqo    repro-b-mybucket-csh3kxpt1awb
queues    repro-a-MyQueue-1fw0qedi4hv5     repro-b-MyQueue-ixrjtu8232wc

Mutation testing

Six mutants, -race, anchor-count asserted before applying, gofmt+vet gated:

Mutant Verdict
Drop the stack name from the hash CAUGHT — after it found a real gap, see below
Drop account+Region from the hash CAUGHT (ScopeChangesTheName)
Clamp the assembled name, sacrificing the suffix CAUGHT (Constraints)
Untabled type gets a generated name anyway CAUGHT (UntabledTypeKeepsItsLogicalID)
crypto/rand suffix per call CAUGHT (UpdateDoesNotMoveAPhysicalID)
Explicit-name branch bypassed CAUGHT (3 existing update/redeploy tests)

The first survived on the first pass, and for a real reason: the stack name is
also a visible segment, so two stacks differ even with it absent from the hash — until
truncation, where two long stack names sharing a truncated prefix would produce
identical segments and, without the stack name in the suffix, identical names. That is
#560 again for exactly the templates whose names are long.
TestCFNGeneratedName_LongStackNamesStillDiffer closes it and asserts non-vacuously
that the segments really do truncate to the same text, so the suffix is carrying the
whole distinction. The mutant is caught on re-run.

Compatibility

Every stack whose template omits a physical name now gets a different physical
name
, so any fixture asserting a resource name equal to its logical ID must be
re-recorded — one test in this repo did (TestCFN_LogGroupDefaultName) and is updated
here. A template that sets the name explicitly is unaffected, and Ref/GetAtt
resolve to whichever name was used.

Closes #560

#560)

A resource with no explicit name got the logical ID verbatim as its physical
name. A logical ID is unique only within a stack, while an IAM role name,
bucket, table or log group is unique across an account or a Region, so any
second stack from the same template collided. Omitting the name is the
recommended practice, so the templates most likely written correctly were
exactly the ones that could not deploy twice.

SQS::Queue and SNS::Topic failed worse and silently: their creates are
idempotent, so two stacks both reported CREATE_COMPLETE while pointing at one
resource, and deleting either destroyed the other's with no error reported.

An omitted name now becomes {stack}-{logicalID}-{suffix}, fitted to each
service's length and case constraints. The suffix is derived (FNV-64a over the
account, Region, stack name and logical ID) where AWS randomizes: UpdateStack
here re-deploys the whole template, so a per-deploy random name would mint a
fresh resource on every update and leak the one it replaced.

The fix is a table of nine account/Region-unique name types, not a blanket
rewrite of every logicalID fallback — PathPart, Domain, SecretId, ClusterId and
ReplicationGroupId are excluded because generating them would change URLs and
identifiers a template legitimately controls.
@scttfrdmn
scttfrdmn merged commit 7afd56e into main Aug 6, 2026
@scttfrdmn scttfrdmn mentioned this pull request Aug 6, 2026
@scttfrdmn
scttfrdmn deleted the fix/560-generated-physical-names branch August 9, 2026 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CloudFormation: a resource with no explicit physical name gets the logical ID, so a second stack from the same template always collides

1 participant