Repository navigation
fix(ec2): merge a launch template with request-level parameters (#453) - #464
Merged
Merged
Conversation
`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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #453
The defect
runInstancesgated its entire launch-template block onImageIdbeing absent:So a request passing both an
ImageIdand aLaunchTemplatenever read the template at all. ItsInstanceType,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.xlargein a private subnet and pinning the AMI per environment at the call — which is whyImageIdis passed alongside a template — got at3.microin the default VPC and a green test.The rule
AWS's
RunInstancesreference is the specification, for theLaunchTemplateparameter: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/MissingParametercheck 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
UserDatais 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 implementDescribeInstanceAttribute— 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.microis 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":A sentinel cannot tell "absent" from "explicitly
t3.micro", so asking fort3.microalongside a template namingm5.largeyieldedm5.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 patternpublicIPPrefalready 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 namesImageId, template carriesc5.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: requestInstanceTypewins; explicitt3.microwins over the template'sm5.large(the sentinel bug); template fills an absent type; neither →t3.micro; requestKeyNamewins; template fills an absentKeyName; template fills an absentImageId.TestEC2_LaunchTemplate_NetworkingPrecedenceWithRequestImageID— with anImageIdpresent, request subnet + SG win while the template's public-IP preference still fills the gap.TestEC2_LaunchTemplate_RequestFalseBeatsTemplateTrue— explicitfalsebeats templatetrue; the three-state string's reason for existing.The entire existing launch-template suite passes unchanged — 29 subtests, including every pre-existing #444 test (
SubnetSourcesA–D,SubnetPrecedence,SecurityGroups,AssociatePublicIPAddress,NoNetworkInterface,DescribeEchoesNetworking) and the #412MissingImageIdtests. That is the evidence the reordering did not regress paths that already worked. The fleet suite matters here too:launchFleetPoolsets a call-levelImageIdwhenever an override names one, so every such fleet launch now reads the template where it previously did not.Mutation-tested
Both mutants compiled (
go vetgate) and were caught:lt != nil && imageID == ""gateMergesWithRequestParams+NetworkingPrecedenceWithRequestImageIDfailedinstanceType == "t3.micro"sentinelFieldPrecedence/an_explicit_t3.micro_wins_over_the_template's_typefailedVerification
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-versionsgreen,test/e2egreen.Docs
docs/services.mdgains 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. TheRunInstancescoverage row links to it. Also recorded: a template'sTagSpecificationsandIamInstanceProfileare not parsed, so neither participates in the merge (follow-up to be filed).