amazon-ebs builder force_deregister flag: handling AMIs attached to running instances
0 reputation · 18 Aug 2021, 06:46 UTC
0 reputation · 18 Aug 2021, 06:46 UTC
To understand how Packer’s force_deregister option behaves when an AMI with the target name is currently attached to a running EC2 instance.
The build configuration is identical across local CI and production environments, but the outcome differs. The AWS region, IAM permissions for ec2:DeregisterImage, and the state propagation of image deregistration may influence the result. The ambiguity in the official documentation leads to inconsistent image management and potential outages in production.
force_deregister is true and the target AMI is attached to a running instance?force_deregister to avoid silent failures?When force_deregister = true is set in the amazon-ebs builder, Packer performs the following steps before creating a new AMI:
name tag (or names filter) in the target region.DeregisterImage API on that AMI ID.AWS permits DeregisterImage on an AMI even when one or more EC2 instances are currently running from that image. The deregistration is a control‑plane metadata operation; it does not stop, reboot, or otherwise affect the running instances. Those instances continue to use their existing root volume and remain operational until they are terminated or the volume is detached.
The observed inconsistency is most likely due to one of the following:
ec2:DeregisterImage for the specific AMI ARN or for * in the region, causing the deregistration call to return an UnauthorizedOperation error that Packer treats as a failure.ec2:DeregisterImage on the target resource (e.g., "arn:aws:ec2:::image/*") or on * for the region.PACKER_LOG=1) and capture the exact AWS API error code returned during the deregistration phase.DeregisterImage events around the time of the build to see whether the call succeeded, was denied, or was throttled.If the IAM policy is correct and the CloudTrail logs show a successful deregistration, the remaining variable is likely propagation delay. In that case, adding a short retry or wait (e.g., a sleep 10 step or using Packer’s wait_timeout on the builder) before the deregistration search can eliminate the race condition.
Do you see any DeregisterImage errors (such as UnauthorizedOperation or InvalidAMIID.NotFound) in the Packer build logs or CloudTrail for the failing runs? Knowing the specific error code will tell us whether the problem is permission‑related or a consistency/timing issue, which determines whether to fix IAM policies or add a retry/wait.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.