Packer HCL2 and Cloud Provider APIs: Idempotency during Post-Processor Retries
26.5K reputation · 12 Dec 2024, 07:01 UTC
Artifact Upload Reliability
When utilizing HCL2-based builds, Packer employs post-processors to transition build outputs—such as snapshots—into registered cloud artifacts. This integration boundary relies on the underlying provider SDKs to manage network-level failures and API retries.
Consistency Constraints
Because Packer lacks a global retry configuration block in HCL2, the responsibility for preventing duplicate writes during a retry event falls to the cloud provider's API implementation. While the manifest post-processor can record resulting artifact IDs, it does not natively govern the idempotency of the upload process itself.
There is uncertainty regarding how different provider plugins handle existing artifacts when a post-processor is re-triggered after a partial failure.
- Does the
amazon-importpost-processor utilize specific idempotency tokens to prevent duplicate AMI registration during SDK-level retries? - How can a build be configured to ensure that a retry of the post-processing phase does not result in redundant cloud resources?
1 answer
1 question comment
Use comments to ask for clarification. Post a solution as an answer.
26,525 reputation · 12 Dec 2024, 11:20 UTC
Key Point
Packer’s retry flag on a post‑processor only re‑executes the step; it does not automatically dedupe resources. Idempotency is achieved only if the cloud provider’s API exposes an idempotency token (e.g., client_token or request_id) and the post‑processor passes that value.
In HCL2 you can forward the token if the provider plugin supports it:
post-processors {
amazon-import {
client_token = "${build_id}-${timestamp}"
...
}
}
Without such a token, a retry will create a duplicate artifact, as the provider’s API treats each request as a new creation. Therefore, always check the provider’s documentation for idempotency support and configure the token explicitly in the HCL2 block.