Skip to content

fix(aws): retry RunInstances on IAM instance-profile propagation race (#572) - #576

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/572-runinstances-iam-race
Sep 5, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/572-runinstances-iam-race

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Summary

Fixes #572: spawn task run (and any headless launch through a freshly-created scoped IAM instance profile) could fail RunInstances with a transient InvalidParameterValue: ... Invalid IAM Instance Profile name, even though CreateOrGetInstanceProfile (pkg/aws/iam.go) already waited 10s and confirmed the profile via GetInstanceProfile.

Root cause: that confirmation only proves IAM control-plane visibility. RunInstances consumes the instance profile through EC2's own, separate IAM→EC2 propagation path, which AWS documents as independently eventually-consistent and explicitly recommends retrying RunInstances against. Under three parallel spawn task run launches (each getting its own per-task scoped profile, since profile names are derived from a hash of the policy content), one profile lost that second race even though the other two — running identical code — succeeded.

Fix: pkg/aws/client.go's Launch now calls RunInstances through a new runInstancesWithRetry helper that retries only this specific transient:

  • Detection (isTransientIAMInstanceProfilePropagation): AWS error code InvalidParameterValue and the error message contains "Invalid IAM Instance Profile name" — mirroring the message-level classification style already used by pkg/taskproto/completion.go's ClassifyLaunchError and the code-based checks in pkg/aws/iam.go (isAlreadyExists, isThrottle).
  • Backoff: bounded 2s, 4s, 8s (3 retries max), mirroring the shape of the existing create-side retryIAM (pkg/aws/iam.go:84) but sized for the consumption-side tail on top of the existing 10s create-side wait.
  • Any other error — including a genuinely different InvalidParameterValue (bad instance type, bad AMI, etc.) or any other AWS error — returns immediately with no retry. This is deliberately narrow so the fix can't become a blanket retry-on-any-launch-error mechanism.

Ordering / orphan-safety check (explicitly requested in the issue): confirmed by reading pkg/launcher/provision.go — the instance record, tags, and any ephemeral FSx are all created strictly after RunInstances succeeds (see the #213 comments at provision.go:141 and provision.go:159-179). A rejected RunInstances call has created nothing, so retrying it accumulates no partial state and cannot orphan anything. This assumption held; no change of approach was needed.

Not done: raising the fixed 10s sleep. As the issue notes, that's still a bet against an unbounded tail, just a slower one — this fix retries on the actual failure instead.

Changes

Test plan

  • New unit tests pass: go test ./pkg/aws/... -run "TestIsTransientIAMInstanceProfilePropagation|TestRunInstancesWithRetry" -v
  • Fail-without confirmed: stashing the client.go change (keeping the new test file) makes the package fail go vet (undefined symbols) — the tests are load-bearing on the fix, not tautological.
  • Pass-with confirmed after restoring the fix.
  • go test -short ./pkg/aws/... ./pkg/launcher/... — all green, no regressions.
  • make check — clean (fmt, vet, short tests all pass).
  • go run . gen-docs --out docs-gen — no diff (this change touches no flag/help text).
  • Nested lambda modules (for d in lambda/*/; do (cd "$d" && go build ./...); done) — all build clean, no go mod tidy needed.
  • Stray tracked binary lambda/autoscale-orchestrator/autoscale-orchestrator got locally rebuilt by the lambda build loop as expected; reverted before committing, confirmed not in the diff.

@codecov

codecov Bot commented Sep 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.50000% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/aws/client.go 87.50% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

…race (#572)

CreateOrGetInstanceProfile's fixed 10s sleep + GetInstanceProfile check only
proves IAM control-plane visibility for a freshly-created scoped instance
profile. RunInstances consumes the profile through EC2's own, separately
eventually-consistent propagation path, which AWS documents and recommends
retrying against. Under parallel `spawn task run` launches, one profile lost
that second race and RunInstances 400'd with InvalidParameterValue: Invalid
IAM Instance Profile name, even though the other concurrent launches (with
different scoped profiles) succeeded on identical code.

RunInstances now retries specifically on this transient, detected by AWS
error code (InvalidParameterValue) + message substring ("Invalid IAM
Instance Profile name") — not on any InvalidParameterValue, which would
waste time retrying genuinely bad config (bad instance type, bad AMI, etc.)
before failing the same way. Backoff is bounded (2s/4s/8s); any other error
still fails immediately with no retry.

Safe to retry: Provision creates the instance record, tags, and any
ephemeral FSx strictly after RunInstances succeeds (#213), so a rejected
RunInstances call has created nothing — a retry attempt accumulates no
partial state to clean up.
@scttfrdmn
scttfrdmn force-pushed the fix/572-runinstances-iam-race branch from c03c525 to af174b5 Compare September 5, 2026 01:35
@scttfrdmn
scttfrdmn merged commit cddd3f4 into main Sep 5, 2026
7 checks passed
@scttfrdmn
scttfrdmn deleted the fix/572-runinstances-iam-race branch September 5, 2026 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant