【问题标题】:Set up step_adjustment in aws_autoscaling_policy from variable in terraform从 terraform 中的变量在 aws_autoscaling_policy 中设置 step_adjustment
【发布时间】:2020-04-17 13:26:11
【问题描述】:

我正在设置一个模块来在 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 作为变量提供到我的模块中。

【问题讨论】:

    标签: amazon-ec2 terraform terraform-provider-aws


    【解决方案1】:

    所以你可以这样做:

    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
    }]
    

    【讨论】:

    • 这是一个很好的答案,简洁明了。非常感谢,效果很好!
    猜你喜欢
    • 2021-08-08
    • 1970-01-01
    • 2022-01-11
    • 2022-10-26
    • 2022-06-22
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 2022-01-27
    相关资源
    最近更新 更多