Packer Amazon EBS Builder and AWS API: shutdown_stop vs shutdown_terminate behavior
26.5K reputation · 07 Mar 2024, 12:09 UTC
When utilizing the Amazon EBS builder in HashiCorp Packer (HCL2), the lifecycle of the temporary instance is managed via specific shutdown configurations. The goal is to ensure a predictable state for the instance upon the completion of the build process to avoid orphaned resources or unexpected billing.
There is an ambiguity regarding how Packer interacts with the AWS API when transitioning from the provisioning phase to the image creation phase. Specifically, the behavior of the shutdown_stop and shutdown_terminate options may vary depending on the instance state and the specific cloud environment configuration.
shutdown_stop: Intended to stop the instance before creating the AMI.shutdown_terminate: Intended to terminate the instance after the AMI is successfully created.
Does the shutdown_stop setting guarantee a clean filesystem sync before the AMI snapshot is triggered? In scenarios where shutdown_terminate is used, does Packer verify the AMI availability before issuing the termination call to the AWS API?
1 answer
1 question comment
Use comments to ask for clarification. Post a solution as an answer.
26,525 reputation · 07 Mar 2024, 16:31 UTC
To build on the previous point regarding shutdown_behavior, it is important to note that switching from the default terminate to stop introduces a specific dependency on the IAM policy assigned to the Packer execution identity.
While terminate relies on ec2:TerminateInstances, using stop requires the ec2:StopInstances permission. If this permission is missing, the build may successfully create the AMI but fail during the final cleanup phase, potentially leaving the temporary instance running and incurring unnecessary compute costs.
Verification Checklist
- IAM Policy: Ensure
ec2:StopInstancesis granted if usingshutdown_behavior = "stop". - Cost Audit: Verify that stopped instances are flagged for deletion in your environment, as EBS volumes continue to incur storage charges even when the instance is not running.