fix: scope IAM instance profile propagation check to IAM only - #700
fix: scope IAM instance profile propagation check to IAM only#700cchristous wants to merge 2 commits into
Conversation
The v1.8.1 propagation dry-run (RunInstances DryRun) hardcoded a t3.nano instance type and omitted networking context, so it failed for reasons unrelated to IAM propagation: - non-x86_64 (arm64) source AMIs hit an architecture mismatch - accounts without a default VPC hit VPCIdNotSpecified Both are terminal (ShouldRetry matched only "Invalid IAM Instance Profile"), so builds died at the propagation check even though the real launch would have succeeded. The temporary role was also left attached on failure, causing DeleteConflict during cleanup. The dry-run now does one job -- confirm the instance profile is visible to EC2 -- and treats any non-"profile not visible" outcome as "past IAM validation", deferring architecture/VPC/instance-type/AZ checks to the real launch (which reports them with accurate context). It passes the configured instance type (or the first spot type) and the resolved subnet so the request is well-formed enough to reach the IAM check. roleIsAttached is set as soon as the role is attached so cleanup detaches it even if the check fails. Fixes hashicorp#675
|
Thank you for your submission! We require that all contributors sign our Contributor License Agreement ("CLA") before we can accept the contribution. Read and sign the agreement Learn more about why HashiCorp requires a CLA and what the CLA includes Have you signed the CLA already but the status is still pending? Recheck it. |
Mirror the source_image GetOk guard rather than a bare type assertion, so a future flow that reaches this step without StepNetworkInfo fails cleanly.
|
Thanks for the fix, @cchristous! I reviewed and tested it.
One non-blocking question: Other than that, everything looks good. |
|
Thanks for testing it, @hariom-hashicorp! Your understanding is correct — there's no reachable path today where The guard is purely defensive/consistency: the line previously used a bare |
Description
Fixes a v1.8.1 regression (introduced in #639) in the temporary IAM instance profile flow.
#639 replaced the post-attach
time.Sleepwith aRunInstancesdry-run that waits for the newly-created instance profile to propagate to EC2. However, the dry-run hardcodedInstanceType: t3.nanoand passed no networking context, so it could fail for reasons unrelated to IAM propagation:t3.nanois x86_64.VPCIdNotSpecified(with no subnet, EC2 falls back to the default VPC and resolves a security group by name).ShouldRetryonly matchedInvalid IAM Instance Profile, so both of these were treated as terminal and the build failed at the propagation check — even though the real launch (which uses the configured instance type and the resolved subnet/security groups) would have succeeded. Separately,roleIsAttachedwas only set after the dry-run passed, so a failed dry-run left the temporary role attached to the instance profile, producingDeleteConflictonDeleteRole/DeleteInstanceProfileduring cleanup.Change. The propagation check has one job: confirm the instance profile is visible to EC2. This scopes it to exactly that:
Invalid IAM Instance Profileoutcome —DryRunOperationor a downstream error such as an architecture or instance-type/AZ mismatch — as "EC2 got past IAM validation, so the profile has propagated." Everything else is deferred to the real launch step, which validates and reports it with accurate context.RunConfig.EffectiveInstanceType()helper that falls back to the firstspot_instance_typesentry wheninstance_typeis unset (spot-only builds), sincePrepareguarantees exactly one is set.roleIsAttachedis set as soon as the role is attached, soCleanupdetaches it even if the propagation check fails.This affects the
amazon-ebs,amazon-ebsvolume, andamazon-ebssurrogatebuilders (which share this step). Theamazon-instancebuilder is unaffected — it uses a separate step that never had the dry-run.Alternatives considered. Making the dry-run fully mirror the real launch (subnet + security groups + exact type) was rejected: security groups add no signal for an IAM-propagation check and introduce a fresh eventual-consistency surface (a just-created temporary security group can transiently return
InvalidGroup.NotFound), and pinning a single instance type is stricter than a spot fleet, which launches on any of several types available in the AZ. Scoping the check to IAM alone is simpler and defers every non-IAM error to the real launch rather than treating it as a propagation failure, so it stops failing the builds the old code broke.Testing.
go build ./...,go vet, and thecommon,builder/ebs,builder/ebsvolume, andbuilder/ebssurrogatepackage tests pass. Added unit tests forEffectiveInstanceType()(including the spot fallback) and for the dry-run request construction.Resolved Issues
Closes #675
Rollback Plan
If a change needs to be reverted, we will roll out an update to the code within 7 days.
Changes to Security Controls
No changes to access controls, encryption, or logging. The temporary IAM role/instance-profile lifecycle and the policy documents applied are unchanged; the granted permissions are identical. The only security-adjacent effect is a reliability improvement to cleanup: the temporary role is now reliably detached and deleted even when the propagation check fails, avoiding leftover temporary roles/instance profiles.