Skip to content

CloudFormation: StackDeployer errors are untyped, so the wire plugin maps codes by string matching #502

Closed

Description

@scttfrdmn

Summary

StackDeployer reports every failure as an untyped fmt.Errorf string, so the
CloudFormation wire plugin added in v0.87.0 (#483) has to recover the AWS error code
by matching substrings of the message. That works, and it is fragile in a way a
typed error would not be.

The current state

emulator/cloudformation_plugin.go's cfnMapDeployerError does this:

switch {
case strings.Contains(msg, "not found"):
    // → ValidationError
case strings.Contains(msg, "parse template"),
     strings.Contains(msg, "parse old template"),
     strings.Contains(msg, "parse new template"):
    // → ValidationError
default:
    // → InternalFailure, 500
}

The strings it matches on come from emulator/betty_cfn.go:

  • "cfn CreateChangeSet: stack %q not found"
  • "cfn DescribeChangeSet: change set %q not found"
  • "cfn DetectStackDrift: stack %q not found"
  • "cfn DescribeStackDriftDetectionStatus: detection %q not found"
  • "parse template: %w", "parse old template: %w", "parse new template: %w"
  • "deploy resource %s: %w"

None of these is part of any contract. Rewording "stack %q not found" to
"no such stack %q" — an entirely reasonable copy-edit — silently turns a
ValidationError at 400 into an InternalFailure at 500 for every consumer, with no
compiler error and no test failure outside the CFN plugin's own suite.

It is also lossy in the other direction: "deploy resource %s: %w" wraps whatever
the underlying plugin returned, which may itself contain "not found" (an
AWS::EC2::Instance whose AMI does not resolve, say). That maps a genuine
resource-level deploy failure onto ValidationError when the accurate answer is
different.

Proposed fix

Declare sentinel errors in betty_cfn.go and wrap rather than format:

// ErrCFNStackNotFound is returned when an operation names a stack that does not exist.
var ErrCFNStackNotFound = errors.New("stack not found")

// ErrCFNChangeSetNotFound is returned when an operation names a change set that does not exist.
var ErrCFNChangeSetNotFound = errors.New("change set not found")

// ErrCFNDriftDetectionNotFound is returned when a drift-detection ID does not resolve.
var ErrCFNDriftDetectionNotFound = errors.New("drift detection not found")

// ErrCFNTemplateInvalid is returned when a template body cannot be parsed.
var ErrCFNTemplateInvalid = errors.New("invalid template")

// ErrCFNResourceDeployFailed is returned when a template resource could not be deployed.
var ErrCFNResourceDeployFailed = errors.New("resource deploy failed")

fmt.Errorf("cfn CreateChangeSet: stack %q: %w", name, ErrCFNStackNotFound) keeps the
message identical for anyone reading logs, and the plugin switches to errors.Is.
The distinction between "the request named something absent" and "a resource failed
to deploy" then survives, which the string match cannot make.

Consider also whether a deploy failure should carry the failing resource's logical ID
as a typed field, so DescribeStackResources and a future
DescribeStackEvents (#501) can report it without re-parsing the message either.

Why it was not done in #483

#483 was scoped as a wire adapter: no CloudFormation logic moved into the plugin and
betty_cfn.go was not touched, so a CI failure in that PR was unambiguously about
the adapter. Changing the model's error surface is a separate, small, mechanical
change that also touches betty_cfn_test.go and BettyClient's callers.

Acceptance criteria

  • Sentinel errors declared in betty_cfn.go and wrapped at every return site
    that currently formats one of the strings above.
  • Message text unchanged, so existing log-reading and any test asserting on a
    message still passes.
  • cfnMapDeployerError uses errors.Is with no strings.Contains.
  • A resource-level deploy failure maps to a distinct code from a
    request-names-something-absent failure, and a test pins the pair.
  • A test asserts the mapping does not depend on message wording: change a
    message in the test and the code still resolves.

Activity

  1. added
    type: chore
    area: cloudformation
    on Aug 2, 2026
  2. scttfrdmn commented on Aug 3, 2026

    @scttfrdmn
    OwnerAuthor

    Left unmilestoned deliberately while the rest of the CloudFormation cluster was swept into v0.88.0 (#521, #522, #526, #527) and v0.89.0 (#508, #518, #520). Recording the reasoning, and a concrete condition for picking it up.

    Why it is not in either release on its own. This is a type: chore with no consumer-visible symptom today: the substring matching in cfnMapDeployerError (emulator/cloudformation_plugin.go:1018) currently produces the right codes for the strings that exist. Its value is entirely preventive — it removes a class of future silent regression where a copy-edit to an error message turns a ValidationError at 400 into an InternalFailure at 500. Shipping it as a standalone release item means a release whose whole content is "no observable change", which is the wrong shape; shipping it as a rider on an unrelated defect release means a refactor of the error surface landing next to fixes that need to be reviewable on their own.

    The right time is a release that adds or reworks a deployer error string, because then the sentinel is the natural way to add it rather than a retrofit.

    That trigger is close, and worth stating so it is not missed. v0.88.0's #522 introduces at least two new deployer-level failures that both need a code on the wire and neither of which any current substring matches:

    • an Fn::FindInMap lookup with an unknown map/top-level/second-level key, which must fail the resource rather than emit a JSON literal — real CloudFormation returns ValidationError;
    • refusing to delete a stack whose export is still imported by another stack — real CloudFormation returns ValidationError with a message naming the importing stacks.

    If #522's implementation surfaces either of those through Deploy/DeleteStack's error return rather than recording them on the resource, then this issue lands in v0.88.0 with it, because the alternative is adding two more strings to the switch and widening exactly the fragility being described. If #522 records both on the resource instead (which is what v0.87.1's #519 work makes the natural choice — a refused resource reports CREATE_FAILED with the plugin's own code, and the deployer returns no error), then the mapper is untouched and this stays parked until something does touch it.

    One correction to the issue's own framing, since v0.87.1 changed it: the second half of "it is also lossy in the other direction" is now less true. "deploy resource %s: %w" no longer wraps a resource's plugin error up to the caller — #519 moved per-resource failures onto DeployedResource.Error and out of the returned error, so a NoSuchBucket from a nested plugin no longer risks being mapped to ValidationError by the "not found" arm. The forward-direction fragility (rewording a deployer message silently changes the wire code) is unaffected and remains the reason to do this.

  3. added this to the v0.88.0 milestone on Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: cloudformationtype: chore

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions