Skip to content

feat(ec2): read an instance's user data back with DescribeInstanceAttribute (#473) - #486

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/473-describe-instance-attribute
Aug 2, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/473-describe-instance-attribute

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #473

DescribeInstanceAttribute was absent entirely, which made an instance's user data
write-only: RunInstances recorded UserData and nothing could read it back, so a
consumer could not assert that the user data their IaC intended reached the instance.
It also left #453's launch-template UserData fallback testable only indirectly —
it could have regressed with nothing observable changing.

What reads back

Four attributes, being the ones backed by state substrate holds:

Attribute Reports
userData The value as stored, still base64 — re-encoding would double it
instanceType
disableApiTermination Now recorded at launch and by ModifyInstanceAttribute
groupSet groupSet>item, the same shape DescribeInstances reports (#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. Answering sourceDestCheck with a default false would be
indistinguishable 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. enaSupport is in AWS's own valid-values list while the same page
says 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>), not
an 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_attribute asserting
response["UserData"] == {}. That is weaker provenance than a capture and is
labelled 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.Value panics under one and not the other.

Behaviour change: ModifyInstanceAttribute requires a stopped instance

userData and instanceType now need stopped, where changing a running
instance's type previously succeeded — IncorrectInstanceState, 400. The
reference's Example 1 says so plainly, and the client-error table names user data as
the same rule's worked example. TestEC2_ModifyInstanceAttribute asserted the old
behaviour and is corrected here; the rejection's own assertion lives in
TestEC2_InstanceAttribute_ModifyRequiresStopped.

disableApiTermination is deliberately exempt. RunInstances documents that
termination 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_TerminationProtectionNotGated
pins it.

One bug found while writing the tests

Go's encoding/xml emits the parent element of an a>b path even for an empty
slice, so a groupSet>item field put <groupSet></groupSet> in every response: a
caller asking for userData was told the instance had no security groups. No struct
decoder can see this — only the raw wire. Fixed with a nil-able wrapper, and pinned
from both directions (NotContains on the wire, plus
TestEC2_InstanceAttribute_EmptyGroupSetIsStillPresent for the case where an empty
groupSet is 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; the groupSet leak above; gating
disableApiTermination; dropping the stopped gate; a non-empty check on
UserData.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 — TerminateInstances
still terminates a protected instance, where real EC2 refuses with
OperationNotPermitted. Noted in the code and docs; being filed separately.

…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
@scttfrdmn scttfrdmn added type: feature New feature or improvement area: ec2 fidelity labels Aug 2, 2026
@codecov

codecov Bot commented Aug 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.66667% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/ec2_instanceattribute.go 90.00% 4 Missing and 3 partials ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 17e6831 into main Aug 2, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the feat/473-describe-instance-attribute branch August 2, 2026 06:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: ec2 fidelity type: feature New feature or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EC2: UserData is not observable through any operation (no DescribeInstanceAttribute)

1 participant