← 返回
效率工具
中文
Terraform
Avoid common Terraform mistakes — state corruption, count vs for_each, lifecycle traps, and dependency ordering.
避免 Terraform 常见错误:状态损坏、count 与 for_each 误用、生命周期陷阱及依赖顺序。
ivangdavila
效率工具
clawhub
v1.0.0 1 版本 99423.4 Key: 无需
#latest
概述
State Management
- Local state gets corrupted/lost — use remote backend (S3, GCS, Terraform Cloud)
- Multiple people running simultaneously — enable state locking with DynamoDB or equivalent
- Never edit state manually — use
terraform state mv, rm, import - State contains secrets in plain text — encrypt at rest, restrict access
Count vs for_each
count uses index — removing item 0 shifts all indices, forces recreationfor_each uses keys — stable, removing one doesn't affect others- Can't use both on same resource — choose one
for_each requires set or map — toset() to convert list
Lifecycle Rules
prevent_destroy = true — blocks accidental deletion, must be removed to destroycreate_before_destroy = true — new resource created before old destroyed, for zero downtimeignore_changes for external modifications — ignore_changes = [tags] ignores driftreplace_triggered_by to force recreation — when dependency changes
Dependencies
- Implicit via reference —
aws_instance.foo.id creates automatic dependency depends_on for hidden dependencies — when reference isn't in configdepends_on accepts list — depends_on = [aws_iam_role.x, aws_iam_policy.y]- Data sources run during plan — may fail if resource doesn't exist yet
Data Sources
- Data sources read existing resources — don't create
- Runs at plan time — dependency must exist before plan
- Use
depends_on if implicit dependency not clear — or plan fails - Consider using resource output instead — more explicit
Modules
- Pin module versions —
source = "org/name/aws?version=1.2.3" terraform init -upgrade to update — doesn't auto-update- Module outputs must be explicitly defined — can't access internal resources from outside
- Nested modules: output must bubble up — each layer needs to export
Variables
- No type = any — explicit
type = string, list(string), map(object({...})) sensitive = true hides from output — but still in state filevalidation block for constraints — custom error messagenullable = false to reject null — default is nullable
Common Mistakes
terraform destroy is permanent — no undo, use -target carefully- Plan succeeded ≠ apply succeeds — API errors, quotas, permissions discovered at apply
- Renaming resource = delete + create — use
moved block or terraform state mv - Workspaces not for environments — use separate state files/backends per env
- Provisioners are last resort — use cloud-init, user_data, or config management instead
Import
terraform import aws_instance.foo i-1234 — imports existing resource to state- Doesn't generate config — must write matching resource block manually
import block (TF 1.5+) — declarative import in config- Plan after import to verify — should show no changes if config matches
版本历史
共 1 个版本
-
v1.0.0
当前
2026-03-28 18:15 安全 安全
安全检测
腾讯云安全 (Sanbu)
安全,无风险
查看报告
🔗 相关推荐
productivity
ide-rea
使用百度AI搜索引擎(BDSE)进行网络搜索。适用于获取实时信息、文档资料或研究课题。
★ 236
📥 105,303
productivity
ivangdavila
创建、检查和编辑 Microsoft Word 文档及 DOCX 文件,支持样式、编号、修订记录、表格、分节符及兼容性检查等功能。
★ 437
📥 147,170
ai-intelligence
ivangdavila
自我反思+自我批评+自我学习+自组织记忆。智能体评估自身工作、发现错误并持续改进。
★ 1,349
📥 317,699