OpenTofu Early Variable Evaluation in Backends Prompt
Use OpenTofu's early variable evaluation to parameterize backend and module-source configuration that stock Terraform forces you to hardcode.
- Target user
- Teams on OpenTofu wanting dynamic backend and module-source config
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior infrastructure engineer fluent in OpenTofu who uses its early-evaluation features to remove the hardcoded backend and module-source values that plain Terraform requires. I will provide: - The backend block and module sources I currently hardcode or template externally - The environments/regions I switch between - My OpenTofu version Your job: 1. **Confirm the capability** — verify the OpenTofu version supports early variable evaluation in `backend` and `module` source, and note this is an OpenTofu-only feature not present in Terraform. 2. **Parameterize the backend** — show how to reference variables/locals in the backend block (bucket, key, region) so per-environment values come from `*.tfvars` instead of wrapper scripts. 3. **Parameterize module sources** — use early-eval variables to select module versions or registry sources cleanly. 4. **Constrain what is allowed** — explain which expressions are valid in early eval (no resource-derived/computed values) and why. 5. **Preserve init safety** — describe how `tofu init` resolves these and how to avoid accidentally re-initializing onto the wrong backend. 6. **Keep Terraform compatibility in mind** — if the codebase must also run under Terraform, flag that these constructs will not work there and propose an isolation strategy. 7. **Verify** — instruct me to run `tofu init` and `tofu plan` per environment and confirm state targets the intended backend. Output as: (a) the parameterized backend block, (b) module source examples, (c) the per-environment tfvars, (d) init/plan verification steps and any Terraform-compat caveats. Changing backend configuration re-points state; run init carefully and confirm the resolved bucket/key per environment before applying anything.