Skip to content

fix(ec2): carry a launch template's tags and instance profile to the launch (#471) - #481

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/471-template-tags-profile
Aug 2, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/471-template-tags-profile

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #471

The defect

A launch template's TagSpecifications and IamInstanceProfile were accepted by CreateLaunchTemplate and stored nowhere, so a template that tagged its instances produced untagged ones and a template naming a role produced an instance with none — with nothing failing to say so.

The tag half is the more damaging. DescribeInstances --filters tag:Env,Values=prod is how IaC finds the resources it just created, and it simply returned nothing, so an assertion on the tags the template asked for could not pass however it was written.

Neither field could even be read back before this release: DescribeLaunchTemplates carries no launchTemplateData, so #456's DescribeLaunchTemplateVersions is what makes the round-trip assertable. That is why #456 came into this milestone rather than #471's acceptance criterion being weakened.

Behaviour

Template tags replace rather than merge. A request naming Env=req against a template naming Env=tmpl,Team=x yields Env=req alone — Team is not inherited. The reference gives no TagSpecifications-specific merge semantics, only the general "Any additional parameters that you specify for the new instance overwrite the corresponding parameters included in the launch template", and replacement is that rule applied to the whole specification rather than a per-field citation.

Substrate's own aws:ec2:fleet-id stamp does not count as the request naming tags. A fleet instance already carries that reserved key by the time the merge runs, so the fallback tests for a non-reserved key (ec2HasUserTags) rather than for an empty set. Had it tested emptiness, a fleet launched from a tagging template would have silently lost the template's tags — the very defect this PR closes, on the path #443 added. It is the same reserved-key exclusion #469's counter uses, and it has its own test.

A template's tags are subject to both tag rules, so a template is not a second unrestricted tagging path. A reserved key or more than 50 tags is rejected at CreateLaunchTemplate and at CreateLaunchTemplateVersion — the latter after any SourceVersion inheritance, so an inherited violation is caught rather than propagated. The launch checks again, deliberately and not redundantly: a template written straight into state by a replayed event log can predate these checks, and that launch is where the key would otherwise be applied.

Scope, stated rather than implied

  • Only the instance scope is stored. A template may also scope tags to volume, network-interface or spot-instances-request, none of which substrate models, so those specifications are recorded nowhere rather than misapplied to the instance.
  • The profile is stored as the single string the request supplied, matching the shape an instance holds, so DescribeLaunchTemplateVersions echoes it in whichever member it arrived in — arn for an arn:-prefixed value, name otherwise. DescribeInstances still surfaces it as an ARN either way, because AWS's instance response shape has no name member; synthesizing the missing member for a template read-back would report the template as naming something the caller never wrote.

Sources

Verified against the AWS reference per CLAUDE.md:

  • ResponseLaunchTemplateData spells the members iamInstanceProfile and TagSpecificationSet.N, while the request spells them IamInstanceProfile and TagSpecification.N — so a round-trip test has to use both names. Noted in the code, because getting it backwards produces a template with no tags and nothing to say so.
  • LaunchTemplateIamInstanceProfileSpecification (response) has exactly arn and name.
  • LaunchTemplateTagSpecification (response) has resourceType and TagSet.N.
  • Template tags land on the instance: "These tags are not applied to the launch template."
  • Name is preferred over Arn when both are given. AWS documents neither as authoritative, so that choice is substrate's, made for consistency with the call-level path — said so in the field comment rather than implied to be a citation.
  • DescribeLaunchTemplateVersions' two worked XML examples show only imageId/instanceType inside launchTemplateData, so both new members' element shapes come from the member tables rather than a worked example.

Refactor

ec2LaunchTagsForResource becomes a one-line wrapper over a prefix-taking ec2TagSpecificationTags, rather than a fifth copy of the TagSpecification.N walk. LaunchTemplateData.TagSpecification.1.Tag.1.Key is the same indexed list one level in, and the whole point of PR #468's shared parser is that there is exactly one walk to check. The six existing callers are untouched.

Tests

