Repository navigation
fix(ec2): RunInstances refuses an AMI that is malformed or names nothing (#733) - #741
Merged
Merged
Conversation
…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
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This was referenced Aug 22, 2026
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.
Substrate accepted any non-empty
ImageIdand launched an instance reporting it —ami-test,not-an-ami,ami-EXAMPLE,ami-0abcdef1234567890. The only check asked whether a value waspresent. 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 parameternames 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 resolvesto 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)asami-+ 17 hex, and it is the samecomputation
GetParameteranswers with —resolveManagedParameternow delegates to the cataloginstead 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.SeedEC2Imageregisters a caller-owned record for a test that needs a specific ID.Deliberately not bundled:
ami-0abcdef1234567890andami-1234567890abcdef0, the example IDsAWS's own reference pages use and generated IaC copies. IaC that hardcodes one fails on real AWS,
so it must fail here.
TestEC2_BundledImages_DocumentationExamplesAreNotBundledpins it.One resolver.
EC2Plugin.resolveImagechecksec2ImageStateKeyfirst, then the catalog forthe request's Region, so a registered image wins over a derived one.
The kind and the refusal.
ec2ImageIDKindjoins the ten existing families inec2_resourceid.go, carrying AWS's published pairInvalidAMIID.NotFound/InvalidAMIID.Malformed. A single insertion inrunInstancesWithTags, beside the emptinessrefusal 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
RunInstancestime, and the checksits ahead of
ensureDefaultVPC, which would otherwise commit a VPC, subnet, security group,gateway and route table before refusing.
CreateFleetinherits the rule through the launch pathit already shares.
CreateLaunchTemplate/CreateLaunchTemplateVersionstay permissive, deliberately: AWSreports mapping problems there through the response's
warningmember rather than refusing, so anAMI 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 becausenothing checked existence. It is now
BundledImageID(region, …al2023-ami-kernel-default-x86_64),so a template omitting
ImageIdstill 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:
ID for
cfn_deployer.go:3289's default. A literal entry would be an AMI resolving in everyRegion, which is exactly the property ec2: RunInstances accepts any ImageId, with no InvalidAMIID.NotFound or .Malformed #733 removes. The default became Region-derived instead.
EC2Imagehas no fields for them,and adding three is a
DescribeImagesrendering change that belongs with ec2: DescribeVolumes answers an unknown VolumeId with an empty set, not InvalidVolume.NotFound #731's half of thatoperation. Filed as a follow-up.
describeImagesis untouched here. A bundled AMI is resolvable and nameable but not yetnameable through
DescribeImages— that operation ignoresImageId.Nentirely today, whichis 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.DescribeImageslists what an account owns; a publicAWS-owned image is not that, and real AWS would answer tens of thousands of images.
InvalidAMIID.Unavailableis 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; theliterals themselves changed. Shared fixtures did most of the work: one edit to
ec2_tags_test.go'sec2TagTestInstancecleared seven top-level failures, and one tonewFleetLaunchTemplateclearedseventeen.
Four orderings the sweep exposed, each a case where the new check preempted the refusal a test was
about:
runInstancesWithTagschecks the AMI beforeec2CheckBlockDeviceMappings, before thevolume-scoped tag check, and before
ensureDefaultVPC'sInvalidGroup.NotFound; andcfn_mappings_test.go's good mapping key had to move tous-east-1because the deployer's defaultRegion is what resolves the AMI.
cloudformation_identity_test.go's shared template const becamecfnInstanceTemplateIn(region)for the one test that deploys into eu-west-1.TestEC2_RunInstances_MissingImageIdstays green — an absentImageIdis stillMissingParameter, notMalformed.Verification
Live SDK run against a built binary on
:4678, nine parts — the issue's own two commands, AWS'sdoc-example ID, SSM discovery then launch, the Windows and Ubuntu parameters, a eu-west-1 ID
refused in us-east-1,
RegisterImagethen launch,CreateFleetrefusing a template'sunresolvable AMI while
CreateLaunchTemplateaccepted it, and a CFN stack with noImageIdreaching
CREATE_COMPLETEon the bundled default:Closes #733