terraform-dependency-analyzer
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTerraform Dependency Analyzer
Terraform依赖分析工具
This skill helps analyze and optimize resource dependencies in Terraform configurations.
此技能可帮助分析和优化Terraform配置中的资源依赖关系。
When to Use
使用场景
Use this skill when:
- Understanding resource relationships and dependencies
- Troubleshooting circular dependency errors
- Optimizing resource creation order
- Refactoring complex configurations
- Documenting infrastructure dependencies
以下场景可使用此技能:
- 理解资源关系与依赖
- 排查循环依赖错误
- 优化资源创建顺序
- 重构复杂配置
- 记录基础设施依赖
Dependency Types
依赖类型
Implicit Dependencies
隐式依赖
Terraform automatically detects dependencies through resource attribute references:
- Most common and preferred method
- Created when one resource references another's attributes
- Example:
subnet_id = aws_subnet.main.id
Terraform会通过资源属性引用自动检测依赖:
- 最常见且推荐的方式
- 当一个资源引用另一个资源的属性时自动创建
- 示例:
subnet_id = aws_subnet.main.id
Explicit Dependencies
显式依赖
Use only when implicit dependencies aren't sufficient:
depends_on- Cross-resource timing requirements
- Dependencies not expressed through attributes
- Ensuring proper creation/deletion order
- Should be used sparingly
仅当隐式依赖无法满足需求时,才使用:
depends_on- 跨资源的时序要求
- 无法通过属性表达的依赖
- 确保正确的创建/删除顺序
- 应谨慎使用
Analysis Workflow
分析流程
1. Generate Dependency Graph
1. 生成依赖图
bash
undefinedbash
undefinedCreate visual dependency graph
创建可视化依赖图
terraform graph | dot -Tpng > graph.png
terraform graph | dot -Tpng > graph.png
View as text
以文本形式查看
terraform graph
undefinedterraform graph
undefined2. Identify Resource Relationships
2. 识别资源关系
Parse configuration to map dependencies:
- Read through resource definitions
- Note attribute references between resources
- Identify explicit declarations
depends_on - Map out dependency chains
解析配置以映射依赖关系:
- 通读资源定义
- 记录资源间的属性引用
- 识别显式的声明
depends_on - 梳理依赖链
3. Check for Circular Dependencies
3. 检查循环依赖
Common causes:
- Security groups with mutual ingress rules
- Resources referencing each other's attributes
- Module outputs creating circular references
Solution approaches:
- Break cycles using separate rule resources
- Restructure resource relationships
- Use data sources to break circular references
常见原因:
- 包含相互入站规则的安全组
- 资源间互相引用属性
- 模块输出导致的循环引用
解决方法:
- 使用独立的规则资源打破循环
- 重构资源关系
- 使用数据源打破循环引用
Common Dependency Patterns
常见依赖模式
VPC Infrastructure
VPC基础设施
- VPC → Internet Gateway, Subnets
- Subnets → NAT Gateway, Route Tables
- Route Tables → Route Table Associations
- Security Groups → EC2/RDS instances
- VPC → 互联网网关、子网
- 子网 → NAT网关、路由表
- 路由表 → 路由表关联
- 安全组 → EC2/RDS实例
IAM Resources
IAM资源
- IAM Role → IAM Policy Attachments
- IAM Role → Resources using the role
- IAM角色 → IAM策略关联
- IAM角色 → 使用该角色的资源
Database Setup
数据库配置
- VPC, Subnets → DB Subnet Group
- Security Group → RDS Instance
- RDS Instance → Application resources
- VPC、子网 → 数据库子网组
- 安全组 → RDS实例
- RDS实例 → 应用资源
Troubleshooting
故障排查
Circular Dependency Errors
循环依赖错误
Process:
- Identify the resources in the cycle from error message
- Determine which reference creates the cycle
- Break the cycle by:
- Using separate rule resources (for security groups)
- Restructuring resource relationships
- Using data sources instead of direct references
排查流程:
- 从错误信息中识别循环中的资源
- 确定是哪个引用导致了循环
- 通过以下方式打破循环:
- 使用独立的规则资源(针对安全组)
- 重构资源关系
- 使用数据源替代直接引用
Slow Apply Times
执行速度缓慢
Analysis:
- Check for unnecessary statements forcing sequential creation
depends_on - Identify resources that could be created in parallel
- Look for bottleneck resources blocking multiple dependencies
Optimization:
- Remove explicit dependencies when implicit ones exist
- Group independent resources together
- Use modules to organize related resources
分析:
- 检查是否存在不必要的语句强制资源按顺序创建
depends_on - 识别可并行创建的资源
- 寻找阻塞多个依赖的瓶颈资源
优化方案:
- 当存在隐式依赖时,移除显式依赖
- 将独立资源分组
- 使用模块组织相关资源
Optimization Guidelines
优化指南
Minimize Explicit Dependencies
减少显式依赖
- Prefer implicit dependencies through attribute references
- Only use when absolutely necessary
depends_on - Remove redundant explicit dependencies
- 优先使用通过属性引用的隐式依赖
- 仅在绝对必要时使用
depends_on - 移除冗余的显式依赖
Maximize Parallelization
最大化并行化
- Ensure independent resources have no unnecessary dependencies
- Group related resources in modules
- Avoid creating artificial dependency chains
- 确保独立资源没有不必要的依赖
- 将相关资源分组到模块中
- 避免创建人为的依赖链
Use Modules Effectively
高效使用模块
- Organize resources by logical grouping
- Use module outputs to express dependencies
- Keep module dependencies clear and minimal
- 按逻辑分组组织资源
- 使用模块输出表达依赖
- 保持模块依赖清晰且精简
Analysis Checklist
分析检查清单
- Generated dependency graph
- Identified all resource relationships
- Checked for circular dependencies
- Verified implicit dependencies are sufficient
- Removed unnecessary statements
depends_on - Identified opportunities for parallelization
- Documented critical dependency chains
- 生成依赖图
- 识别所有资源关系
- 检查循环依赖
- 验证隐式依赖是否足够
- 移除不必要的语句
depends_on - 识别并行化的机会
- 记录关键依赖链