# Terraform 风格指南 (terraform-style-guide)

> 按照 HashiCorp 官方风格约定和最佳实践生成与维护 Terraform HCL 代码。

- 分类: [代码规范](https://www.myskills.tools/zh/categories/code-standards/)
- 来源: 官方 · HashiCorp
- 代码仓库: https://github.com/hashicorp/agent-skills/tree/main/plugins/terraform/skills/terraform-style-guide (commit 4451cec)
- SKILL.md: https://github.com/hashicorp/agent-skills/blob/main/plugins/terraform/skills/terraform-style-guide/SKILL.md
- 安装命令: `npx skills add hashicorp/agent-skills --skill terraform-style-guide`
- 许可证: MPL-2.0
- 兼容工具: Claude Code, Codex
- 权限: read-repo ✓, write-files ✓, shell ✓, network ✗
- 状态: 扫描通过
- 规则扫描无法覆盖所有风险，安装前请阅读此技能的 SKILL.md，确认权限后再使用。

## 核心规则

每个变量都必须有 type 和 description，每个输出都必须有 description。

## 执行阶段

1. **按顺序生成** — 先写 provider 和版本约束，再写数据源，按依赖顺序建资源，最后为关键属性添加输出，可配置值都用变量。
2. **组织文件** — 拆分为 terraform.tf、providers.tf、main.tf、variables.tf、outputs.tf、locals.tf，变量和输出按字母排序。
3. **格式与命名** — 两空格缩进并对齐等号，元参数在前、lifecycle 在后；名称用小写下划线的单数名词，多实例优先 for_each 而非 count。
4. **安全与版本** — 显式固定版本，敏感值标记 sensitive，不硬编码凭据，按 SECURITY.md 启用加密；不提交 state 文件，提交 .terraform.lock.hcl。
5. **校验** — 提交前运行 terraform fmt -recursive 和 terraform validate，可再用 tflint、checkov 或 tfsec，并对照审查清单。

## 权限说明（据 SKILL.md 整理）

| 能力 | 是否需要 | 说明 |
|---|---|---|
| 文件写入 | 需要 | 生成和修改 Terraform 配置文件（.tf）。 |
| 命令执行 | 部分需要 | 仅运行 terraform fmt、terraform validate 以及 tflint、checkov、tfsec 等检查工具。 |
| 网络外发 | 不需要 | 技能未要求访问网络，也不执行 plan 或 apply。 |

## SKILL.md 头部信息（YAML）

```yaml
---
name: terraform-style-guide
description: Generate Terraform HCL code following HashiCorp's official style conventions and best practices. Use when writing, reviewing, or generating Terraform configurations.
metadata:
  lifecycle-status: active
---
```

首页: https://www.myskills.tools/zh/ · URL: https://www.myskills.tools/zh/skills/terraform-style-guide/
