如何避免Terraform对AWS自动扩缩容策略重复执行原地更新?
问题
每次执行terraform plan时,adjustment_type字段都会被标记为需要更新,具体输出如下:
Terraform will perform the following actions: # aws_autoscaling_policy.this will be updated in-place ~ resource "aws_autoscaling_policy" "this" { + adjustment_type = "ChangeInCapacity"
对应的自动扩缩容策略资源定义:
resource "aws_autoscaling_policy" "this" { name = var.service_name # Same between `terraform` invocations. autoscaling_group_name = aws_autoscaling_group.this.name adjustment_type = "ChangeInCapacity" policy_type = "TargetTrackingScaling" # ASG merely serves as an EC2-ready-to-be-made-scalable, hence `false`. enabled = false target_tracking_configuration { predefined_metric_specification { predefined_metric_type = "ASGAverageCPUUtilization" } target_value = 99.0 } }
使用的版本信息:
- terraform 1.3.1
- terragrunt 0.40.2
原因与解决方案
原因:当
policy_type设为TargetTrackingScaling时,AWS的目标追踪型扩缩容策略并不需要adjustment_type参数,后端也不会存储该字段的值。Terraform同步远程资源状态时,发现本地定义了这个字段但远程不存在,就会判定需要将该字段添加到资源中,导致每次执行terraform plan都会出现更新提示。解决办法:直接从
aws_autoscaling_policy的资源定义中删除adjustment_type字段。目标追踪策略完全依赖target_tracking_configuration中的配置完成扩缩容逻辑,不需要adjustment_type,移除后Terraform就不会再检测到该字段的状态差异,即可消除不必要的更新提示。
内容的提问来源于stack exchange,提问作者VanillaDonuts
相关产品推荐
相关产品推荐

