Files
nexus/wiki/entities/Terragrunt.md

101 lines
2.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "Terragrunt"
type: entity
tags:
- devops
- iac
- terraform
- automation
created: 2026-04-26
---
# Terragrunt
## Definition
Terragrunt 是由 Gruntwork 开发的**Terraform 轻量封装工具**,核心目标是贯彻 DRYDon't Repeat Yourself原则简化多环境、多账户 Terraform 配置的管理。
## Core Value
Terragrunt 对 Terraform 的核心改进在于**减少重复配置**
| 问题 | Terraform 方案 | Terragrunt 方案 |
|------|---------------|----------------|
| provider 块重复 | 每个环境独立声明 | `terragrunt.hcl` 统一管理 |
| remote_state 块重复 | 多处硬编码 | 模板化复用 |
| 模块引用方式 | 固定分支引用 | 版本锁定引用 |
| 多环境同步 | 手动复制配置 | `include` 块继承 |
**来源**: [[ctp-topic-48-terraform-vs-terragrunt]]
## Compatibility
Terragrunt **完全兼容** Terraform 的所有命令和 HCL 语法:
- `terragrunt plan` → 底层调用 `terraform plan`
- `terragrunt apply` → 底层调用 `terraform apply`
- 所有 Terraform HCL 块和属性完全兼容
这意味着 Terragrunt **不是** Terraform 的替代品,而是增强层。
**来源**: [[ctp-topic-48-terraform-vs-terragrunt]]
## Key Features
### DRY Remote State
通过 `remote_state` 块集中管理状态文件位置:
```hcl
remote_state {
backend = "s3"
config = {
bucket = "my-terraform-state"
key = "${path_relative_to_include()}/terraform.tfstate"
region = "eu-west-1"
encrypt = true
}
}
```
### Provider Management
跨环境统一 provider 配置,避免在每个模块中重复声明。
### Include & Inheritance
通过 `include` 块实现配置的继承与覆盖:
```hcl
include {
path = find_in_parent_folders("root.hcl")
}
```
## Use Case: Micro Focus Labs Landing Zone
Micro Focus Labs Landing Zone 使用 Terragrunt 管理多账户配置,所有资源通过 Terraform/Terragrunt 管理Jenkins 流水线扫描 GitHub 仓库变更触发 plan/apply。
**来源**: [[ctp-topic-3-deploy-and-maintain-infrastructure]], [[ctp-topic-25-labs-landing-zone-overview-itom-teams]]
## Ecosystem Position
```
Terraform Ecosystem
├── Terraform (HashiCorp) — 核心 IaC 引擎
├── Terragrunt (Gruntwork) — DRY 封装层 ←
├── Terraform Enterprise (HashiCorp) — 企业 CI 平台
└── Gruntwork Library (Gruntwork) — 预建模块库
```
## Related Entities
- [[Terraform]] — Terragrunt 包装的核心引擎
- [[HashiCorp]] — Terraform 创立公司
- [[Gruntwork]] — Terragrunt 开发公司
## Related Concepts
- [[Infrastructure-as-Code]] — Terragrunt 的应用领域
- [[DRY Principle]] — Terragrunt 的设计哲学
## Related Sources
- [[ctp-topic-48-terraform-vs-terragrunt]]
- [[ctp-topic-3-deploy-and-maintain-infrastructure]]
- [[ctp-topic-15-working-with-renovatebot]]Renovate Bot 扫描 Terragrunt 配置)