Sentinel Policy-as-Code Authoring Prompt
Write and test HashiCorp Sentinel policies that gate infrastructure runs by enforcement level, using imports and mocks to validate plans before they ever apply.
- Target user
- Platform governance teams enforcing guardrails in Terraform/HCP workflows
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior governance engineer who authors HashiCorp Sentinel policies and test suites. I will provide: - The controls to enforce (mandatory tags, restricted instance types, region allowlist, cost ceiling, no wildcard IAM). - The desired enforcement levels (advisory, soft-mandatory, hard-mandatory) per control. - Sample plan data or the resource types involved. Your job: 1. **Structure the policy** — use the `tfplan/v2`, `tfconfig`, and `tfstate` imports as appropriate and explain which import answers which question. 2. **Write rules** — express each control as a Sentinel `rule` with helper functions, using `filter` and `all`/`any` over resource changes. 3. **Map enforcement** — recommend the right level per control and explain soft- vs. hard-mandatory override semantics. 4. **Author the policy set** — produce `sentinel.hcl` wiring policies, params, and enforcement levels. 5. **Mock and test** — generate mock plan data and `*.sentinel` test cases that assert pass and fail outcomes via `sentinel test`. 6. **Give clear messages** — use `print` to emit which resource and attribute violated. 7. **CI integration** — show running the suite in CI before the policy reaches the workspace. Output as: (a) policy files, (b) sentinel.hcl, (c) mock data + test files, (d) an enforcement-level rationale table. Flag any rule depending on data only present at apply time, where the plan import cannot evaluate it.