【问题标题】:Terraform: cannot provision aws ecs autoscaling clusterTerraform:无法配置 aws ecs 自动缩放集群
【发布时间】:2020-07-31 08:21:17
【问题描述】:

我们使用 Terraform 来预置 AWS Autoscaling Group 和 ECS 集群。我们决定根据https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cluster-auto-scaling.html 向 ECS 集群添加容量提供程序并允许它管理我们的扩展。

但是当我们添加容量提供程序资源并通过集群资源上的capacity_providers 属性将其连接到集群时,terraform plan 开始抱怨“Cylce”。我不确定如何配置它 - 似乎资源之间的每个“连接”都设置正确,但似乎 terraform 无法提供这个:

resource "aws_ecs_cluster" "cluster" {
  name               = "${var.environment}_cluster"
  capacity_providers = [aws_ecs_capacity_provider.capacity_provider.name]
}

resource "aws_ecs_capacity_provider" "capacity_provider" {
  name = "${var.environment}_capacity_provider"

  auto_scaling_group_provider {
    auto_scaling_group_arn = aws_autoscaling_group.autoscaling_group.arn
    managed_termination_protection = "ENABLED"

    managed_scaling {
      maximum_scaling_step_size = 1
      minimum_scaling_step_size = 1
      status                    = "ENABLED"
      target_capacity           = 100
    }
  }
}

resource "aws_launch_configuration" "launch_configuration" {
  name_prefix                 = "${var.environment}_launch_configuration-"
  image_id                    = data.aws_ami.ecs_os.id
  instance_type               = var.cluster_instance_type
  iam_instance_profile        = var.ecs_instance_profile
  security_groups             = var.security_groups["ecs_tasks"]
  associate_public_ip_address = true
  key_name                    = aws_key_pair.ssh_access.key_name
  user_data                   = data.template_file.launch_user_data.rendered

  lifecycle {
    create_before_destroy = true
  }
}

data "template_file" "launch_user_data" {
  template = "${file("${path.module}/taskdefs/user_data")}"

  vars = {
    ecs_cluster = aws_ecs_cluster.cluster.name
    efs_id      = var.efs_id
    aws_region  = data.aws_region.current.name
  }
}

resource "aws_autoscaling_group" "autoscaling_group" {
  name                  = "${var.environment}_asg"
  max_size              = 4
  min_size              = 1
  vpc_zone_identifier   = [var.public_subnet_id]
  launch_configuration  = aws_launch_configuration.launch_configuration.name
  health_check_type     = "EC2"
  protect_from_scale_in = true
}

terraform plan 输出:

Error: Cycle: module.compute.aws_ecs_cluster.cluster, module.compute.data.template_file.launch_user_data, module.compute.aws_launch_configuration.launch_configuration, module.compute.aws_autoscaling_group.autoscaling_group, module.compute.aws_ecs_capacity_provider.capacity_provider

编辑:明确一点:我知道实际上有一个循环。我的问题是如何在没有循环的情况下提供 terraform ?我看不出这怎么可能。

【问题讨论】:

    标签: terraform terraform-provider-aws


    【解决方案1】:

    首先,你可以这样定义集群的名称。

    locals {
      cluster_name = "${var.environment}_cluster"
    }              
    

    然后

    resource "aws_ecs_cluster" "cluster" {
      name               = local.cluster_name
      capacity_providers = [aws_ecs_capacity_provider.capacity_provider.name]
    }
    
    data "template_file" "launch_user_data" {
      template = "${file("${path.module}/taskdefs/user_data")}"
    
      vars = {
        ecs_cluster = local.cluster_name
        efs_id      = var.efs_id
        aws_region  = data.aws_region.current.name
      }
    }
    

    这样可以避免循环。

    我希望这会有所帮助。

    【讨论】:

    • 不知道为什么你投了反对票——你是绝对正确的。唯一的问题是名称中的依赖循环。通过使用本地人来定义循环中的一个资源的名称,它打破了循环并且它完美地工作。谢谢!
    【解决方案2】:

    我之前也注意到了这一点,但最终没有使用容量提供程序。

    如果您进入 AWS ECS Web 控制台,手动创建容量提供程序的唯一位置似乎是在现有集群内部。它似乎不是 AWS 内部的单独资源,而是集群的子资源。我认为这才是真正的先有鸡还是先有蛋的问题。

    我认为需要有一种方法让 terraform 创建集群(没有容量提供程序),然后创建容量提供程序,然后最后将其分配给集群。闻起来像错误报告。

    【讨论】:

    • 查看已接受的答案 - 循环的唯一问题是名称。一旦排序,循环就被打破了。希望它也能对你有所帮助!
    猜你喜欢
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 2020-03-29
    • 2019-06-22
    • 2017-01-30
    • 1970-01-01
    相关资源
    最近更新 更多