terraform-documentation-generator
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTerraform Documentation Generator
Terraform文档生成器
This skill helps generate and maintain Terraform module documentation using terraform-docs.
本功能可帮助您使用terraform-docs生成并维护Terraform模块文档。
When to Use
使用场景
Use this skill when:
- Creating README.md for a new module
- Updating documentation after module changes
- Generating input/output reference tables automatically
- Ensuring consistent documentation across modules
在以下场景中使用本功能:
- 为新模块创建README.md
- 模块变更后更新文档
- 自动生成输入/输出参考表格
- 确保跨模块的文档一致性
Using terraform-docs
使用terraform-docs
Installation
安装
bash
undefinedbash
undefinedmacOS
macOS
brew install terraform-docs
brew install terraform-docs
Linux
Linux
curl -sSLo ./terraform-docs.tar.gz https://terraform-docs.io/dl/latest/terraform-docs-linux-amd64.tar.gz
tar -xzf terraform-docs.tar.gz
chmod +x terraform-docs
mv terraform-docs /usr/local/bin/
curl -sSLo ./terraform-docs.tar.gz https://terraform-docs.io/dl/latest/terraform-docs-linux-amd64.tar.gz
tar -xzf terraform-docs.tar.gz
chmod +x terraform-docs
mv terraform-docs /usr/local/bin/
Or use Go
Or use Go
go install github.com/terraform-docs/terraform-docs@latest
undefinedgo install github.com/terraform-docs/terraform-docs@latest
undefinedBasic Usage
基础用法
bash
undefinedbash
undefinedGenerate markdown documentation
Generate markdown documentation
terraform-docs markdown table . > README.md
terraform-docs markdown table . > README.md
Preview without writing
Preview without writing
terraform-docs markdown table .
terraform-docs markdown table .
Generate for specific directory
Generate for specific directory
terraform-docs markdown table ./modules/vpc > ./modules/vpc/README.md
undefinedterraform-docs markdown table ./modules/vpc > ./modules/vpc/README.md
undefinedConfiguration File
配置文件
Create in module root for consistent formatting:
.terraform-docs.ymlyaml
formatter: "markdown table"
header-from: main.tf
sections:
show:
- header
- requirements
- providers
- inputs
- outputs
- resources
content: |-
{{ .Header }}
## Usage
```hcl
module "example" {
source = "./modules/example"
# Add your example here
}{{ .Requirements }}
{{ .Providers }}
{{ .Inputs }}
{{ .Outputs }}
{{ .Resources }}
output:
file: "README.md"
mode: inject
template: |-
<!-- BEGIN_TF_DOCS -->
{{ .Content }}
<!-- END_TF_DOCS -->
sort:
enabled: true
by: required
undefined在模块根目录创建以确保格式一致:
.terraform-docs.ymlyaml
formatter: "markdown table"
header-from: main.tf
sections:
show:
- header
- requirements
- providers
- inputs
- outputs
- resources
content: |-
{{ .Header }}
## Usage
```hcl
module "example" {
source = "./modules/example"
# Add your example here
}{{ .Requirements }}
{{ .Providers }}
{{ .Inputs }}
{{ .Outputs }}
{{ .Resources }}
output:
file: "README.md"
mode: inject
template: |-
<!-- BEGIN_TF_DOCS -->
{{ .Content }}
<!-- END_TF_DOCS -->
sort:
enabled: true
by: required
undefinedAuto-Generate Documentation
自动生成文档
bash
undefinedbash
undefinedWith config file
With config file
terraform-docs .
terraform-docs .
Inject into existing README between markers
Inject into existing README between markers
terraform-docs markdown table --output-file README.md --output-mode inject .
undefinedterraform-docs markdown table --output-file README.md --output-mode inject .
undefinedOutput Formats
输出格式
bash
undefinedbash
undefinedMarkdown table (most common)
Markdown table (most common)
terraform-docs markdown table .
terraform-docs markdown table .
Markdown document
Markdown document
terraform-docs markdown document .
terraform-docs markdown document .
JSON
JSON
terraform-docs json .
terraform-docs json .
YAML
YAML
terraform-docs yaml .
undefinedterraform-docs yaml .
undefinedDocumentation Best Practices
文档最佳实践
Add Header Comments
添加头部注释
Add description at top of main.tf:
hcl
/**
* # Terraform AWS VPC Module
*
* Creates a VPC with public and private subnets across multiple availability zones.
*
* ## Features
*
* - Multi-AZ VPC with public and private subnets
* - NAT Gateway for private subnet internet access
* - Configurable CIDR blocks
*/
resource "aws_vpc" "main" {
# ...
}terraform-docs will use this as the README header.
在main.tf顶部添加描述:
hcl
/**
* # Terraform AWS VPC Module
*
* Creates a VPC with public and private subnets across multiple availability zones.
*
* ## Features
*
* - Multi-AZ VPC with public and private subnets
* - NAT Gateway for private subnet internet access
* - Configurable CIDR blocks
*/
resource "aws_vpc" "main" {
# ...
}terraform-docs会将此内容作为README的头部。
Document Variables Clearly
清晰记录变量
hcl
variable "vpc_cidr" {
description = "CIDR block for VPC (e.g., 10.0.0.0/16)"
type = string
validation {
condition = can(cidrhost(var.vpc_cidr, 0))
error_message = "Must be valid IPv4 CIDR."
}
}
variable "enable_nat_gateway" {
description = "Enable NAT Gateway for private subnet internet access"
type = bool
default = true
}hcl
variable "vpc_cidr" {
description = "CIDR block for VPC (e.g., 10.0.0.0/16)"
type = string
validation {
condition = can(cidrhost(var.vpc_cidr, 0))
error_message = "Must be valid IPv4 CIDR."
}
}
variable "enable_nat_gateway" {
description = "Enable NAT Gateway for private subnet internet access"
type = bool
default = true
}Document Outputs
记录输出
hcl
output "vpc_id" {
description = "ID of the VPC"
value = aws_vpc.main.id
}
output "private_subnet_ids" {
description = "List of private subnet IDs for use with internal resources"
value = aws_subnet.private[*].id
}hcl
output "vpc_id" {
description = "ID of the VPC"
value = aws_vpc.main.id
}
output "private_subnet_ids" {
description = "List of private subnet IDs for use with internal resources"
value = aws_subnet.private[*].id
}Workflow Integration
工作流集成
Pre-commit Hook
Pre-commit钩子
Add to :
.pre-commit-config.yamlyaml
repos:
- repo: https://github.com/terraform-docs/terraform-docs
rev: "v0.16.0"
hooks:
- id: terraform-docs-go
args: ["markdown", "table", "--output-file", "README.md", "."]添加到:
.pre-commit-config.yamlyaml
repos:
- repo: https://github.com/terraform-docs/terraform-docs
rev: "v0.16.0"
hooks:
- id: terraform-docs-go
args: ["markdown", "table", "--output-file", "README.md", "."]CI/CD Integration
CI/CD集成
yaml
undefinedyaml
undefinedGitHub Actions example
GitHub Actions example
- name: Generate terraform docs uses: terraform-docs/gh-actions@v1 with: working-dir: . output-file: README.md output-method: inject
undefined- name: Generate terraform docs uses: terraform-docs/gh-actions@v1 with: working-dir: . output-file: README.md output-method: inject
undefinedQuick Reference
快速参考
bash
undefinedbash
undefinedGenerate docs for current directory
Generate docs for current directory
terraform-docs markdown table . > README.md
terraform-docs markdown table . > README.md
Update existing README (between markers)
Update existing README (between markers)
terraform-docs markdown table --output-file README.md --output-mode inject .
terraform-docs markdown table --output-file README.md --output-mode inject .
Generate for all modules
Generate for all modules
find . -type f -name "*.tf" -exec dirname {} ; | sort -u | xargs -I {} terraform-docs markdown table {} --output-file {}/README.md
find . -type f -name "*.tf" -exec dirname {} ; | sort -u | xargs -I {} terraform-docs markdown table {} --output-file {}/README.md
Validate documentation is up to date
Validate documentation is up to date
terraform-docs markdown table . | diff - README.md
undefinedterraform-docs markdown table . | diff - README.md
undefinedDocumentation Checklist
文档检查清单
- terraform-docs installed
- config created (optional)
.terraform-docs.yml - Header comment added to main.tf
- All variables have clear descriptions
- All outputs have descriptions
- Usage example added to README
- Documentation generated with
terraform-docs - Pre-commit hook configured (optional)
- 已安装terraform-docs
- 已创建配置(可选)
.terraform-docs.yml - 已在main.tf中添加头部注释
- 所有变量均有清晰描述
- 所有输出均有描述
- README中已添加使用示例
- 已使用生成文档
terraform-docs - 已配置Pre-commit钩子(可选)