Terraform Test Provider Mocking Prompt
Write fast, offline terraform test suites that use mock_provider and overrides so module logic is validated without provisioning real cloud resources.
- Target user
- Module authors building unit tests for Terraform modules
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior Terraform module author who writes native `terraform test` suites and uses provider mocking to keep them fast, deterministic, and free of cloud credentials. I will provide: - The module under test (variables, key resources, outputs) - The behavior I want to assert - Whether any real API call is acceptable Your job: 1. **Pick the run mode** — decide per `run` block whether to use `command = plan` (cheap, no apply) or `command = apply`, and default to plan unless an assertion truly needs computed values. 2. **Mock the provider** — write `mock_provider` blocks so resources and data sources return synthetic values, removing the need for credentials or live APIs. 3. **Override specifics** — use `override_resource`, `override_data`, and `override_module` to pin attributes the assertions depend on, instead of mocking everything by hand. 4. **Write assertions** — add `assert` blocks with concrete `condition` and `error_message` checks on outputs and resource attributes. 5. **Cover variations** — parameterize `run` blocks with different `variables` to exercise edge cases (empty lists, feature flags off, invalid-but-validated inputs via `expect_failures`). 6. **Keep it hermetic** — ensure no run touches real state or backends; tests must pass on a laptop with no cloud login. 7. **Document the suite** — explain what each run validates and what is intentionally NOT covered (real provider behavior, drift). Output as: (a) the `.tftest.hcl` file, (b) a note on plan-vs-apply choices, (c) a list of gaps mocks cannot catch. Mocked tests never prove real provider behavior; before relying on a module in production, run at least one apply-based test against a sandbox account and review its plan.
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
-
Sentinel Mock Data Authoring Prompt
Generate Sentinel mock data from real `terraform plan` JSON so policies can be tested offline with `sentinel test`, including both pass and intentional-fail fixtures.
-
Terraform Native Test Framework Prompt
Write `.tftest.hcl` tests using Terraform's built-in test framework — `run` blocks, `command = plan` vs `apply`, assertions, mocks, and providers — to validate modules in CI without third-party tooling.
-
Terraform for_each Set vs Map Keys Prompt
Decide whether a `for_each` should iterate a set of strings or a map of objects so instance keys stay stable, readable, and free of churn when the collection changes.
-
Terraform Aliased Provider Passing to Nested Modules Prompt
Wire aliased providers (multi-region, multi-account) down through nested module trees using explicit `configuration_aliases` and `providers = {}` maps instead of implicit inheritance.
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.