Skip to content

fix(cfn): a failed create rolls the stack back (#520) - #547

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/520-create-rollback
Aug 3, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/520-create-rollback

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #520

The defect

A stack holding a resource a plugin refused reported a terminal CREATE_COMPLETE, with every resource beside it left in place. A consumer's failure-handling path could not be tested at all, because substrate never produced the failure that path exists to handle — the teardown-side twin of the v0.88.0 cluster: HTTP 200 while the observable a caller parses is wrong.

aws cloudformation create-stack --stack-name partial --template-body file:///tmp/partial.json
aws cloudformation describe-stacks --stack-name partial --query 'Stacks[0].StackStatus'
# before: CREATE_COMPLETE      after: ROLLBACK_COMPLETE
aws sqs get-queue-url --queue-name still-here
# before: 200                  after: QueueDoesNotExist

What changed

ROLLBACK is the default, as the API's is. The resources the create had already made are swept through #518's dispatcher in the inverse of the deploy order, and the stack reaches ROLLBACK_COMPLETE naming the failed resource and the plugin's own error code. The record stays, because a ROLLBACK_COMPLETE stack still blocks a create of the same name — the one thing that makes the failure impossible to miss.

CreateStack's two failure parameters are read and honoured for the first time, and they are mutually exclusive — "you can specify either DisableRollback or OnFailure, but not both" — so giving both is a ValidationError rather than a precedence rule. The test is presence, not value: the CLI's --no-disable-rollback sends DisableRollback=false explicitly, so a rule that ignored a false would accept a combination the API rejects. This was measured against the CLI rather than assumed.

Option Outcome
OnFailure=ROLLBACK / DisableRollback=false swept, ROLLBACK_COMPLETE
OnFailure=DO_NOTHING / DisableRollback=true nothing swept, CREATE_FAILED — what substrate used to do unconditionally
OnFailure=DELETE swept and the record removed

DescribeStacks reports the option it was given rather than emitting DisableRollback: false for every stack. A sweep that cannot delete gives ROLLBACK_FAILED and keeps the record so the undeleted resource is discoverable. All of these answer 200 on the wire: real CreateStack returns its StackId before any rollback happens, so a rolled-back stack is not a failed call, and a 5xx would make an SDK retry a create that already ran.

A failed stack publishes no outputs, and therefore exports none. A duplicate export name is still answered as an error rather than as a rolled-back stack, because it is a refusal of the request: a request substrate declines outright never became a stack whose resources could roll back. That ordering — resource loop, then the redeploy guard, then export checks, then the failure branch — is the fix for eight tests that collided when the failure branch ran first.

A second defect, found rather than reported

UpdateStack is a re-Deploy of the whole template, so every unchanged resource's create is re-issued and its plugin refuses it as already existing — where real CloudFormation issues no call at all for a resource it is not changing. Before rollback this was merely invisible: the refusal landed on the resource's error field and the stack still said UPDATE_COMPLETE. With rollback added it becomes data loss, since every UpdateStack would see failures and sweep the resources it was asked to keep.

So an already-exists refusal is cleared when — and only when — the stack's previous deployment created that logical ID successfully and the template still declares it identically (canonically marshalled JSON, since Go's encoder sorts map keys). Each condition is load-bearing and each has a test: a rename into a name another stack owns, a resource that failed the previous time (so this stack never owned the name), and a record belonging to another account or Region all remain real failures. The refusal cannot be keyed on the physical ID — a refused create returns an empty one, measured on AWS::EC2::LaunchTemplate.

The error-code set was harvested by deploying twice against the default registry, the same way cfnDeleteAbsentCodes was. Ambiguous codes (ConflictException, AlreadyExistsException) are deliberately absent: clearing one would hide a real failure, whereas a missing entry only preserves the pre-existing behaviour.

Update rollback is an approximation, stated as one

A failed UpdateStack reports UPDATE_ROLLBACK_COMPLETE by re-deploying the stored previous template, since that is the only description of the previous state substrate holds. It converges on that template's declared state rather than restoring properties field by field, so a resource the failed update replaced may keep a new physical ID. Recorded in the doc comment and in docs/services.md — an undocumented approximation is the defect shape this release is about. A previous record that cannot be read leaves the stack at UPDATE_FAILED with the reason logged (//nolint:nilerr, explained at the site) rather than a rollback attempted against nothing.

Tests

15 new (11 deployer-level, 4 on the wire), plus three existing contracts updated with their reasoning recorded. TestCFN_FailedResourceDoesNotStopTheStack was rewritten rather than deleted, per the plan: the behaviour it pinned did not go away, it stopped being unconditional, so it is now the DO_NOTHING case. Every failure is a real refusal — "no" is under S3's three-character minimum — never an injected one or a missing plugin, since ServiceNotAvailable is substrate's own condition rather than one a caller hits against AWS.

Mutation testing: 11 applied, 11 caught. Two survived on first pass and each is now pinned by a test written for it — a stack record from another account/Region being read as the previous deployment, and a resource that failed previously being read as one this stack owns. Others covered: OnFailure defaulting to DO_NOTHING, sweeping the refused resource, cfnDeleteStackOp for a create rollback, the redeploy guard removed entirely, the declaration check dropped, the presence test weakened to a value test, the error-code prefix split dropped, and the ROLLBACK_FAILED branch made unreachable.

Verification

gofmt -l ., go build ./..., go vet ./..., golangci-lint run ./... → 0 issues, make test (race), make docs-reference docs-reference-check docs-versions, npm --prefix docs run build, go vet -C test/e2e ./..., go test -C test/e2e ./....

End to end against a live server: the plan's own reproduction, all four option outcomes, both mutual-exclusion errors, an unchanged-resource update reaching UPDATE_COMPLETE with its bucket alive, and a failed update reaching UPDATE_ROLLBACK_COMPLETE.

Compatibility

A test asserting that a partial stack reaches CREATE_COMPLETE, or that a resource beside a failed one survives, will go red. That is the fix. Pass OnFailure=DO_NOTHING (or DisableRollback=true) to keep the old outcome deliberately.

A stack holding a resource a plugin refused reported a terminal
CREATE_COMPLETE with every resource beside it left in place, so a
consumer's failure-handling path could not be tested at all — substrate
never produced the failure that path exists to handle.

The default is ROLLBACK, as the API's is: the resources the create had
already made are swept through #518's dispatcher in the inverse of the
deploy order, and the stack reaches ROLLBACK_COMPLETE naming the failed
resource and the plugin's own error code. CreateStack's OnFailure and
DisableRollback are read and honoured for the first time, and are
mutually exclusive as the reference makes them — tested by presence, not
value, since --no-disable-rollback sends an explicit false.

While building it, a second defect: UpdateStack is a re-Deploy, so every
unchanged resource's create is re-issued and its plugin refuses it as
already existing. Invisible before rollback; with rollback it would make
every update sweep the resources it was asked to keep. An already-exists
refusal is now cleared only when the previous deployment created that
logical ID successfully and the template still declares it identically.

Closes #520
@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.84615% with 42 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/betty_cfn_rollback.go 83.58% 22 Missing and 11 partials ⚠️
emulator/betty_cfn.go 76.92% 8 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 2faac55 into main Aug 3, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the feat/520-create-rollback 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 failed resource does not roll the stack back

1 participant