根据 terraform 中的变量在 aws_autoscaling_policy 中设置 step_adjustment

Set up step_adjustment in aws_autoscaling_policy from variable in terraform

我正在设置一个模块以在 terraform 中的 ASG 中配置自动缩放。理想情况下,我想将地图列表传递给我的模块并让它循环遍历它们,为列表中的每个地图添加一个 step_adjustment 到策略,但这似乎不起作用。

当前设置:

  name = "Example Auto-Scale Up Policy"
  policy_type = "StepScaling"
  autoscaling_group_name = "${aws_autoscaling_group.example_asg.name}"
  adjustment_type = "PercentChangeInCapacity"
  estimated_instance_warmup = 300
  step_adjustment {
    scaling_adjustment          = 20
    metric_interval_lower_bound = 0
    metric_interval_upper_bound = 5
  }
  step_adjustment {
    scaling_adjustment          = 25
    metric_interval_lower_bound = 5
    metric_interval_upper_bound = 15
  }
  step_adjustment {
    scaling_adjustment          = 50
    metric_interval_lower_bound = 15
  }
  min_adjustment_magnitude  = 4
}

我只想将三个 step_adjustments 作为变量提供到我的模块中。

所以你可以这样做的方法如下:

variable "step_adjustments" {
    type        = list(object({ metric_interval_lower_bound = string, metric_interval_upper_bound = string, scaling_adjustment = string }))
    default     = []
}

# inside your resource
resource "aws_appautoscaling_policy" "scale_up" {
  name = "Example Auto-Scale Up Policy"
  policy_type = "StepScaling"
  autoscaling_group_name = "${aws_autoscaling_group.example_asg.name}"
  adjustment_type = "PercentChangeInCapacity"
  estimated_instance_warmup = 300 
  dynamic "step_adjustment" {
    for_each = var.step_adjustments
    content {
      metric_interval_lower_bound = lookup(step_adjustment.value, "metric_interval_lower_bound")
      metric_interval_upper_bound = lookup(step_adjustment.value, "metric_interval_upper_bound")
      scaling_adjustment          = lookup(step_adjustment.value, "scaling_adjustment")
    }
  }
}

# example input into your module
step_adjustments = [
{
  scaling_adjustment          = 2
  metric_interval_lower_bound = 0
  metric_interval_upper_bound = 5
},
{
  scaling_adjustment          = 1
  metric_interval_lower_bound = 5
  metric_interval_upper_bound = "" # indicates infinity
}]