OpenTofu Provider-Defined Functions & Early Variable Evaluation Prompt
Adopt OpenTofu's early variable evaluation and provider-defined functions to make module sources, backends, and provider configuration dynamic and DRY where Terraform-style static requirements once blocked you.
- Target user
- Platform engineers building reusable OpenTofu modules
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a platform engineer modernizing OpenTofu modules to use features that diverge from Terraform: early variable/local evaluation (variables usable in `module.source`, `backend`, and `module.for_each` under `-var`/env at init time) and provider-defined functions (`provider::<name>::<fn>(...)`). Your job is to apply these to remove copy-paste and hardcoded strings — without introducing init-time surprises or provider version traps.
I will provide:
- The OpenTofu version in use and the providers involved
- The current module structure and the hardcoded values you want to make dynamic (module sources, backend config, region/account, per-item modules)
- Any provider-defined functions you want to use, and the target compatibility (must it still run on Terraform?)
Your job:
1. **Early-eval opportunities** — identify where early evaluation legitimately helps: parameterizing `module "x" { source = "...${var.registry}..." }`, `backend "s3" { bucket = var.state_bucket }`, or `module.for_each = var.regions`. For each, show the variable definition and how it must be supplied at `init`/`plan` (CLI `-var`, `TF_VAR_`, or `.tfvars`) — and warn that these values must be resolvable statically, no resource references.
2. **Provider-defined functions** — show correct `provider::<name>::<fn>` usage, the `required_providers` block that must be present for the function namespace to resolve, and how a missing/old provider version produces a "function not found" failure. Give a concrete example replacing a brittle string manipulation.
3. **Portability decision** — state clearly which of these features are OpenTofu-only and would break `terraform` if the module must stay cross-compatible; recommend keeping a portable variant or gating on the tool.
4. **Init/plan ordering** — explain what is evaluated at init vs. plan, so an early-eval backend or source change is picked up correctly, and why `-reconfigure`/`-migrate-state` may be needed when backend inputs change.
5. **Verification** — commands to prove it works: `tofu init` with the vars, `tofu plan` showing the resolved sources/backends, and how to catch a value that failed to resolve statically.
Output as: (a) the early-eval refactor per target with variable + supply method, (b) provider-defined-function usage with the required_providers block, (c) a portability/compatibility note, (d) the init/plan verification commands. Keep changes reproducible across CI.
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
-
OpenTofu Native Test Framework Module Validation Prompt
Build a real test suite for a reusable OpenTofu module using the native `tofu test` framework — combining fast plan-only assertions, targeted apply-and-verify runs, mocked providers, and variable-validation coverage into a CI gate that catches regressions before they reach state.
-
IaC Module Documentation Generator Prompt
Generate clear, consumable documentation for an infrastructure-as-code module, role, or chart — inputs, outputs, examples, requirements, and an upgrade note — so consumers adopt the paved road without reading the source, across any IaC tool.
-
IaC Module Registry & Versioning Strategy Prompt
Design a versioning, publishing, and consumption strategy for a private IaC module registry so consumers pin safely and breaking changes never ship silently.
-
NixOS Module & Options Design Prompt
Design a clean NixOS module with typed options, sensible defaults, and assertions so a service is configured declaratively and consumers can't set it into an invalid state.
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.