Packer Custom Provisioner Plugin Prompt
Scaffold a custom Packer provisioner plugin in Go using the plugin SDK, with config validation, prepare/provision hooks, and tests.
- Target user
- Image-pipeline engineers extending Packer beyond built-in provisioners
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior infrastructure-as-code engineer who builds Packer plugins in Go to standardize how golden images are hardened across an org.
I will provide:
- What the provisioner must do during the build (run a hardening tool, inject config, register the image)
- The configuration fields users will set in the HCL2 template
- Target Packer version and plugin SDK version
Your job:
1. **Scaffold with the SDK** — lay out the plugin using `packer-plugin-sdk`, registering the provisioner via `plugin.Set` and a `main.go` entrypoint.
2. **Define typed config** — declare a `Config` struct with mapstructure tags and use `hcl2spec` generation so HCL2 validation works.
3. **Validate in Prepare** — implement `Prepare(raws ...interface{})` to decode config, accumulate all errors, and return them together rather than failing on the first.
4. **Implement Provision** — implement `Provision(ctx, ui, comm, vars)` using the `Communicator` to upload/run commands, streaming output to `ui` and honoring context cancellation.
5. **Handle failures** — return errors that abort the build cleanly and explain how a failed provisioner leaves a tainted artifact.
6. **Test and install** — provide a Go unit test for Prepare validation and the `packer plugins install` / `required_plugins` block for consumption.
Output as: directory layout, the provisioner Go file, the main.go registration, and an HCL2 template block using the plugin.
Note that a provisioner that ignores context cancellation can hang builds and leak the temporary build instance and its cost.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Packer Build Provenance & Image Signing Prompt
Add provenance, SBOM generation, and signing to a Packer image pipeline so every golden image is attributable, scannable, and verifiable before it ships to production.
-
Terratest Go Infrastructure Test Suite Prompt
Design a Terratest suite in Go that provisions real infrastructure, asserts on outputs and live behavior, and tears everything down cleanly even when assertions fail.
-
Packer HCL2 Multi-Cloud Build Matrix Prompt
Refactor Packer into HCL2 with shared build blocks and a source matrix that bakes equivalent images across AWS, Azure, and GCP in parallel — one provisioning path, many targets, with consistent tagging and manifests.
-
Packer + Ansible Provisioner Pipeline Prompt
Combine Packer and Ansible to build immutable golden images where Ansible roles do the in-image configuration, with shared roles between image-build and runtime, fast caching, and validation.
More Infrastructure as Code prompts & error guides
Browse every Infrastructure as Code prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.