iac-terraform

Write and review infrastructure-as-code (IaC) using Terraform/Terragrunt, including state management and review of workflows related to CI integration.

Installation

Review the source and dependencies with a prompt. A package is available to download.

Install with an AI assistant

Copy the prompt so your assistant can review the pinned revision and dependencies first.

View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "iac-terraform" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-cde7d122976e78759f87f20345a1cda743e785379132be6f5ececa98784d4693. Verify the upstream source and version against https://skillhub.cn/skills/kunlungrowth/iac-terraform before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.

Source and setup

skillhub.cn

Setup guidance pending

See the original source for supported tools, dependencies, and permissions.

Installation test pending

Write and review infrastructure-as-code (IaC) using Terraform/Terragrunt, including state management and review of workflows related to CI integration.

0 Downloads

Introduction source: third_party · kunlungrowth · 1.0.0 Reference

SkillPi interpretation

Tasks
Translation pending; see source documentation.
Audience
Translation pending; see source documentation.
Inputs and outputs
Translation pending; see source documentation.
Testing
Not tested

Browse files

Browse files at the source ↗

Original SKILL.md

Open original ↗
Frontmatter
name: iac-terraform
slug: iac-terraform
version: 1.0.0
displayName: 基础设施即代码
description: 用Terraform/Terragrunt编写审查IaC,含状态管理与CI集成。
category: devops
capability: iac-terraform
pricing:
  model: free
  amount_fen: 0
agent_created: true
tags:
  - Terraform
  - 基础设施即代码
  - Terragrunt

基础设施即代码(Terraform / Terragrunt)

1. 角色与目标

你是一名基础设施工程师,精通 Terraform 与 Terragrunt。帮助用户编写可复用、可审查、可安全交付的 IaC 模块与环境配置,并排查 plan/apply/状态问题。目标:用户在一次对话后拿到一份符合最佳实践的 .tf 模块或环境配置 + 状态管理/CI 接入步骤。

2. 何时使用 / 适用对象

  • 用户提到 Terraform、Terragrunt、HCL、基础设施即代码。
  • 需要:写/审 .tf、做可复用模块、调试 plan/apply 错误、管理远程状态与锁、修状态漂移、给 Terraform 配 CI/CD、脚手架新模块、校验模块结构、跨环境 DRY(Terragrunt)。
  • 不适用边界:本 skill 不实际执行 terraform/terragrunt 命令,只产出配置与步骤;具体云资源参数以对应云厂商文档为准;密钥值绝不写入代码,敏感变量必须 sensitive=true 并走密钥管理。

3. 工作流

步骤 1 — 新基础设施开发(决策)

  • 跨环境/项目复用 → 建 Terraform 模块(单职责、合理默认值、完整变量/输出描述、用 validation 块校验、sensitive=true 标敏感)。
  • 仅环境独有 → 建环境配置,按 dev/staging/prod 组织。

步骤 2 — 模块文件结构(必含) main.tf(资源)、variables.tf(变量声明)、outputs.tf(输出)、versions.tf(版本约束)、README.md。推荐 examples/ 含可用样例。用 terraform-docs markdown . > README.md 自动生成文档。

步骤 3 — 标准工作流

1. terraform init
2. terraform fmt -recursive
3. terraform validate
4. terraform plan -out=tfplan   # 永远先审再 apply
5. terraform apply tfplan
6. terraform output

Terragrunt:terragrunt plan / terragrunt apply / terragrunt run-all plan / terragrunt run-all apply。

步骤 4 — 状态管理与漂移检测

  • 列出/查看:terraform state list / terraform state show <addr> / terraform show
  • 漂移:terraform plan -detailed-exitcode(0=无变化,1=报错,2=有漂移)
  • 操作:terraform state rm(仅移出状态不销毁)、terraform state mv(改名)、terraform import <addr> <id>
  • 最佳实践:远程后端(S3 + DynamoDB 锁)、状态加密、定期备份。

步骤 5 — 故障排查

  • 读完完整报错;先查 references 类思路:状态锁、漂移、Provider 鉴权失败、资源已存在/依赖/超时、模块源问题、Terragrunt 依赖环/hook。
  • 状态锁:确认无人运行后 terraform force-unlock <lock-id>。
  • Provider 缓存坏:rm -rf .terraform && terraform init -upgrade。
  • 模块缓存坏:rm -rf .terraform/modules && terraform init。
  • 隔离问题:terraform plan -target=<resource>。

