Terraform Policy

作者 hashicorpf706481af9b8MPL-2.0890 个星标收录于 2026年10月8日更新于 2026年10月8日仓库3天前更新

Write, test, or convert Terraform Policy files (.policy.hcl, .policytest.hcl, Sentinel→tfpolicy). Triggers: policy.hcl, policytest, convert sentinel, tfpolicy, write a policy.

AI 生成的概览

编写、测试和转换 Terraform Policy 文件(.policy.hcl、.policytest.hcl)以及 Sentinel 策略。

功能
指导根据描述或需求编写 Terraform Policy 的 .policy.hcl 文件,并将 Sentinel 的 .sentinel 策略转换为 Terraform Policy。它还涵盖 .policytest.hcl 测试文件的编写与调试,包括对模拟资源状态的要求。相关指导会根据已安装的 tfpolicy CLI 版本进行调整,覆盖 0.2.x 和 0.3.x 两个版本线。
适用场景
适用于根据需求创建新的 Terraform Policy、迁移 Sentinel 策略库,或编写和调试 policytest 文件。不适用于 Terraform 模块的 .tftest.hcl 文件或一般 Terraform HCL 编写。
运行要求
需要 tfpolicy CLI,在给出编写或测试指导前应先确认其已安装版本。仅为说明性内容,不附带脚本。

terraform-policy

UTILITY SKILL — INVOKES: tfpolicy-author [blocked] | tfpolicy-test [blocked]

USE FOR:

  • Writing a new .policy.hcl policy from a description or requirement
  • Converting a .sentinel policy to Terraform Policy
  • Writing or debugging a .policytest.hcl test file
  • Migrating a Sentinel policy library to Terraform Policy

Before giving authoring or testing instructions, check the installed tfpolicy CLI version and tailor guidance accordingly. This skill maintains guidance for the two most recent minor lines, 0.2.x and 0.3.x; when a new minor ships, drop the oldest line and add the new one.

  • If the CLI is 0.2.x (baseline), include a top-level policy { required_providers { ... } } block when authoring .policy.hcl files containing resource or provider policies. It is mandatory for tfpolicy validate; version-range validation is best effort, and wildcard targets such as resource_policy "*" are not schema-validated. tfpolicy test does not preflight mocked attrs/prior_attrs against provider schemas, core::alltrue/core::anytrue do not exist, and, only in this 0.2.x line, mock resource {} blocks may omit attrs/prior_attrs entirely.
  • If the CLI is 0.3.x or newer, the other guidance above still applies, but the 0.2.x allowance for omitting resource state does not: every mock resource {} block in .policytest.hcl files must declare attrs or prior_attrs; if both evaluate to empty, the test case is skipped (provider {} and module {} mocks are unaffected) (see tfpolicy-test [blocked]). tfpolicy test reuses the target .policy.hcl's existing top-level policy { required_providers { ... } } block (there is no separate .policytest.hcl-level declaration) to validate provider, resource, and data-source policies and core::getdatasource()/core::getresources() arguments against resolved provider schemas before any test runs, failing the whole run on a schema mismatch (see tfpolicy-test [blocked]). core::alltrue(list) and core::anytrue(list) are also available — prefer them over the core::length() list-comprehension workaround (see tfpolicy-author [blocked]). meta.tfe_stack and meta.tfe_workspace.tags are available to resource, provider, and module policies; Stack fields are empty outside Stack evaluations.
  • If the CLI version is unknown, ask the user to check it first or provide guidance that clearly distinguishes the 0.2.x and 0.3.x paths.

DO NOT USE FOR:

  • Writing .tftest.hcl files for Terraform modules — use terraform-test
  • General Terraform HCL authoring — use terraform-style-guide

Routing

TaskSub-skill
Write or convert a .policy.hcl policytfpolicy-author [blocked]
Write or debug a .policytest.hcl testtfpolicy-test [blocked]

Examples

  • "Block EC2 instances without encryption" → tfpolicy-author [blocked]
  • "Convert this Sentinel policy to tfpolicy" → tfpolicy-author [blocked]
  • "Write a policytest for my EBS policy" → tfpolicy-test [blocked]

Troubleshooting

  • Wrong skill triggered? Load the sub-skill directly from the routing table above.
bash
npx skills add hashicorp/agent-skills/terraform/terraform-policy/skills/tfpolicy-authornpx skills add hashicorp/agent-skills/terraform/terraform-policy/skills/tfpolicy-test

来源与署名

来源:hashicorp/agent-skills位于plugins/terraform/skills/terraform-policy提交f706481

许可证: MPL-2.0

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架