amazon-ec2 - 从 terraform 中的变量在 aws_autoscaling_policy 中设置 step_ adjustment

标签 amazon-ec2 terraform terraform-provider-aws

我正在设置一个模块来配置 terraform 中 ASG 的自动缩放。理想情况下,我想将 map 列表传递到我的模块并让它循环遍历它们,为列表中的每个 map 添加一个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_ adjustmentments 作为变量提供到我的模块中。

最佳答案

所以你可以这样做:

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

关于amazon-ec2 - 从 terraform 中的变量在 aws_autoscaling_policy 中设置 step_ adjustment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61272777/

相关文章:

java - Elasticsearch : "failed to get node info for {IP}" and "noNodeAvailableException" in service log

amazon-web-services - 在 Azure DevOps 上使用 Terraform 部署到 AWS 时的 AssumeRole

amazon-web-services - 以编程方式创建 AWS Athena View

kubernetes - 无法从堡垒访问通过 Terraform 进行的 AWS EKS 集群设置

aws-cloudformation - Terraform 插值将不需要的零添加到列表中

mysql - 如何使用带有 perl 的 ssh 隧道连接到 MySQL

amazon-web-services - 将 AWS Route 53 DNS 记录静态链接到 EC2 实例

list - Terraform,创建 map ,其中列表元素是值

node.js - 在 https 上从不同位置快速提供文件

amazon-web-services - 在 AWS-ECS-Task 完成其任务后,您如何停止它?