Terraform 基础设施即代码使用指南

# MyTech IaC 完整使用指南

## 目录

- [概述](#概述)
- [项目架构](#项目架构)
- [环境要求](#环境要求)
- [IaC 项目总览](#iac-项目总览)
- [通用配置说明](#通用配置说明)
- [使用指南](#使用指南)
- [最佳实践](#最佳实践)
- [常见问题](#常见问题)

## 概述

MyTech 的基础设施即代码(IaC)体系采用 Terragrunt + Terraform 架构,通过模块化和层级化配置实现多环境、多区域、多服务的统一管理。

### 核心技术栈

- **Terraform**: v1.8.2 - 基础设施即代码工具
- **Terragrunt**: v0.72.4 - Terraform 包装器,提供 DRY 配置
- **Terramate**: v0.13.0 - 堆栈管理和编排工具
- **AWS Provider**: >= 6.0.0 - AWS 云服务提供商

### 体系特点

- ✅ **统一架构**: 所有项目采用相同的目录结构和配置模式
- ✅ **模块复用**: 通过 devops-terraform-modules 实现模块共享
- ✅ **多层级配置**: Organization → Department → Environment → Region → Service
- ✅ **环境隔离**: 完全独立的环境配置和状态管理
- ✅ **标签统一**: 自动化的资源标签管理
- ✅ **远程状态**: S3 Backend 集中管理状态文件

## 项目架构

### 整体架构图

```
MyTech IaC 体系
│
├── devops-terraform-modules/          # Terraform 模块仓库
│   ├── modules/aws/
│   │   ├── ec2/                      # EC2 实例模块
│   │   ├── eks/                      # EKS 集群模块
│   │   ├── iam/                      # IAM 权限模块
│   │   ├── route53/                  # DNS 管理模块
│   │   └── secrets-manager/          # 密钥管理模块
│   └── docs/                         # 文档目录
│
├── devops-iac-secrets/               # 密钥管理项目
├── devops-iac-dns/                   # DNS 管理项目
├── devops-iac-ec2/                   # EC2 实例管理项目
├── devops-iac-eks/                   # EKS 集群管理项目
└── devops-iac-iam/                   # IAM 权限管理项目
```

### 标准目录结构

所有 IaC 项目遵循统一的目录结构:

```
devops-iac-{service}/
├── .tool-versions                    # 工具版本定义
├── README.md
└── live/                             # 实际环境配置
    ├── _common.hcl                   # 通用配置
    ├── root.hcl                      # 根配置
    └── {org}/                        # 组织层级
        ├── org.hcl
        └── {dept}/                   # 部门层级
            ├── dept.hcl
            └── {env}/                # 环境层级
                ├── env.hcl
                └── {cloud}/          # 云服务商层级
                    └── {region}/     # 区域层级
                        ├── region.hcl
                        └── {service}/    # 服务层级
                            ├── terragrunt.hcl
                            └── stack.tm.hcl
```

## 环境要求

### 必需工具

```bash
# 安装 asdf 版本管理工具
brew install asdf

# 添加插件
asdf plugin add terraform
asdf plugin add terragrunt
asdf plugin add terramate

# 在项目目录下安装指定版本
cd devops-iac-{service}
asdf install
```

### AWS 凭证配置

```bash
# 方法 1: 使用 AWS CLI
aws configure

# 方法 2: 使用环境变量
export AWS_ACCESS_KEY_ID="your-access-key"
export AWS_SECRET_ACCESS_KEY="your-secret-key"
export AWS_DEFAULT_REGION="ap-northeast-1"
```

## IaC 项目总览

### 项目对比表

| 项目 | 管理资源 | 主要模块 | 典型部门 | 更新频率 |
|------|---------|---------|---------|---------|
| devops-iac-secrets | AWS Secrets Manager | secrets-manager | 所有部门 | 中等 |
| devops-iac-dns | Route53 DNS | route53/private-zone | infra | 高 |
| devops-iac-ec2 | EC2 实例 | ec2 | infra, prime, security | 中等 |
| devops-iac-eks | EKS 集群 | eks | infra, prime | 低 |
| devops-iac-iam | IAM 权限 | iam | security, infra | 低 |

### 1. devops-iac-secrets - 密钥管理

**用途**: 管理 AWS Secrets Manager 中的密钥和敏感信息

**主要功能**:
- 创建和管理密钥
- 配置密钥恢复窗口
- 生成只读访问策略
- 密钥轮换管理

**配置示例**:
```hcl
inputs = {
  secrets = {
    "prod/am/mysql/connection" = {
      description             = "MySQL connection credentials"
      recovery_window_in_days = 7
      secret_string = jsonencode({
        host     = "mysql.example.com"
        username = "admin"
        password = "secure-password"
      })
      create_read_only_policy = true
      tags = merge(local.default_tags, {
        service = "mysql"
      })
    }
  }
}
```

### 2. devops-iac-dns - DNS 管理

**用途**: 管理 AWS Route53 私有托管区域和 DNS 记录

**主要功能**:
- 创建私有托管区域
- 管理 DNS 记录(A, CNAME, TXT 等)
- VPC 关联管理
- 跨区域 DNS 配置

**配置示例**:
```hcl
inputs = {
  zone_name          = "prod.internal.mytech.com"
  primary_vpc_id     = "vpc-xxxxx"
  primary_vpc_region = "ap-northeast-1"
  
  dns_records = {
    "api.prod.internal.mytech.com" = {
      type    = "CNAME"
      ttl     = 300
      records = ["internal-lb.elb.amazonaws.com"]
    }
  }
}
```

### 3. devops-iac-ec2 - EC2 实例管理

**用途**: 管理 EC2 实例及其相关资源

**主要功能**:
- EC2 实例创建和配置
- EBS 卷管理
- 弹性 IP 分配
- Jumpserver 集成

**配置示例**:
```hcl
inputs = {
  instance_list = {
    "aws-jp-prod-app-server-01" = {
      ami_id          = "ami-xxxxx"
      instance_type   = "m6a.xlarge"
      subnet_id       = "subnet-xxxxx"
      security_groups = ["sg-xxxxx"]
      create_eip      = true
      
      root_block_device = {
        volume_size = 100
        volume_type = "gp3"
      }
      
      tags = merge(local.default_tags, {
        team = "backend"
      })
    }
  }
}
```

### 4. devops-iac-eks - EKS 集群管理

**用途**: 管理 AWS EKS Kubernetes 集群

**主要功能**:
- EKS 集群创建
- 节点组管理
- IRSA 配置
- 集群插件管理

### 5. devops-iac-iam - IAM 权限管理

**用途**: 管理 AWS IAM 角色、策略和权限

**主要功能**:
- IAM 角色创建
- 策略管理
- 服务账号配置
- 跨账号访问

## 通用配置说明

### 配置层级

所有 IaC 项目共享相同的配置层级:

| 文件 | 层级 | 示例值 | 说明 |
|------|------|--------|------|
| `root.hcl` | Root | - | Backend & Provider 配置 |
| `_common.hcl` | Common | - | 通用配置和标签 |
| `org.hcl` | Organization | `mytech`, `user` | 组织标识 |
| `dept.hcl` | Department | `am`, `infra`, `security` | 部门/团队 |
| `env.hcl` | Environment | `prod`, `qa`, `uat`, `dev` | 环境 |
| `region.hcl` | Region | `ap-northeast-1` | AWS 区域 |
| `terragrunt.hcl` | Service | - | 服务配置 |

### Root 配置示例

```hcl
# Backend 配置 - S3 远程状态存储
generate "backend" {
  path      = "backend.tf"
  if_exists = "overwrite_terragrunt"
  contents  = <<EOF
terraform {
  backend "s3" {
    bucket  = "mytech-iac-terraform-state"
    key     = "live/${path_relative_to_include()}/terraform.tfstate"
    region  = "ap-northeast-1"
    encrypt = true
  }
}
EOF
}
```

### 标签策略

所有资源自动包含以下标签:

```hcl
{
  "iac/managed_by" = "terraform"
  org              = "mytech"
  dept             = "am"
  env              = "prod"
}
```

## 使用指南

### 基本操作

```bash
# 1. 进入服务目录
cd live/mytech/am/prod/aws/ap-northeast-1/{service}

# 2. 初始化
terragrunt init

# 3. 查看计划
terragrunt plan

# 4. 应用变更
terragrunt apply

# 5. 查看状态
terragrunt state list

# 6. 销毁资源
terragrunt destroy
```

### 批量操作

```bash
# 在部门级别批量操作
cd live/mytech/am

# 批量初始化
terragrunt run-all init

# 批量查看计划
terragrunt run-all plan

# 批量应用
terragrunt run-all apply --terragrunt-parallelism 5
```

### 添加新环境

```bash
# 1. 创建目录结构
mkdir -p live/mytech/{dept}/{new-env}/aws/ap-northeast-1/{service}

# 2. 创建环境配置
cat > live/mytech/{dept}/{new-env}/env.hcl << 'EOF'
locals {
  env = "new-env"
}
EOF

# 3. 创建区域配置
cat > live/mytech/{dept}/{new-env}/aws/ap-northeast-1/region.hcl << 'EOF'
locals {
  aws_region = "ap-northeast-1"
}
EOF

# 4. 复制并修改服务配置
cp -r live/mytech/{dept}/prod/aws/ap-northeast-1/{service}/* \
      live/mytech/{dept}/{new-env}/aws/ap-northeast-1/{service}/

# 5. 初始化并应用
cd live/mytech/{dept}/{new-env}/aws/ap-northeast-1/{service}
terragrunt init
terragrunt plan
terragrunt apply
```

### 跨项目操作示例

#### 部署新应用环境

```bash
# 1. 创建 IAM 角色
cd devops-iac-iam/live/mytech/app/prod/aws/ap-northeast-1/iam
terragrunt apply

# 2. 创建 EC2 实例
cd devops-iac-ec2/live/mytech/app/prod/aws/ap-northeast-1/ec2
terragrunt apply

# 3. 配置 DNS 记录
cd devops-iac-dns/live/mytech/infra/prod/aws/ap-northeast-1/prod.internal.mytech.com
terragrunt apply

# 4. 存储密钥
cd devops-iac-secrets/live/mytech/app/prod/aws/ap-northeast-1/secrets-manager
terragrunt apply
```

## 最佳实践

### 1. 版本控制

✅ **推荐做法**:
- 所有配置文件纳入 Git 版本控制
- 使用有意义的提交信息
- 通过 Pull Request 进行代码审查
- 使用 Git tag 标记重要版本

❌ **禁止做法**:
- 提交敏感信息(密码、密钥)
- 直接推送到主分支
- 跳过代码审查

### 2. 模块版本管理

```hcl
# ✅ 推荐:使用特定版本
terraform {
  source = "git::https://github.com/MyTech/devops-iac-terraform-modules.git//modules/aws/ec2?ref=v1.2.0"
}

# ⚠️ 谨慎:使用 main 分支(仅开发环境)
terraform {
  source = "git::https://github.com/MyTech/devops-iac-terraform-modules.git//modules/aws/ec2?ref=main"
}
```

### 3. 命名规范

#### 资源命名
```
格式: {cloud}-{region-code}-{env}-{dept}-{service}-{resource-type}

示例:
- aws-jp-prod-infra-jumpserver
- aws-jp-prod-am-mysql-cluster
```

#### 密钥命名
```
格式: {env}/{dept}/{service}/{resource}/{name}

示例:
- prod/am/mysql/connection/prod_mytech_am_ro
- qa/security/api/keys/sec_api_key
```

#### DNS 记录命名
```
格式: {service}.{env}.internal.{domain}

示例:
- api.prod.internal.mytech.com
- mysql.qa.internal.mytech.com
```

### 4. 安全实践

```hcl
# ✅ 使用 Secrets Manager
secrets = {
  "prod/app/db-password" = {
    secret_string = jsonencode({
      password = "secure-password"
    })
    recovery_window_in_days = 7
  }
}

# ❌ 不要硬编码密钥
password = "my-password-123"  # 禁止!
```

### 5. 变更管理流程

1. **开发阶段**: 在 dev 环境测试
2. **测试阶段**: 在 qa/uat 环境验证
3. **生产部署**: 
   - 创建变更请求
   - 在维护窗口执行
   - 准备回滚计划
   - 监控部署结果

## 常见问题

### Q1: 如何处理状态锁定?

```bash
# 查看锁定信息并解锁
terragrunt force-unlock <lock-id>
```

### Q2: 如何回滚变更?

```bash
# 方法 1: 使用 Git 回滚
git revert <commit-hash>
terragrunt apply

# 方法 2: 手动恢复配置
# 编辑配置文件恢复到之前的状态
terragrunt apply
```

### Q3: 如何更新模块版本?

```bash
# 1. 更新 terragrunt.hcl 中的 ref 参数
# 2. 重新初始化
terragrunt init -upgrade

# 3. 查看变更
terragrunt plan

# 4. 应用更新
terragrunt apply
```

### Q4: 如何调试配置?

```bash
# 启用调试日志
export TG_LOG=debug
terragrunt plan

# 验证配置
terragrunt validate-inputs
```

### Q5: 如何处理依赖关系?

```hcl
# 在 terragrunt.hcl 中定义依赖
dependency "vpc" {
  config_path = "../vpc"
}

inputs = {
  vpc_id = dependency.vpc.outputs.vpc_id
}
```

### Q6: 如何管理多区域部署?

```bash
# 每个区域独立管理
cd live/mytech/am/prod/aws/ap-northeast-1/service
terragrunt apply

cd live/mytech/am/prod/aws/ap-southeast-1/service
terragrunt apply
```

### Q7: 如何优化性能?

```bash
# 使用并行执行
terragrunt run-all apply --terragrunt-parallelism 10

# 缓存模块
export TERRAGRUNT_DOWNLOAD=.terragrunt-cache
```

### Q8: 如何处理敏感输出?

```hcl
# 在模块中标记敏感输出
output "secret_value" {
  value     = aws_secretsmanager_secret_version.this.secret_string
  sensitive = true
}

# 查看敏感输出
terragrunt output -json | jq -r '.secret_value.value'
```

## 相关资源

### 官方文档

- [Terraform 文档](https://www.terraform.io/docs)
- [Terragrunt 文档](https://terragrunt.gruntwork.io/docs/)
- [Terramate 文档](https://terramate.io/docs)
- [AWS Provider 文档](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)

### 内部资源

- [devops-terraform-modules](https://github.com/MyTech/devops-iac-terraform-modules) - Terraform 模块仓库
- [devops-iac-secrets](https://github.com/MyTech/devops-iac-secrets) - 密钥管理项目
- [devops-iac-dns](https://github.com/MyTech/devops-iac-dns) - DNS 管理项目
- [devops-iac-ec2](https://github.com/MyTech/devops-iac-ec2) - EC2 管理项目
- [devops-iac-eks](https://github.com/MyTech/devops-iac-eks) - EKS 管理项目
- [devops-iac-iam](https://github.com/MyTech/devops-iac-iam) - IAM 管理项目

## 支持与反馈

如有问题或建议,请:

1. 查看本文档的常见问题部分
2. 搜索已有的 GitHub Issues
3. 创建新的 Issue 并提供详细信息
4. 联系 DevOps 团队

---

**文档版本**: 1.0.0  
**最后更新**: 2025-11-18  
**维护团队**: DevOps Team

  

 

# IaC 快速参考手册

## 常用命令速查

### Terragrunt 基础命令

| 命令 | 说明 | 示例 |
|------|------|------|
| `terragrunt init` | 初始化工作目录 | `terragrunt init` |
| `terragrunt plan` | 查看执行计划 | `terragrunt plan` |
| `terragrunt apply` | 应用变更 | `terragrunt apply` |
| `terragrunt destroy` | 销毁资源 | `terragrunt destroy` |
| `terragrunt state list` | 列出状态中的资源 | `terragrunt state list` |
| `terragrunt output` | 查看输出值 | `terragrunt output` |
| `terragrunt validate` | 验证配置 | `terragrunt validate` |

### Terragrunt 批量命令

| 命令 | 说明 |
|------|------|
| `terragrunt run-all init` | 批量初始化 |
| `terragrunt run-all plan` | 批量查看计划 |
| `terragrunt run-all apply` | 批量应用变更 |
| `terragrunt run-all destroy` | 批量销毁资源 |
| `terragrunt run-all apply --terragrunt-parallelism 5` | 并行执行(5个并发) |

### Terramate 命令

| 命令 | 说明 |
|------|------|
| `terramate list` | 列出所有堆栈 |
| `terramate list --changed` | 列出变更的堆栈 |
| `terramate run terraform init` | 在所有堆栈中运行命令 |
| `terramate run --changed terraform apply` | 在变更的堆栈中运行 |

## 项目快速索引

### 项目位置

```bash
/Users/bingbingsun/Projects/devops/
├── devops-terraform-modules/    # 模块仓库
├── devops-iac-secrets/          # 密钥管理
├── devops-iac-dns/              # DNS 管理
├── devops-iac-ec2/              # EC2 管理
├── devops-iac-eks/              # EKS 管理
└── devops-iac-iam/              # IAM 管理
```

### 项目功能对照

| 需求 | 使用项目 | 目录示例 |
|------|---------|---------|
| 添加密钥 | devops-iac-secrets | `live/mytech/am/prod/aws/ap-northeast-1/secrets-manager/` |
| 添加 DNS 记录 | devops-iac-dns | `live/mytech/infra/prod/aws/ap-northeast-1/prod.internal.mytech.com/` |
| 创建 EC2 实例 | devops-iac-ec2 | `live/mytech/infra/prod/aws/ap-northeast-1/ec2/` |
| 管理 EKS 集群 | devops-iac-eks | `live/mytech/infra/prod/aws/ap-northeast-1/eks/` |
| 配置 IAM 权限 | devops-iac-iam | `live/mytech/security/prod/aws/ap-northeast-1/iam/` |

## 目录结构速查

```
live/{org}/{dept}/{env}/{cloud}/{region}/{service}/
     └─┬─┘ └─┬──┘ └─┬─┘ └─┬──┘ └──┬───┘ └───┬───┘
       │     │      │      │       │         │
       │     │      │      │       │         └─ 服务 (secrets-manager, ec2, dns)
       │     │      │      │       └─────────── 区域 (ap-northeast-1)
       │     │      │      └─────────────────── 云服务商 (aws)
       │     │      └────────────────────────── 环境 (prod, qa, uat, dev)
       │     └───────────────────────────────── 部门 (am, infra, security, prime)
       └─────────────────────────────────────── 组织 (mytech, user)
```

## 配置文件速查

| 文件 | 位置 | 作用 |
|------|------|------|
| `root.hcl` | `live/` | Backend & Provider 配置 |
| `_common.hcl` | `live/` | 通用配置 & 标签 |
| `org.hcl` | `live/{org}/` | 组织配置 |
| `dept.hcl` | `live/{org}/{dept}/` | 部门配置 |
| `env.hcl` | `live/{org}/{dept}/{env}/` | 环境配置 |
| `region.hcl` | `live/{org}/{dept}/{env}/{cloud}/{region}/` | 区域配置 |
| `terragrunt.hcl` | `live/{org}/{dept}/{env}/{cloud}/{region}/{service}/` | 服务配置 |
| `stack.tm.hcl` | `live/{org}/{dept}/{env}/{cloud}/{region}/{service}/` | Stack 元数据 |

## 常见操作速查

### 1. 添加新密钥

```bash
# 1. 进入目录
cd devops-iac-secrets/live/mytech/am/prod/aws/ap-northeast-1/secrets-manager

# 2. 编辑 terragrunt.hcl,添加密钥配置
# 3. 应用变更
terragrunt plan
terragrunt apply
```

### 2. 添加 DNS 记录

```bash
# 1. 进入目录
cd devops-iac-dns/live/mytech/infra/prod/aws/ap-northeast-1/prod.internal.mytech.com

# 2. 编辑 terragrunt.hcl,在 dns_records 中添加记录
# 3. 应用变更
terragrunt plan
terragrunt apply
```

### 3. 创建 EC2 实例

```bash
# 1. 进入目录
cd devops-iac-ec2/live/mytech/infra/prod/aws/ap-northeast-1/ec2

# 2. 编辑 terragrunt.hcl,在 instance_list 中添加实例配置
# 3. 应用变更
terragrunt plan
terragrunt apply
```

### 4. 查看资源状态

```bash
# 列出所有资源
terragrunt state list

# 查看特定资源详情
terragrunt state show <resource-address>

# 查看输出
terragrunt output
```

### 5. 更新模块版本

```bash
# 1. 修改 terragrunt.hcl 中的 ref 参数
# 2. 重新初始化
terragrunt init -upgrade

# 3. 查看变更
terragrunt plan

# 4. 应用更新
terragrunt apply
```

## 环境对照表

| 环境代码 | 全称 | 用途 |
|---------|------|------|
| prod | Production | 生产环境 |
| qa | Quality Assurance | QA 测试环境 |
| uat | User Acceptance Testing | UAT 测试环境 |
| sit | System Integration Testing | 集成测试环境 |
| dev | Development | 开发环境 |
| fat | Feature Acceptance Testing | 功能测试环境 |
| mirror | Mirror | 镜像环境 |

## 区域代码对照

| 区域代码 | AWS 区域 | 位置 |
|---------|---------|------|
| ap-northeast-1 | Asia Pacific (Tokyo) | 东京 |
| ap-southeast-1 | Asia Pacific (Singapore) | 新加坡 |
| ap-east-1 | Asia Pacific (Hong Kong) | 香港 |
| us-east-1 | US East (N. Virginia) | 美国东部 |

## 部门代码对照

| 部门代码 | 全称 | 说明 |
|---------|------|------|
| am | Asset Management | 资产管理 |
| infra | Infrastructure | 基础设施 |
| security | Security | 安全团队 |
| prime | Prime | Prime 业务 |
| defi | DeFi | DeFi 业务 |
| datacenter | Data Center | 数据中心 |
| qc | Quality Control | 质量控制 |

## 故障排查速查

### 问题 1: 状态锁定

```bash
# 查看锁定信息
terragrunt force-unlock <lock-id>
```

### 问题 2: 模块下载失败

```bash
# 清理缓存
rm -rf .terragrunt-cache
terragrunt init
```

### 问题 3: 权限错误

```bash
# 检查 AWS 凭证
aws sts get-caller-identity

# 重新配置
aws configure
```

### 问题 4: 配置验证失败

```bash
# 启用调试日志
export TG_LOG=debug
terragrunt plan
```

## 命名规范速查

### 资源命名

```
格式: {cloud}-{region-code}-{env}-{dept}-{service}-{resource-type}
示例: aws-jp-prod-infra-jumpserver
```

### 密钥命名

```
格式: {env}/{dept}/{service}/{resource}/{name}
示例: prod/am/mysql/connection/prod_mytech_am_ro
```

### DNS 命名

```
格式: {service}.{env}.internal.{domain}
示例: api.prod.internal.mytech.com
```

## 标签规范

### 必需标签(自动添加)

```hcl
{
  "iac/managed_by" = "terraform"
  org              = "mytech"
  dept             = "am"
  env              = "prod"
}
```

### 推荐标签

```hcl
{
  service     = "api-gateway"
  team        = "backend"
  cost_center = "engineering"
  owner       = "john@example.com"
}
```

## 安全检查清单

- [ ] 不要提交敏感信息到 Git
- [ ] 使用 Secrets Manager 存储密钥
- [ ] 启用状态文件加密
- [ ] 使用最小权限原则
- [ ] 定期轮换密钥
- [ ] 在生产环境操作前先在测试环境验证
- [ ] 准备回滚计划
- [ ] 监控变更结果

## 相关文档

- [完整使用指南](./IaC-Complete-Guide.md)
- [Terraform 官方文档](https://www.terraform.io/docs)
- [Terragrunt 官方文档](https://terragrunt.gruntwork.io/docs/)

---

**最后更新**: 2025-11-18



## 🚀 快速开始

### 使用模块

在 Terragrunt 配置中引用模块:

```hcl
terraform {
source = "git::https://github.com/mytech/devops-iac-terraform-modules.git//modules/aws/ec2?ref=v1.0.0"
}
```

### 版本管理

推荐使用特定版本或 tag:

```hcl
# 使用 tag 版本(推荐)
source = "...?ref=v1.0.0"

# 使用分支(开发环境)
source = "...?ref=main"

# 使用 commit hash(精确控制)
source = "...?ref=abc123"
```

## 📁 项目结构

```
devops-terraform-modules/
├── modules/ # Terraform 模块
│ └── aws/ # AWS 相关模块
│ ├── ec2/ # EC2 实例模块
│ ├── eks/ # EKS 集群模块
│ ├── iam/ # IAM 权限模块
│ ├── route53/ # DNS 管理模块
│ └── secrets-manager/ # 密钥管理模块
├── examples/ # 使用示例
└── docs/ # 文档目录
├── IaC-Complete-Guide.md # 完整使用指南
└── IaC-Quick-Reference.md # 快速参考手册
```

## 🔗 相关项目

### IaC 项目

| 项目 | 说明 | 仓库 |
|------|------|------|
| **devops-iac-secrets** | AWS Secrets Manager 密钥管理 | [GitHub](https://github.com/mytech/devops-iac-secrets) |
| **devops-iac-dns** | Route53 DNS 管理 | [GitHub](https://github.com/mytech/devops-iac-dns) |
| **devops-iac-ec2** | EC2 实例管理 | [GitHub](https://github.com/mytech/devops-iac-ec2) |
| **devops-iac-eks** | EKS 集群管理 | [GitHub](https://github.com/mytech/devops-iac-eks) |
| **devops-iac-iam** | IAM 权限管理 | [GitHub](https://github.com/mytech/devops-iac-iam) |

## 🛠️ 技术栈

- **Terraform**: >= 1.8.0
- **AWS Provider**: >= 6.0.0
- **Terragrunt**: >= 0.72.0

## 📖 模块使用示例

### EC2 模块

```hcl
terraform {
source = "git::https://github.com/mytech/devops-iac-terraform-modules.git//modules/aws/ec2?ref=main"
}

inputs = {
instance_list = {
"my-server" = {
ami_id = "ami-xxxxx"
instance_type = "t3.medium"
subnet_id = "subnet-xxxxx"
security_groups = ["sg-xxxxx"]
 
tags = {
Name = "my-server"
Env = "prod"
}
}
}
}
```

### Secrets Manager 模块

```hcl
terraform {
source = "git::https://github.com/mytech/devops-iac-terraform-modules.git//modules/aws/secrets-manager?ref=main"
}

inputs = {
secrets = {
"prod/app/db-password" = {
description = "Database password"
recovery_window_in_days = 7
secret_string = jsonencode({
username = "admin"
password = "secure-password"
})
create_read_only_policy = true
}
}
}
```

### Route53 模块

```hcl
terraform {
source = "git::https://github.com/mytech/devops-iac-terraform-modules.git//modules/aws/route53/private-zone?ref=main"
}

inputs = {
zone_name = "internal.example.com"
primary_vpc_id = "vpc-xxxxx"
primary_vpc_region = "ap-northeast-1"
 
dns_records = {
"api.internal.example.com" = {
type = "CNAME"
ttl = 300
records = ["lb.amazonaws.com"]
}
}
}
```

## 🤝 贡献指南

### 添加新模块

1. 在 `modules/aws/` 下创建模块目录
2. 编写模块代码(main.tf, variables.tf, outputs.tf)
3. 添加 README.md 说明
4. 在 `examples/` 中添加使用示例
5. 提交 Pull Request

### 更新现有模块

1. 创建功能分支
2. 修改模块代码
3. 更新版本号和 CHANGELOG
4. 测试模块功能
5. 提交 Pull Request

### 版本发布

```bash
# 1. 更新版本号
git tag -a v1.0.0 -m "Release version 1.0.0"

# 2. 推送 tag
git push origin v1.0.0

# 3. 创建 GitHub Release
```

## 📝 最佳实践

### 模块设计

- ✅ 保持模块简单和专注
- ✅ 提供清晰的输入和输出
- ✅ 使用有意义的变量名
- ✅ 添加详细的文档和示例
- ✅ 支持标签和命名规范

### 版本管理

- ✅ 使用语义化版本(Semantic Versioning)
- ✅ 维护 CHANGELOG
- ✅ 向后兼容性
- ✅ 在 README 中说明破坏性变更

### 测试

- ✅ 在开发环境测试新功能
- ✅ 验证向后兼容性
- ✅ 测试边界情况
- ✅ 文档化测试步骤

## 📞 支持

如有问题,请:
- 查看 [完整使用指南](./docs/IaC-Complete-Guide.md)
- 查看 [快速参考手册](./docs/IaC-Quick-Reference.md)
- 搜索或创建 GitHub Issue
- 联系 DevOps 团队

## 📄 许可证

Copyright © 2025 mytech

---

**维护团队**: DevOps Team
**最后更新**: 2025-11-18






posted @ 2026-10-14 10:25  JameMei  阅读(7)  评论(0)    收藏  举报