Skip to content

fix(ec2): RunInstances refuses an AMI that is malformed or names nothing (#733) - #741

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/733-runinstances-resolvable-ami
Aug 22, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/733-runinstances-resolvable-ami

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Substrate accepted any non-empty ImageId and launched an instance reporting it — ami-test,
not-an-ami, ami-EXAMPLE, ami-0abcdef1234567890. The only check asked whether a value was
present. So the mistake generated infrastructure code makes most often, naming an AMI that does
not exist in the target Region, was invisible until the same template reached real AWS — the
opposite of what substrate is for. Found by a live SDK run against a built binary.

What lands

A bundled AMI catalog (emulator/ec2_bundled_images.go, new). Nine AWS public SSM parameter
names resolve to a real image — Amazon Linux 2023 (full and minimal, x86_64 and arm64), Windows
Server 2022 Full Base, ECS-optimized AL2023 and AL2, Ubuntu 24.04 and 22.04 — each path taken
verbatim from the AWS page that publishes it. Any other AMI-shaped /aws/service/… path resolves
to its family's entry, because substrate has answered every such path since #267 and an unlisted
one must still resolve to something launchable.

The ID is derived, sha256(region + ":" + parameter) as ami- + 17 hex, and it is the same
computation GetParameter answers with — resolveManagedParameter now delegates to the catalog
instead of hashing the name itself. One table means the AMI substrate hands out cannot be an AMI
it then refuses. Derived rather than seeded: no startup write, every Region, stable across runs,
processes and replays. emulator.BundledImageID(region, parameter) exposes it;
TestServer.SeedEC2Image registers a caller-owned record for a test that needs a specific ID.

Deliberately not bundled: ami-0abcdef1234567890 and ami-1234567890abcdef0, the example IDs
AWS's own reference pages use and generated IaC copies. IaC that hardcodes one fails on real AWS,
so it must fail here. TestEC2_BundledImages_DocumentationExamplesAreNotBundled pins it.

One resolver. EC2Plugin.resolveImage checks ec2ImageStateKey first, then the catalog for
the request's Region, so a registered image wins over a derived one.

The kind and the refusal. ec2ImageIDKind joins the ten existing families in
ec2_resourceid.go, carrying AWS's published pair InvalidAMIID.NotFound /
InvalidAMIID.Malformed. A single insertion in runInstancesWithTags, beside the emptiness
refusal and after the launch-template merge, answers syntax before absence. That position matters
twice: an AMI reaching a launch through a template is refused at RunInstances time, and the check
sits ahead of ensureDefaultVPC, which would otherwise commit a VPC, subnet, security group,
gateway and route table before refusing. CreateFleet inherits the rule through the launch path
it already shares.

CreateLaunchTemplate / CreateLaunchTemplateVersion stay permissive, deliberately: AWS
reports mapping problems there through the response's warning member rather than refusing, so an
AMI rule would be a refusal AWS does not make. The template's AMI is checked when it is used.

The CFN deployer's default was the literal "ami-00000000" — a value that existed only because
nothing checked existence. It is now BundledImageID(region, …al2023-ami-kernel-default-x86_64),
so a template omitting ImageId still deploys, against an AMI substrate can resolve.

Narrowed from the plan

Three things the plan called for that implementation changed, recorded here because the release
notes come from the CHANGELOG, not the plan:

  1. No region-independent literal fixture entry. The plan wanted one catalog entry with a fixed
    ID for cfn_deployer.go:3289's default. A literal entry would be an AMI resolving in every
    Region, which is exactly the property ec2: RunInstances accepts any ImageId, with no InvalidAMIID.NotFound or .Malformed #733 removes. The default became Region-derived instead.
  2. Descriptors omit architecture, platform and root device. EC2Image has no fields for them,
    and adding three is a DescribeImages rendering change that belongs with ec2: DescribeVolumes answers an unknown VolumeId with an empty set, not InvalidVolume.NotFound #731's half of that
    operation. Filed as a follow-up.
  3. describeImages is untouched here. A bundled AMI is resolvable and nameable but not yet
    nameable through DescribeImages — that operation ignores ImageId.N entirely today, which
    is ec2: DescribeVolumes answers an unknown VolumeId with an empty set, not InvalidVolume.NotFound #731's worse defect. Until PR 3 lands, a bundled AMI describes as absent.

Bundled AMIs are resolvable but not enumerated by an unqualified describe — substrate's
reading, recorded in docs/services.md. DescribeImages lists what an account owns; a public
AWS-owned image is not that, and real AWS would answer tens of thousands of images.
InvalidAMIID.Unavailable is out of scope: substrate models no deregistered-but-extant image.

The sweep

Roughly 300 edits across 40 test files in two packages. Not one test was launching an AMI that
existed
— a quarter of the distinct literals were non-hex (ami-base, ami-tmpl,
ami-0legacy000000001) and would now trip Malformed, so seeding could not have rescued them; the
literals themselves changed. Shared fixtures did most of the work: one edit to ec2_tags_test.go's
ec2TagTestInstance cleared seven top-level failures, and one to newFleetLaunchTemplate cleared
seventeen.

Four orderings the sweep exposed, each a case where the new check preempted the refusal a test was
about: runInstancesWithTags checks the AMI before ec2CheckBlockDeviceMappings, before the
volume-scoped tag check, and before ensureDefaultVPC's InvalidGroup.NotFound; and
cfn_mappings_test.go's good mapping key had to move to us-east-1 because the deployer's default
Region is what resolves the AMI. cloudformation_identity_test.go's shared template const became
cfnInstanceTemplateIn(region) for the one test that deploys into eu-west-1.
TestEC2_RunInstances_MissingImageId stays green — an absent ImageId is still
MissingParameter, not Malformed.

Verification

gofmt -l . · go build ./... · go vet ./... · golangci-lint run ./...   clean
make test (race)                                                      pass
make coverage                                                         emulator 83.3%, total 82.6%
make docs-reference docs-reference-check docs-versions                 clean
npm --prefix docs run build + href/id anchor diff over dist            no dangling anchors
go vet -C test/e2e ./... && go test -C test/e2e ./...                  pass

Live SDK run against a built binary on :4678, nine parts — the issue's own two commands, AWS's
doc-example ID, SSM discovery then launch, the Windows and Ubuntu parameters, a eu-west-1 ID
refused in us-east-1, RegisterImage then launch, CreateFleet refusing a template's
unresolvable AMI while CreateLaunchTemplate accepted it, and a CFN stack with no ImageId
reaching CREATE_COMPLETE on the bundled default:

1. run-instances --image-id ami-00000000000000000
   InvalidAMIID.NotFound: The image ID 'ami-00000000000000000' does not exist
2. run-instances --image-id not-an-ami
   InvalidAMIID.Malformed: Invalid id: "not-an-ami"
3. run-instances --image-id ami-0abcdef1234567890
   InvalidAMIID.NotFound
4. GetParameter -> ami-d4c4e839d3f47ba84;  i-6cedd00fc53f2c27  ami-d4c4e839d3f47ba84  running
6. eu-west-1 -> ami-e70f32506019b1b47 (us-east-1 was ami-d4c4e839d3f47ba84)
   InvalidAMIID.NotFound in us-east-1
8. CreateLaunchTemplate accepted lt-cd8450278a83a138; CreateFleet -> InvalidAMIID.NotFound
9. Box  CREATE_COMPLETE  i-8c4645df6741803c

Closes #733

…ing (#733)

Substrate accepted any non-empty ImageId and launched an instance reporting it,
so the mistake generated IaC makes most often — naming an AMI that does not
exist in the target Region — was invisible until the template reached real AWS.

A new emulator/ec2_bundled_images.go bundles nine AWS public SSM parameters as
resolvable images with derived, Region-scoped IDs, and the SSM resolver now
answers from that one table so the AMI GetParameter hands out is the AMI
RunInstances accepts. ec2ImageIDKind joins the ten existing EC2 ID families, and
one insertion in runInstancesWithTags answers InvalidAMIID.Malformed before
InvalidAMIID.NotFound. CreateFleet inherits the rule; the two launch-template
paths deliberately do not.

Closes #733
@scttfrdmn
scttfrdmn merged commit 9634aba into main Aug 22, 2026
15 checks passed
@scttfrdmn
scttfrdmn deleted the fix/733-runinstances-resolvable-ami branch August 22, 2026 18:31
@codecov

codecov Bot commented Aug 22, 2026

Copy link
Copy Markdown

Codecov Report

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

Files with missing lines Patch % Lines
emulator/testing.go 75.00% 2 Missing and 2 partials ⚠️
emulator/ec2_plugin.go 85.71% 1 Missing and 1 partial ⚠️
emulator/ec2_bundled_images.go 97.56% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

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: RunInstances accepts any ImageId, with no InvalidAMIID.NotFound or .Malformed

1 participant