Repository navigation
feat(ec2): read an instance's user data back with DescribeInstanceAttribute (#473) - #486
Merged
Merged
Conversation
…ribute (#473) Substrate recorded UserData at launch and nothing could observe it, so a consumer could not assert that the user data their IaC intended reached the instance — and #453's launch-template UserData fallback could only be tested indirectly, meaning it could regress with nothing observable changing. Four attributes are readable, being the ones backed by state substrate holds: userData (as stored, still base64), instanceType, disableApiTermination, and groupSet. Every other name in AWS's valid-values list is refused rather than defaulted — answering sourceDestCheck with a default false would be indistinguishable from a real instance that has it disabled. That message is captured from real AWS in aws/aws-cli#4273 and is byte-identical to moto's. An unset attribute is a present-but-empty element, which the reference cannot settle; it ships from moto's assertion and is labelled as weaker provenance. ModifyInstanceAttribute gained UserData.Value and DisableApiTermination.Value, and now requires a stopped instance for userData and instanceType — a new rejection on a path that previously succeeded. disableApiTermination is deliberately exempt, since RunInstances documents it as settable while running. Closes #473
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This was referenced Aug 2, 2026
Merged
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 #473
DescribeInstanceAttributewas absent entirely, which made an instance's user datawrite-only:
RunInstancesrecordedUserDataand nothing could read it back, so aconsumer could not assert that the user data their IaC intended reached the instance.
It also left #453's launch-template
UserDatafallback testable only indirectly —it could have regressed with nothing observable changing.
What reads back
Four attributes, being the ones backed by state substrate holds:
AttributeuserDatainstanceTypedisableApiTerminationModifyInstanceAttributegroupSetgroupSet>item, the same shapeDescribeInstancesreports (#444)Scalars are
<value>-wrapped, as all three of the reference's worked examples show,and exactly one attribute appears per response.
Unmodelled attributes are refused, not defaulted
InvalidParameterValue, 400,Value (enaSupport) for parameter attribute is invalid. Unknown attribute.AnsweringsourceDestCheckwith a defaultfalsewould beindistinguishable from a real instance that has it disabled, and a consumer asserting
on it would get a green test built on a value substrate invented.
This is the strongest message provenance in the release: captured from real AWS in
aws/aws-cli#4273 and byte-identical to moto's string — a capture and an independent
reimplementation agreeing. The reference could not have supplied it; its Errors
section is empty.
enaSupportis in AWS's own valid-values list while the same pagesays the attribute "is not supported", and #4273 captures exactly that rejection.
The criterion the reference cannot settle
An unset attribute is a present but empty element (
<userData></userData>), notan omitted one. All three of the reference's examples show an attribute that has a
value, so this ships from moto's
test_describe_instance_attributeassertingresponse["UserData"] == {}. That is weaker provenance than a capture and islabelled as such in the code and the docs, because the shapes are not
interchangeable: an SDK maps a present-but-empty element to an empty struct and an
omitted one to nil, so
resp.UserData.Valuepanics under one and not the other.Behaviour change:
ModifyInstanceAttributerequires a stopped instanceuserDataandinstanceTypenow needstopped, where changing a runninginstance's type previously succeeded —
IncorrectInstanceState, 400. Thereference's Example 1 says so plainly, and the client-error table names user data as
the same rule's worked example.
TestEC2_ModifyInstanceAttributeasserted the oldbehaviour and is corrected here; the rejection's own assertion lives in
TestEC2_InstanceAttribute_ModifyRequiresStopped.disableApiTerminationis deliberately exempt.RunInstancesdocuments thattermination protection can be enabled "when you launch an instance, while the
instance is running, or while the instance is stopped", so gating it would refuse a
call real EC2 accepts — the same class of defect in the other direction, and harder
to notice because it looks like extra rigor.
TestEC2_InstanceAttribute_TerminationProtectionNotGatedpins it.
One bug found while writing the tests
Go's
encoding/xmlemits the parent element of ana>bpath even for an emptyslice, so a
groupSet>itemfield put<groupSet></groupSet>in every response: acaller asking for
userDatawas told the instance had no security groups. No structdecoder can see this — only the raw wire. Fixed with a nil-able wrapper, and pinned
from both directions (
NotContainson the wire, plusTestEC2_InstanceAttribute_EmptyGroupSetIsStillPresentfor the case where an emptygroupSetis what was asked for).Verification
16 new tests.
gofmt,go build ./...,go vet ./...,golangci-lint run ./...(0 issues),
make test(race),make docs-reference docs-reference-check docs-versions,go test -C test/e2e ./..., and the VitePress build all pass.Mutation-tested 18/18 caught, including: omitting the empty
<userData/>;flattening the
<value>wrapper; thegroupSetleak above; gatingdisableApiTermination; dropping the stopped gate; a non-empty check onUserData.Value(which would make clearing user data inexpressible);case-insensitive attribute matching; and looking the instance up before validating
the attribute.
Termination protection is recorded and reported but not acted on —
TerminateInstancesstill terminates a protected instance, where real EC2 refuses with
OperationNotPermitted. Noted in the code and docs; being filed separately.