terraform-module-scaffolder

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Terraform Module Scaffolder

Terraform 模块脚手架生成工具

This skill helps create well-structured Terraform modules following best practices and conventions.
该功能可帮助遵循最佳实践与规范创建结构清晰的Terraform模块。

When to Use

适用场景

Use this skill when:
  • Creating a new Terraform module from scratch
  • Setting up standardized module structure
  • Need templates for common AWS/Azure/GCP resources
  • Want to ensure module follows Terraform conventions
使用该功能的场景:
  • 从零开始创建新的Terraform模块
  • 搭建标准化的模块结构
  • 需要AWS/Azure/GCP常见资源的模板
  • 希望确保模块遵循Terraform规范

Module Structure

模块结构

Generate modules with this standard structure:
module-name/
├── main.tf           # Primary resource definitions
├── variables.tf      # Input variables
├── outputs.tf        # Output values
├── versions.tf       # Provider and Terraform version constraints
├── README.md         # Module documentation
└── examples/         # Usage examples (optional)
    └── basic/
        └── main.tf
生成的模块将采用以下标准结构:
module-name/
├── main.tf           # Primary resource definitions
├── variables.tf      # Input variables
├── outputs.tf        # Output values
├── versions.tf       # Provider and Terraform version constraints
├── README.md         # Module documentation
└── examples/         # Usage examples (optional)
    └── basic/
        └── main.tf

Instructions

操作步骤

1. Gather Requirements

1. 收集需求

Ask the user:
  • What is the module name?
  • What cloud provider (AWS, Azure, GCP, multi-cloud)?
  • What resources should the module create?
  • Any specific requirements or constraints?
询问用户以下信息:
  • 模块名称是什么?
  • 目标云服务商是哪个(AWS、Azure、GCP、多云)?
  • 模块需要创建哪些资源?
  • 是否有特定需求或限制条件?

2. Create Core Files

2. 创建核心文件

main.tf - Include:
  • Resource definitions with clear naming
  • Local values for computed attributes
  • Data sources if needed
variables.tf - Include:
  • Required variables first, then optional
  • Clear descriptions for each variable
  • Sensible defaults where appropriate
  • Type constraints (string, number, bool, list, map, object)
  • Validation rules for critical inputs
outputs.tf - Include:
  • Resource IDs and ARNs
  • Connection information (endpoints, URLs)
  • Computed attributes that other modules might need
  • Clear descriptions for each output
versions.tf - Include:
  • Terraform version constraint (use ~> for minor version)
  • Provider version constraints
  • Required providers block
README.md - Include:
  • Module description and purpose
  • Usage example
  • Requirements section
  • Inputs table (can be auto-generated later)
  • Outputs table (can be auto-generated later)
main.tf - 包含:
  • 命名清晰的资源定义
  • 用于计算属性的本地值
  • 必要时添加数据源
variables.tf - 包含:
  • 先定义必填变量,再定义可选变量
  • 为每个变量添加清晰描述
  • 为合适的变量设置合理默认值
  • 类型约束(string、number、bool、list、map、object)
  • 为关键输入添加验证规则
outputs.tf - 包含:
  • 资源ID与ARN
  • 连接信息(端点、URL)
  • 其他模块可能需要的计算属性
  • 为每个输出添加清晰描述
versions.tf - 包含:
  • Terraform版本约束(使用~>指定小版本兼容)
  • 服务商版本约束
  • 必要的providers块
README.md - 包含:
  • 模块描述与用途
  • 使用示例
  • 需求说明部分
  • 输入变量表格(后续可自动生成)
  • 输出值表格(后续可自动生成)

3. Apply Best Practices

3. 遵循最佳实践

  • Use consistent naming:
    resource_type-purpose
    (e.g.,
    s3-logs
    ,
    vpc-main
    )
  • Add tags to all taggable resources with variables for custom tags
  • Use
    terraform fmt
    formatting
  • Include lifecycle blocks where appropriate
  • Add
    depends_on
    only when implicit dependencies don't work
  • Use
    count
    or
    for_each
    for conditional resources
  • 使用统一命名规则:
    资源类型-用途
    (例如:
    s3-logs
    vpc-main
  • 为所有可打标签的资源添加标签,并通过变量支持自定义标签
  • 使用
    terraform fmt
    进行代码格式化
  • 在合适的位置添加生命周期块
  • 仅当隐式依赖不生效时添加
    depends_on
  • 对条件化资源使用
    count
    for_each

4. Add Example Usage

4. 添加使用示例

Create
examples/basic/main.tf
showing minimal working example:
hcl
module "example" {
  source = "../.."
  
  # Required variables
  name = "example"
  
  # Optional variables with common values
  tags = {
    Environment = "dev"
    ManagedBy   = "terraform"
  }
}
创建
examples/basic/main.tf
文件,展示最简可用示例:
hcl
module "example" {
  source = "../.."
  
  # Required variables
  name = "example"
  
  # Optional variables with common values
  tags = {
    Environment = "dev"
    ManagedBy   = "terraform"
  }
}

Validation Checklist

验证检查清单

Before completing, verify:
  • All files use consistent formatting (
    terraform fmt
    )
  • Variables have descriptions and appropriate types
  • Outputs have descriptions
  • Version constraints are specified
  • README includes usage example
  • Module follows naming conventions
  • Tags are configurable via variables
完成前,请验证以下内容:
  • 所有文件使用统一格式(已通过
    terraform fmt
    格式化)
  • 变量均有描述与合适的类型约束
  • 输出值均有描述
  • 已指定版本约束
  • README包含使用示例
  • 模块遵循命名规范
  • 可通过变量配置标签