New in emulator/ec2_launchtemplate_test.go:

  • CreateLaunchTemplate → DescribeLaunchTemplateVersions round-trips both fields, in both the Name and Arn forms.
  • An instance launched from a tagging template reports those tags and a tag:Env,Values=prod filter finds it — the half a consumer's IaC depends on.
  • An instance launched from a profile-naming template reports it, from either request member.
  • The full precedence matrix: request-wins / template-fills / neither, for tags and for the profile, with the replace-not-merge row asserting that Team is absent.
  • SourceVersion inheritance does not outrank the new version's own tags or profile — one arm per overlay field.
  • A reserved key and a 51st tag are rejected at CreateLaunchTemplate; a reserved key is rejected at CreateLaunchTemplateVersion and the version is not appended; exactly 50 template tags launch.
  • A template written straight into state with a reserved key, and one with 51 tags, are both rejected at the launch.
  • A fleet launched from a tagging template keeps the template's tags and aws:ec2:fleet-id.

Verification

From the repo root: gofmt -l ., go build ./..., go vet ./... clean; golangci-lint run ./... → 0 issues; make test (race) green; make docs-reference docs-reference-check docs-versions clean; go test -C test/e2e ./... green.

Mutation-tested: 19 mutations of the new logic, all 19 killed. Three survived the first pass and each bought a test — the two ec2OverlayTemplateData arms (now covered by the SourceVersion case) and the launch-path tag-limit check (now covered by the stored-51-tag case).

Docs

docs/services.md — the "not parsed at all" line is replaced; the per-field precedence table gains four rows; the instance-scope-only limit, the fleet-stamp interaction, the two tag rules and the profile echo direction are all stated. The reserved-key and 50-tag sections gain their launch-template halves.

…launch (#471)

A launch template's TagSpecifications and IamInstanceProfile were accepted by
CreateLaunchTemplate and stored nowhere, so a template that tagged its instances
produced untagged ones and a template naming a role produced an instance with
none — with nothing failing to say so.

The tag half is the more damaging: DescribeInstances --filters tag:Env,Values=prod
is how IaC finds the resources it just created, and it returned nothing, so an
assertion on the tags the template asked for could not pass however it was
written. Neither field could even be read back before this release, because
DescribeLaunchTemplates carries no launchTemplateData; #456's
DescribeLaunchTemplateVersions is what makes the round-trip assertable.

Template tags replace rather than merge. A request naming Env=req against a
template naming Env=tmpl,Team=x yields Env=req alone — Team is not inherited. The
reference gives no TagSpecifications-specific merge semantics, only the general
"Any additional parameters that you specify for the new instance overwrite the
corresponding parameters included in the launch template", and replacement is that
rule applied to the whole specification.

Substrate's own aws:ec2:fleet-id stamp does not count as the request naming tags.
A fleet instance already carries that reserved key by the time the merge runs, so
the fallback tests for a non-reserved key rather than for an empty set — had it
tested emptiness, a fleet launched from a tagging template would have silently
lost the template's tags, the very defect this closes, on the path #443 added.
It is the same reserved-key exclusion #469's counter uses.

A template's tags are subject to both tag rules, so a template is not a second
unrestricted tagging path: a reserved key or more than 50 tags is rejected at
CreateLaunchTemplate and at CreateLaunchTemplateVersion — the latter after any
SourceVersion inheritance, so an inherited violation is caught rather than
propagated. The launch checks again, deliberately: a template written straight
into state by a replayed event log can predate these checks, and that launch is
where the key would otherwise be applied.

Two scope limits, stated rather than implied. Only the instance scope is stored: a
template may also scope tags to volume, network-interface or
spot-instances-request, none of which substrate models, so those specifications
are recorded nowhere rather than misapplied. And the profile is stored as the one
string the request supplied, so DescribeLaunchTemplateVersions echoes it in
whichever member it arrived in — arn for an arn:-prefixed value, name otherwise.
DescribeInstances still surfaces it as an ARN either way, because AWS's instance
response shape has no name member; synthesizing the missing member for a template
read-back would report the template as naming something the caller never wrote.

ec2LaunchTagsForResource becomes a one-line wrapper over a prefix-taking
ec2TagSpecificationTags rather than a fifth copy of the TagSpecification.N walk,
so all five callers still share exactly one walk to check.

All 19 mutations of the new logic are killed by the new tests, including the two
overlay arms and the launch-path limit check.
@codecov

codecov Bot commented Aug 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 1b0ac91 into main Aug 2, 2026
16 checks passed
@scttfrdmn scttfrdmn mentioned this pull request Aug 2, 2026
@scttfrdmn
scttfrdmn deleted the fix/471-template-tags-profile branch August 2, 2026 22:02
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.

EC2: a launch template's TagSpecifications and IamInstanceProfile are not parsed

1 participant