Skip to content

fix(ec2): merge a launch template with request-level parameters (#453) - #464

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/453-launch-template-merge
Aug 2, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/453-launch-template-merge

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #453

The defect

runInstances gated its entire launch-template block on ImageId being absent:

if imageID == "" {
    // ... resolve template, read every field from it
}

So a request passing both an ImageId and a LaunchTemplate never read the template at all. Its InstanceType, KeyName, UserData, security groups and — since #444 — its subnet and public-IP preference were all silently dropped.

The launch still succeeded and still returned an instance ID, so nothing in the response said anything was wrong. The instance simply was not the one that was asked for. A consumer templating a c5.xlarge in a private subnet and pinning the AMI per environment at the call — which is why ImageId is passed alongside a template — got a t3.micro in the default VPC and a green test.

The rule

AWS's RunInstances reference is the specification, for the LaunchTemplate parameter:

Any additional parameters that you specify for the new instance overwrite the corresponding parameters included in the launch template.

Per field, request wins; the template fills what the request omitted. The template is now resolved whenever one is named, and each existing fallback stays guarded on the call-level value being absent.

The #412 ImageId / MissingParameter check still runs after resolution, so a template remains a valid sole source of the AMI, and a template carrying none still fails.

Two things beyond the mechanical fix

UserData is a new fallback, not a restored one. A template's user data was parsed and stored but never applied to the launch. Note that no registered operation reads it back — substrate does not implement DescribeInstanceAttribute — so the merge is observable only in recorded instance state, not through an API call. That is stated in the CHANGELOG rather than claimed as tested.

An explicit InstanceType=t3.micro is now honored over a template's type. The default used to be applied before the template was read, and the template fallback then tested for that literal value as a proxy for "the request named no instance type":

if instanceType == "t3.micro" && lt.LatestData.InstanceType != "" { ... }

A sentinel cannot tell "absent" from "explicitly t3.micro", so asking for t3.micro alongside a template naming m5.large yielded m5.large — exactly inverting the documented precedence. The default is now resolved last, after the template has had its chance, so it applies only when neither side names a type. This is the same three-state pattern publicIPPref already uses (#444). It was reachable before this change and far more reachable after it, which is why it is fixed here.

Tests

Four new tests in emulator/ec2_launchtemplate_test.go:

  • TestEC2_LaunchTemplate_MergesWithRequestParams — the headline case, the whole bug in one launch: request names ImageId, template carries c5.xlarge / key name / non-default subnet / SG / AssociatePublicIpAddress=true; asserts the request's AMI and all five template values on the same instance.
  • TestEC2_LaunchTemplate_FieldPrecedence — 7 cases: request InstanceType wins; explicit t3.micro wins over the template's m5.large (the sentinel bug); template fills an absent type; neither → t3.micro; request KeyName wins; template fills an absent KeyName; template fills an absent ImageId.
  • TestEC2_LaunchTemplate_NetworkingPrecedenceWithRequestImageID — with an ImageId present, request subnet + SG win while the template's public-IP preference still fills the gap.
  • TestEC2_LaunchTemplate_RequestFalseBeatsTemplateTrue — explicit false beats template true; the three-state string's reason for existing.

The entire existing launch-template suite passes unchanged — 29 subtests, including every pre-existing #444 test (SubnetSources A–D, SubnetPrecedence, SecurityGroups, AssociatePublicIPAddress, NoNetworkInterface, DescribeEchoesNetworking) and the #412 MissingImageId tests. That is the evidence the reordering did not regress paths that already worked. The fleet suite matters here too: launchFleetPool sets a call-level ImageId whenever an override names one, so every such fleet launch now reads the template where it previously did not.

Mutation-tested

Both mutants compiled (go vet gate) and were caught:

Mutation Result
Restore the lt != nil && imageID == "" gate MergesWithRequestParams + NetworkingPrecedenceWithRequestImageID failed
Restore the instanceType == "t3.micro" sentinel FieldPrecedence/an_explicit_t3.micro_wins_over_the_template's_type failed

Verification

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

Docs

docs/services.md gains a new §"A launch template merges with the request, field by field" with the per-field precedence table, the AWS quote, and both fixes stated including why each failed silently. The RunInstances coverage row links to it. Also recorded: a template's TagSpecifications and IamInstanceProfile are not parsed, so neither participates in the merge (follow-up to be filed).

`runInstances` gated its entire launch-template block on `ImageId` being
absent, so a request passing both an `ImageId` and a `LaunchTemplate` never
read the template at all: its `InstanceType`, `KeyName`, `UserData`, security
groups and (since #444) its subnet and public-IP preference were all silently
dropped. The launch still succeeded and still returned an instance ID, so
nothing in the response said anything was wrong -- the instance simply was not
the one that was asked for.

The template is now resolved whenever one is named, with each field falling
back to it only when the request did not supply that field. AWS's RunInstances
reference is the specification: "Any additional parameters that you specify for
the new instance overwrite the corresponding parameters included in the launch
template." The #412 `ImageId` check still runs after resolution, so a template
remains a valid sole source of the AMI.

The `UserData` fallback is new rather than restored -- a template's user data
was parsed and stored but never applied. No registered operation reads it back
(there is no `DescribeInstanceAttribute`), so the merge is observable only in
recorded instance state, not through an API call.

Separately, an explicit `InstanceType=t3.micro` is now honored over a
template's type. The default used to be applied before the template was read,
and the template fallback then tested for that literal value as a proxy for
"the request named no instance type" -- so asking for `t3.micro` alongside a
template naming `m5.large` yielded `m5.large`, inverting the documented
precedence. The default is now resolved last, so it applies only when neither
side names a type.

Closes #453
@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 035a442 into main Aug 2, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the feat/453-launch-template-merge 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 is ignored entirely when ImageId is also passed

1 participant