Skip to content

fix(cfn): resolve Fn::ImportValue against a cross-stack export registry (#522) - #537

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/522-importvalue
Aug 3, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/522-importvalue

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Completes #522 by resolving its last intrinsic. The Mappings/Fn::FindInMap,
Fn::GetAZs, Fn::Cidr and pseudo-parameter halves shipped in PR #535; the plan
called for splitting Fn::ImportValue out if PR 3 grew, because the export
registry plus the delete-refusal is a new cross-stack model rather than another
resolver. It did, and this is that split.

The defect

Fn::ImportValue was the last intrinsic falling through resolveValue's
JSON-encoding fallback, so a template importing another stack's subnet ID deployed
a resource whose SubnetId was the literal {"Fn::ImportValue":"net-SubnetID"}
and reported CREATE_COMPLETE. Same shape as the rest of v0.88.0 — substrate
reports success where the observable is wrong — and the worst instance of it,
because the two-stack split is how every non-trivial template is organised.

The model

An output may now declare Export: {Name: …}, absent from cfnOutput until now.
The name is an expression rather than a string, because the conventional form
is Export: {Name: !Sub '${AWS::StackName}-SubnetID'} — an export name must be
unique per account and Region, so a template hard-coding one cannot be deployed
twice. It resolves before the first resource deploys, which the API permits: an
export name may not depend on a resource, so it is knowable from the template and
its parameters alone.

Exports are scoped per account and Region, per the documented restriction that
cross-stack references are limited to the same account and Region. Getting this
wrong in the permissive direction would be worse than not resolving at all: a
template would pass under test and fail in AWS. deleteStack on the wire now uses
the caller's deployer for the same reason — the stack record is unpartitioned, but
the export visibility deciding the refusal is not.

Four rules are enforced, and they are why exports are modeled rather than faked —
an import that resolves against nothing enforceable is a lookup, not a reference:

  • an import of an unpublished name fails the resource, with the name in its
    ResourceStatusReason, rather than resolving to "" or to JSON;
  • an export name already held by another stack is refused;
  • a stack whose export is imported cannot be deleted;
  • an imported export's value cannot be changed — nor dropped, which is the
    same thing from the importer's side, so a removal is refused on the same terms.
    Re-deploying an unchanged value is not a change, so an idempotent redeploy still
    works.

Both refusals are a ValidationError at 400 naming the export and every importing
stack, because the API's own remedy is "first find out which stacks are importing
them" and a refusal that withheld the answer would send the caller to ListImports
for something substrate already knows.

The one decision a reader would otherwise get wrong

What counts as an import is recorded when the resolver walks the template, not
derived from the template's text. An Fn::ImportValue in the branch of an Fn::If
that was not taken never happened, and neither did one that failed to resolve, so
neither pins an exporting stack. Only the resolver knows which branch it walked.

Relatedly: Fn::ImportValue was deliberately kept out of cfnIntrinsicNames until
it resolved. resolveNested (#526) consults that table to decide what is an
intrinsic, and admitting an unresolvable one would have resolved every nested
import to "" — strictly worse than the JSON fallback, which at least left the
intrinsic visible in the request the plugin refused. The table now carries it, with
that reasoning recorded in place.

Added

ListExports and ListImports, which were InvalidAction before there was
anything to list. ListExports reports the ExportingStackId ARN
DescribeStacks reports for the same stack, in one page with no NextToken;
ListImports reports stack names, and an export nothing imports — including
one that does not exist — is an empty list rather than an error, since the
reference documents no service-specific errors for it. DescribeStacks
additionally reports each output's ExportName beside it.

Provenance

DeleteStack documents only TokenAlreadyExists, so the ValidationError/400 for
both export refusals is substrate's own choice — the same code the service uses
for every other unsatisfiable request on it. The messages are substrate's; no
capture of real wording was taken. Everything else here (the scope restriction, the
uniqueness rule, the modify-while-imported prohibition, the permitted inner
functions, ListExports/ListImports shapes and their absence of
service-specific errors) is from the API reference.

Substrate does not enforce "an export name can't use Ref or GetAtt functions that
depend on a resource": by the time export names resolve, the resources have
deployed. That is strictly more permissive rather than differently behaved.

Tests

16 new tests over both seams. The plan's stated gate — an Fn::ImportValue of
another stack's export resolves, and deleting the exporting stack is refused
— plus
the account/Region scoping in both directions, the name conflict, the
change-and-drop refusals, the resolution-time import recording (including an import
in a false Fn::If branch not counting), resolution at depth inside a structured
property, legacy-snapshot scope back-compatibility, determinism of which conflict is
reported, and the ListExports/ListImports/DescribeStacks-ExportName wire
shapes.

Mutation testing: 27 mutants, 27 CAUGHT, 0 survivors. Five survived the first
round and each was a real test-design gap, closed with a test rather than argued
equivalent:

  • sameScope requiring an exact account match → a v0.87 snapshot's stacks vanish
    from ListExports rather than exporting nothing (TestCFN_LegacySnapshotStacksStayInScope)
  • loadExports including the deploying stack → a redeploy imports the name it is
    in the middle of redefining (TestCFN_AStackDoesNotImportItsOwnExport)
  • iterating the conflict map unsorted → which of two conflicts is reported varies
    between runs (TestCFN_TheReportedExportConflictIsDeterministic)
  • the delete refusal not excluding the stack itself → a stack importing its own
    export is permanently undeletable (TestCFN_AStacksOwnImportDoesNotPinIt)
  • deleteStack using the default identity → a non-default caller's own import is
    invisible, so a delete that must be refused is allowed
    (TestCFNPlugin_DeleteRefusalUsesTheCallersOwnNamespace)

Verification

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

End to end through the real AWS CLI against substrate server: the import
resolves to e2e-net-bucket and sqs list-queues reads it back; list-exports
reports the ARN describe-stacks reports; list-imports names the importer;
deleting the exporter is refused with the ValidationError above; an unknown
export gives CREATE_FAILED with Fn::ImportValue: no exported output named "nope-Name" in this account and region; describe-stacks shows ExportName
beside the output; deleting the importer first releases the exporter.

Closes #522

…ry (#522)

Fn::ImportValue was the last intrinsic falling through to its JSON encoding,
so a template importing another stack's subnet ID deployed a resource whose
SubnetId was the literal {"Fn::ImportValue":"net-SubnetID"} and reported
CREATE_COMPLETE. That is the silent-success shape this release is about, and
the worst instance of it: the two-stack split is how every non-trivial
template is organised.

An output may now declare Export: {Name: …}. The name is an expression rather
than a string because the conventional form is
Export: {Name: !Sub '${AWS::StackName}-SubnetID'}; it resolves before the
first resource deploys, which the API permits since an export name may not
depend on a resource.

Exports are scoped per account and Region, per the documented restriction that
cross-stack references are limited to the same account and Region. Getting
that wrong permissively would be worse than not resolving at all: a template
would pass under test and fail in AWS. deleteStack on the wire now uses the
caller's deployer for the same reason.

Four rules are enforced, which is why exports are modeled rather than faked —
an import that resolves against nothing enforceable is a lookup, not a
reference: an import of an unpublished name fails the resource rather than
resolving to "" or to JSON; a name another stack already exports is refused; a
stack whose export is imported cannot be deleted; and an imported export's
value cannot be changed, nor dropped, which is the same thing from the
importer's side. Both refusals are ValidationError at 400 naming the export
and every importing stack.

What counts as an import is recorded when the resolver walks the template, not
derived from the template text: an Fn::ImportValue in the branch of an Fn::If
that was not taken never happened, and neither did one that failed to resolve.
Only the resolver knows which branch it walked.

Adds ListExports and ListImports, which were InvalidAction before there was
anything to list, and reports each output's ExportName in DescribeStacks.

Closes #522
@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.44860% with 29 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/betty_cfn.go 84.27% 13 Missing and 12 partials ⚠️
emulator/cloudformation_plugin.go 92.72% 2 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit a1a92a3 into main Aug 3, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the fix/522-importvalue branch August 3, 2026 09:34
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: Fn::FindInMap needs a Mappings model, and Fn::ImportValue/Cidr/GetAZs are expanded but unresolved

1 participant