关于 terraform-policy
terraform-policy 是用于编写和测试 Terraform Policy 文件的实用技能。它将策略创建或转换交给 tfpolicy-author,将测试交给 tfpolicy-test,并依据已安装的 tfpolicy CLI 版本提供指导。
主要功能
- 01编写和转换 .policy.hcl 文件
- 02编写和调试 .policytest.hcl 文件
- 03将任务分派给专用子技能
- 04区分不同 tfpolicy CLI 版本的行为
适用场景
- 01根据需求创建策略
- 02转换 Sentinel 策略
- 03调试策略测试
- 04选择适用于当前版本的编写指导
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ hashicorp/ agent-skills --skill terraform-policy安装后在 Claude Code 中输入/terraform-policy确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
.gitignoreexamples/conversion/cloudfront-associated-with-waf/cloudfront-associated-with-waf.policy.hclcloudfront-associated-with-waf.sentinelREADME.mdexamples/conversion/cloudtrail-server-side-encryption-enabled/cloudtrail-server-side-encryption-enabled.policy.hclcloudtrail-server-side-encryption-enabled.sentinelREADME.mdexamples/conversion/dms-endpoint-should-be-ssl-configured/dms-endpoint-should-be-ssl-configured.policy.hcldms-endpoint-should-be-ssl-configured.sentinelREADME.mdexamples/conversion/dms-endpoints-should-use-ssl/dms-endpoints-should-use-ssl.policy.hcldms-endpoints-should-use-ssl.sentinelREADME.mdexamples/conversion/ec2-network-acl-should-have-subnet-ids/ec2-network-acl-should-have-subnet-ids.policy.hclec2-network-acl-should-have-subnet-ids.sentinelREADME.mdexamples/conversion/ec2-vpc-default-security-group-no-traffic/ec2-vpc-default-security-group-no-traffic.policy.hclec2-vpc-default-security-group-no-traffic.sentinelREADME.mdexamples/conversion/efs-access-point-should-enforce-user-identity/efs-access-point-should-enforce-user-identity.policy.hclefs-access-point-should-enforce-user-identity.sentinelREADME.mdexamples/conversion/elasticache-redis-replication-group-encryption-at-transit-enabled/elasticache-redis-replication-group-encryption-at-transit-enabled.policy.hclelasticache-redis-replication-group-encryption-at-transit-enabled.sentinelREADME.mdexamples/conversion/elasticsearch-encrypted-at-rest/elasticsearch-encrypted-at-rest.policy.hclelasticsearch-encrypted-at-rest.sentinelREADME.mdexamples/conversion/elasticsearch-https-required/elasticsearch-https-required.policy.hclelasticsearch-https-required.sentinel详细介绍
terraform-policy
工具技能 — 调用:tfpolicy-author | tfpolicy-test
适用于:
- 根据描述或需求编写新的
.policy.hcl策略 - 将
.sentinel策略转换为 Terraform Policy - 编写或调试
.policytest.hcl测试文件 - 将 Sentinel 策略库迁移到 Terraform Policy
在提供编写或测试说明之前,请检查已安装的 tfpolicy CLI 版本,并据此调整指导。本技能维护最近两个次要版本系列 0.2.x 和 0.3.x 的指导;发布新的次要版本时,移除最旧的系列并加入新系列。
- 如果 CLI 为
0.2.x(基线版本),编写包含资源或提供程序策略的.policy.hcl文件时,请加入顶层policy { required_providers { ... } }块。tfpolicy validate要求必须包含此块;版本范围验证尽力而为,且不会对resource_policy "*"等通配符目标进行架构验证。tfpolicy test不会根据提供程序架构预先检查模拟的attrs/prior_attrs,core::alltrue/core::anytrue不存在,并且仅在此0.2.x系列中,模拟的resource {}块可以完全省略attrs/prior_attrs。 - 如果 CLI 为
0.3.x或更高版本,上述其他指导仍然适用,但不再允许像0.2.x那样省略资源状态:.policytest.hcl文件中的每个模拟resource {}块都必须声明attrs或prior_attrs;如果两者的计算结果都为空,则会跳过该测试用例(provider {}和module {}模拟不受影响)(参见 tfpolicy-test)。tfpolicy test会复用目标.policy.hcl中现有的顶层policy { required_providers { ... } }块(无需在.policytest.hcl层级单独声明),在运行任何测试之前,根据已解析的提供程序架构验证提供程序、资源和数据源策略,以及core::getdatasource()/core::getresources()参数;如果架构不匹配,整个运行将失败(参见 tfpolicy-test)。也可以使用core::alltrue(list)和core::anytrue(list)——优先使用它们,而不是core::length()列表推导式变通方案(参见 tfpolicy-author)。资源、提供程序和模块策略可以使用meta.tfe_stack和meta.tfe_workspace.tags;在 Stack 评估之外,Stack 字段为空。 - 如果 CLI 版本未知,请先让用户检查版本,或提供明确区分
0.2.x和0.3.x路径的指导。
不适用于:
- 为 Terraform 模块编写
.tftest.hcl文件——请使用terraform-test - 编写通用 Terraform HCL——请使用
terraform-style-guide
路由
| 任务 | 子技能 |
|---|---|
编写或转换 .policy.hcl 策略 | tfpolicy-author |
编写或调试 .policytest.hcl 测试 | tfpolicy-test |
示例
- “阻止未启用加密的 EC2 实例” → tfpolicy-author
- “将此 Sentinel 策略转换为 tfpolicy” → tfpolicy-author
- “为我的 EBS 策略编写 policytest” → tfpolicy-test
故障排除
- 触发了错误的技能? 直接从上方路由表加载子技能。
npx skills add hashicorp/agent-skills/terraform/terraform-policy/skills/tfpolicy-author
npx skills add hashicorp/agent-skills/terraform/terraform-policy/skills/tfpolicy-test
评论
支持 Markdown