解决 Terraform 报错 Provider produced inconsistent plan
分析 Terraform 在 apply 阶段报 Provider produced inconsistent final plan 的 Provider 内部状态漂移根因与解决步骤。
问题现象
在执行 terraform apply 过程中抛出异常中断:
Error: Provider produced inconsistent final plan
When expanding the plan for aws_security_group_rule.my_rule to include new values
learned so far during apply, provider "registry.terraform.io/hashicorp/aws"
produced an invalid new value for .description: was cty.StringVal("old"), but
now cty.StringVal("new").
This is a bug in the provider, which should be reported in the provider's own issue tracker.
触发根因
- 云服务商 Provider 插件在
Plan阶段预测的属性值与执行真实 API 调用后返回的属性值不一致。 - 某些资源(如 Security Group Ingress / EKS Addons)存在双向声明冲突(例如既在父资源中定义了内嵌规则,又定义了独立的单独规则资源)。
解决步骤与操作命令
步骤一:升级 Provider 插件至最新修复版本
很多 inconsistent plan 属于已知 Provider bug,升级版本即可解决:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.30" # 升级至最新稳定小版本
}
}
}
运行升级命令:
terraform init -upgrade
步骤二:避免内嵌属性与独立资源混合使用
例如对于安全组,不要同时使用 aws_security_group.ingress 和 aws_security_group_rule:
仅保留独立资源声明方式。
步骤三:利用 target 逐个应用拆解依赖死锁
terraform apply -target=aws_security_group.my_sg
terraform apply
验证确认
重新执行:
terraform apply -auto-approve
变更顺利应用,不再抛出 inconsistent final plan 致命错误。


