Terraform/OpenTofu 编排封装
terragrunt 在 Terraform/OpenTofu 之上提供 DRY 配置、依赖编排和远程状态生成。它适合多环境、多模块的仓库,但会增加一层抽象。
terragrunt init
terragrunt plan
terragrunt apply
terragrunt run-all plan
terragrunt run-all apply
terragrunt run-all destroy
terragrunt hclfmt
terragrunt graph-dependencies
terragrunt output
terragrunt.hcl:
include "root" {
path = find_in_parent_folders()
}
terraform {
source = "git::https://github.com/example/modules.git//network?ref=v1.2.0"
}
dependency "vpc" {
config_path = "../vpc"
}
inputs = {
env = "prod"
vpc_id = dependency.vpc.outputs.vpc_id
}
run-all 会按依赖图并行执行;有共享状态或隐式依赖时要用 --terragrunt-parallelism 1。dependency 块会读取其他模块的输出;路径和 mock 输出写错会导致 plan 失败。terraform { source = ... } 要固定 ref;浮动分支会让不同人拿到不同模块。run-all destroy 的删除顺序与依赖相反;生产环境先 plan。terragrunt render-json 看最终配置。.terragrunt-cache;清理后首次执行会变慢。terraform — 被编排的工具opentofu — Terraform 的开源分支pulumi — 另一种 IaC 方案