Skip to content

feat(cloudformation,ec2): stamp aws:cloudformation:* on the EC2 resources a stack creates (#746) - #766

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/cfn-stack-tag-stamp-746
Aug 23, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/cfn-stack-tag-stamp-746

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

AWS puts three tags on every resource a stack creates —
aws:cloudformation:stack-name, aws:cloudformation:stack-id and
aws:cloudformation:logical-id — and substrate's deployer put none. That is not only a
missing member: it left a bundled policy permanently unsatisfiable.
AmazonECS_FullAccess's ManagedCloudformationResourcesCleanupPolicy allows four EC2
deletes on any resource whose ec2:ResourceTag/aws:cloudformation:stack-name is like
EC2ContainerService-*; the resolution for that key already worked (#730), CreateTags
refuses an aws: key as AWS does, and no path existed by which the tag could ever exist —
so the statement granted nothing at all, and nothing distinguished that from a policy bug.

Design

The stamp is written from deployResource, the sole caller of dispatchResource, which
already holds the logical ID, the physical ID and the stack's name, account and region. So
none of the 110 dispatch arms is touched.

It is deliberately a state write and not a synthesized CreateTags: that call refuses the
aws: keys it would be carrying, and StackDeployer.dispatch runs CheckAccess on every
synthesized request, so the parameter route would newly require ec2:CreateTags of every
principal whose deployment succeeds today. resourceTags and applyTagsToResource are
promoted to free functions taking a StateManager, the same refactor ec2LookupDefaultVPC
already had, because the deployer holds no plugin.

The stack ID written is the ARN AWS::StackId resolves to rather than a second derivation of
it — two derivations of one identity is the defect #517 was. The write is an upsert, so
re-deploying rewrites the three tags rather than accumulating them, and aws: keys are
already outside the 50-tag limit, so the stamp cannot push a caller's own tags over it.

Scoped to EC2, which is what the bundled statement needs: the resolver maps an ID prefix to a
state key and only EC2 keeps tags in a store shaped that way, so all nine EC2 types the
deployer creates are covered by prefix and a tenth arrives free. A non-EC2 physical ID is
skipped silently — a stack creates far more non-EC2 resources than EC2 ones, so a warning per
resource would bury a real one — as is a resource that failed to deploy or has no physical ID.

Provenance

Of the three keys, only aws:cloudformation:stack-name appears on any AWS CloudFormation page
that was reachable: every user-guide page documenting the set returned an empty body, and
API_CreateStack.html documents caller-supplied stack-tag propagation, which is the other
mechanism. The triple is observed behavior rather than the API model, and
docs/services.md records it as such.

Verification

gofmt/go build/go vet/golangci-lint clean (0 issues); make test green with race;
coverage holds at emulator 83.4%, total 82.8%; make docs-reference docs-reference-check docs-versions pass; the VitePress build succeeds with no href="#…" lacking an id;
test/e2e vet and test green.

Six new tests (emulator/cfn_stack_tags_test.go), the last of which asserts the whole chain
rather than the tag: it attaches the bundled AmazonECS_FullAccess, deploys
EC2ContainerService-web and unrelated-stack, and has the policy actually authorize a
DeleteSecurityGroup for the first and refuse it for the second. Fail-before: 14 FAIL lines
across 5 tests with the stamp call removed.

Live SDK fail-before/pass-after against built binaries, aws CLI over --endpoint-url,
deploying a ten-resource stack (nine EC2 types plus a bucket):

main this branch
tags on each of the nine EC2 resources none the three, on all nine
tags on the S3 bucket none none (skipped)
aws:cloudformation:stack-id vs the AWS::StackId output None identical ARNs
create-tags Key=aws:cloudformation:stack-name by a caller InvalidParameterValue InvalidParameterValue
bundled cleanup delete, EC2ContainerService-live's SG AccessDenied Return: true
bundled cleanup delete, unrelated-stack's SG AccessDenied AccessDenied
tag count after re-deploying the stack 0 → 0 3 → 3

Carve-outs, filed not dropped

Also fixes a lazy-continuation bug in docs/services.md introduced by #744, where a paragraph
following a bullet list was rendered into the list item.

Compatibility

A test asserting an exact tag set on a CFN-deployed EC2 resource now sees three more tags, and
a tag-scoped policy that matched nothing before may now match.

Closes #746

…rces a stack creates (#746)

AWS puts aws:cloudformation:stack-name, aws:cloudformation:stack-id and
aws:cloudformation:logical-id on every resource a stack creates; substrate's
deployer put none. That left AmazonECS_FullAccess's
ManagedCloudformationResourcesCleanupPolicy permanently unsatisfiable: it
conditions on ec2:ResourceTag/aws:cloudformation:stack-name, the resolution for
that key already worked (#730), CreateTags refuses an aws: key as AWS does, and
no path existed by which the tag could ever exist.

The stamp is written from deployResource, the sole caller of dispatchResource,
which already holds the logical ID, the physical ID and the stack's name,
account and region — so none of the 110 dispatch arms is touched. It is a state
write rather than a synthesized CreateTags because that call refuses the aws:
keys it would carry, and because every synthesized request is authorized, so
the parameter route would newly require ec2:CreateTags of every principal whose
deployment succeeds today. resourceTags and applyTagsToResource are promoted to
free functions taking a StateManager, as ec2LookupDefaultVPC already is, since
the deployer holds no plugin.

Scoped to EC2, which is what the bundled statement needs: the resolver maps an
ID prefix to a state key and only EC2 keeps tags in a store shaped that way, so
all nine EC2 types the deployer creates are covered by prefix. A non-EC2
physical ID is skipped silently, as is a resource that failed to deploy or has
no physical ID. Extending the stamp is #765; caller-supplied stack tags, still
dropped, are #764.

Only aws:cloudformation:stack-name appears on any reachable AWS CloudFormation
page, so the triple is observed behavior rather than the API model and
docs/services.md records it as such.

Closes #746
@scttfrdmn scttfrdmn added this to the v0.109.0 milestone Aug 23, 2026
@codecov

codecov Bot commented Aug 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.87097% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/cfn_deployer.go 85.00% 2 Missing and 1 partial ⚠️
emulator/ec2_plugin.go 81.81% 0 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 7be5985 into main Aug 23, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the fix/cfn-stack-tag-stamp-746 branch August 23, 2026 05:51
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.

cfn: the deployer does not apply stack tags to the resources it creates

1 participant