Terraform Provider-Defined Functions Authoring Prompt
Design, implement, and document provider-defined functions (Terraform 1.8+) in a Go provider so configs can call typed, side-effect-free helpers instead of brittle string-munging in HCL.
- Target user
- Provider authors and platform engineers extending Terraform with custom functions
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior Terraform/IaC engineer who has shipped production providers with the terraform-plugin-framework and knows that provider-defined functions (1.8+) must be pure, deterministic, and side-effect-free. I will provide: - The transformation or computation I want to expose as a function (e.g. CIDR math, naming/encoding helpers, structured ID parsing) - The provider's existing Go package layout and framework version - The input/output types I expect callers to use Your job: 1. **Validate the use case** — confirm the logic is pure and deterministic (no network/clock/random); if it isn't, it belongs in a data source or resource, not a function. State this explicitly. 2. **Design the signature** — define parameters, variadic params, return type, and how `null`/unknown values flow through. Map HCL types to framework attr types precisely. 3. **Implement in Go** — write the `function.Function` with `Definition`, `Metadata`, and `Run`; show error handling via `resp.Error` and how to surface argument-position errors. 4. **Register and version** — wire the function into the provider's `Functions()` list and note the minimum `required_version` (`>= 1.8`) and provider schema implications. 5. **Test** — write unit tests for the `Run` logic plus a `.tftest.hcl`/acceptance test invoking the function from real config, covering edge cases (empty input, null, unknown during plan). 6. **Document** — generate the registry doc stub and an HCL usage example showing `provider::name::func(...)` syntax. Output as: (a) the Go function implementation, (b) the registration snippet, (c) unit + config-level tests, (d) the registry doc block, (e) an HCL usage example with version constraint. Caution: functions run during plan and apply — never embed I/O, mutable global state, or randomness; review the plan to confirm outputs are stable across runs before relying on them.
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
-
Terraform Provider Development Scaffolding Prompt
Scaffold a custom Terraform provider with the Plugin Framework — resources, data sources, schema, CRUD, import, and acceptance tests — for wrapping an internal API that has no existing provider.
-
Terraform CIDR & Subnet Calculation Design Prompt
Get cidrsubnet/cidrsubnets/cidrhost math right so subnet layouts are non-overlapping, correctly sized, and stable as the network grows.
-
Dynamic Multi-Instance Provider Configuration with for_each Prompt
Use for_each on provider configurations (Terraform 1.9+) to fan resources out across many accounts or regions without hand-written aliases
-
Terraform Provider Mirror for Air-Gapped CI Prompt
Set up a Terraform provider mirror (filesystem or network mirror) and `.terraformrc` so `terraform init` works in air-gapped or rate-limited CI without reaching the public registry.
More Terraform prompts & error guides
Browse every Terraform 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.