Repository navigation
fix(cfn): generate a per-stack physical name when a template omits one (#560) - #587
Merged
Merged
Conversation
#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.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:IAM::RoleEntityAlreadyExistsIAM::InstanceProfileEntityAlreadyExistsS3::BucketBucketAlreadyExistsLogs::LogGroupResourceAlreadyExistsExceptionDynamoDB::TableResourceInUseExceptionSQS::QueueCREATE_COMPLETE— sharing the first stack's queueSNS::TopicCREATE_COMPLETE— sharing the first stack's topicThe 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
QueueDoesNotExistwhile thesurviving stack still reported
CREATE_COMPLETE. No error was reported to anyone.The fix
An omitted name becomes
{stack}-{logicalID}-{suffix}, the shape CloudFormationdocuments 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
resolveStringPropcontract, and a template that sets a name isasking for the un-repeatable behavior AWS also gives it.
The suffix is derived, not random, and that is the load-bearing decision.
UpdateStackin substrate is a re-Deployof the whole template —clearUnchangedRedeploysexists precisely because every unchanged resource iscreated 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_UpdateDoesNotMoveAPhysicalIDis that gate; a
crypto/randsuffix 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,ClusterIdandReplicationGroupIdare deliberately excluded, with acomment 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/e2evet + test.Live against
substrate server --address :4671with the real AWS CLI(
AWS_MAX_ATTEMPTS=1), the same template as two stacks:!Ref: each stack's instance profile holds its own role.update-stackon an unchanged template →UPDATE_COMPLETE, andevery
PhysicalResourceIdbyte-identical.repro-bleftrepro-a's queue, bucket and roleall intact. (
repro-bitself reportsDELETE_FAILED— that is CloudFormation: DeleteStack never detaches a role from an instance profile, so the stack ends DELETE_FAILED and can never converge #581, whosesignature
MyRole DELETE_COMPLETE/MyProfile DeleteConflictis unchanged by thisPR and is the next one in this milestone.)
Mutation testing
Six mutants,
-race, anchor-count asserted before applying,gofmt+vetgated:ScopeChangesTheName)Constraints)UntabledTypeKeepsItsLogicalID)crypto/randsuffix per callUpdateDoesNotMoveAPhysicalID)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_LongStackNamesStillDiffercloses it and asserts non-vacuouslythat 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 updatedhere. A template that sets the name explicitly is unaffected, and
Ref/GetAttresolve to whichever name was used.
Closes #560