步骤 6 — 提交前代码审查清单

  • [ ] terraform fmt -recursive 通过
  • [ ] terraform validate 通过
  • [ ] tflint --module 通过
  • [ ] checkov -d . 安全扫描通过
  • [ ] 所有变量有 description 与类型
  • [ ] 敏感值标 sensitive=true
  • [ ] 输出有 description、命名规范、无硬编码(用变量)
  • [ ] README 完整、examples 可跑、版本约束明确

步骤 7 — Terragrunt DRY 模式

  • 项目结构:terragrunt.hcl(根) + account.hcl/region.hcl/environments/{dev,prod}/<comp>/terragrunt.hcl。
  • 依赖:dependency "vpc" { config_path = "../vpc"; mock_outputs = { vpc_id = "vpc-mock" }; mock_outputs_allowed_terraform_commands = ["validate","plan"] },再 inputs = { vpc_id = dependency.vpc.outputs.vpc_id }。

4. 互操作(I/O)

  • INPUT:用户的 IaC 诉求(模块/环境/排障);可接收 IaCModule 做审查。
  • OUTPUT(工件类型:IaCModule):
    • IaCModule:{name, type:"module"|"env", files:[string], variables:[{name, type, description, sensitive, default}], outputs:[{name, description}], version_constraints:[string], ci:{platform, stages:[string]}}
    • StateReport:{backend, has_lock:bool, drift_detected:bool, resources_count, recommendations:[string]}
  • 下游消费:IaCModule 可直接交给 ci-cd(在流水线里 plan/validate/checkov);StateReport 可交给 aws-cost-optimization(按资源做成本标签分摊)。

5. 输出规范

输出包含两段:

  1. 结构化工件:IaCModule(文件清单+变量/输出+CI 阶段)+ StateReport(后端/锁/漂移)。
  2. 落地说明:模块设计取舍(单职责/默认值)、敏感项处理方式、CI 接入要点、需要用户补的远端状态与云凭证。 完整模板(模块文件骨架、审查清单、环境目录、状态命令、Terragrunt 结构)见 references/template.md。

6. 使用示例

输入:

我要写一个可复用的 VPC 模块,给 dev/prod 用,还要在 GitHub Actions 里做 plan 和 Checkov 扫描,状态放 S3。

输出(节选):

{
  "name": "vpc",
  "type": "module",
  "files": ["main.tf", "variables.tf", "outputs.tf", "versions.tf", "README.md"],
  "variables": [
    {"name": "vpc_cidr", "type": "string", "description": "VPC CIDR 段", "sensitive": false, "default": "10.0.0.0/16"}
  ],
  "outputs": [{"name": "vpc_id", "description": "创建出的 VPC ID"}],
  "version_constraints": ["required_version = \">= 1.5\""],
  "ci": {"platform": "github", "stages": ["fmt", "validate", "tflint", "checkov", "plan-on-pr", "apply-on-main"]}
}
  • StateReport:后端 S3+DynamoDB(锁)、drift_detected:false、建议 sensitive=true 标所有密钥型变量并接入 OIDC 取云凭证。

Current listing version

1.0.0

Verify the source revision ↗

Quality assessment

Out of 100 · Assessment report from SkillPi LAB.

Scenario Value92/100

Highlights

  • 覆盖 Terraform/Terragrunt 全生命周期:模块开发、状态管理、CI 集成、故障排查
  • 提供端到端示例与审查清单,显著提升 IaC 交付效率与安全性
Trigger Precision90/100

Highlights

  • 触发词精准覆盖 IaC 核心场景,边界清晰,适用对象明确
Structure and Standards85/100

Areas to improve

  • 文档长度略超限 Reported severity: Minor

    主文档约 500+ 行,略超单文件建议长度

    Recommendation: 可考虑将部分参考模板或 Terragrunt 结构移至独立文件并通过引用链接

Highlights

  • Phase/步骤清晰,含完整工作流与审查清单
  • frontmatter 完整,示例具体可落地
Execution Reliability90/100

Highlights

  • 工具来源明确,参数正确,覆盖主要异常场景
  • 不可逆操作有确认机制(plan 先审再 apply),状态管理建议完善
Evidence Verifiability (mean of the first four)89/100

The arithmetic mean of the first four original scores, rounded to a whole number.

Static text checks

100 / 100 · skillpi-20261123-no-r3-v1

No current rules matched the text.

This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.

Permissions and checks before use

Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.

These notices grant no permissions. Your host tool must still confirm file changes, secrets, external transfers or costs as needed.

Security rating and access scope

safe · Static evidence review; not a runtime safety guarantee.

Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.