简体   繁体   English

无法使用 terraform 将容量提供程序添加到 AWS ECS 集群

[英]Unable to add capacity provider to AWS ECS cluster with terraform

I'm trying to add a capacity provider to a ECS cluster using terraform so that it can autoscale.我正在尝试使用 terraform 将容量提供程序添加到 ECS 集群,以便它可以自动缩放。 The autoscaling group needs to know the cluster to create instances in the cluster, but the cluster also need to know the autoscaling group through its capacity provider.自动伸缩组需要知道集群才能在集群中创建实例,但集群还需要通过其容量提供程序知道自动伸缩组。 How can I resolve this circular dependency using terraform and a capacity provider?如何使用 terraform 和容量提供程序解决这种循环依赖?

Here is my infrastructure code for the cluster creation这是我用于创建集群的基础架构代码

# The ECS cluster
resource "aws_ecs_cluster" "my_cluster" {
  name = "my-cluster"
  capacity_providers = [aws_ecs_capacity_provider.my_cp.name]
}

# The capacity provider
resource "aws_ecs_capacity_provider" "my_cp" {
  name = "my-cp"

  auto_scaling_group_provider {
    auto_scaling_group_arn         = aws_autoscaling_group.my_asg.arn
    managed_termination_protection = "DISABLED"

    managed_scaling {
      maximum_scaling_step_size = 1000
      minimum_scaling_step_size = 1
      status                    = "ENABLED"
      target_capacity           = 10
    }
  }
}

Here is the infrastructure code for the autoscaling group and its dependencies这是自动缩放组及其依赖项的基础结构代码

# The image for the cluster instances 
data "aws_ssm_parameter" "instance_image" {
  name = "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}

# The launch config of the instances
resource "aws_launch_configuration" "my_launch_config" {
  name          = "my-launch-config"
  image_id      = data.aws_ssm_parameter.instance_image.value
  instance_type = "t3.small"
  iam_instance_profile = my_iam_profile
  security_groups = my_security_groups
  associate_public_ip_address = false
  key_name = "my-keypair"

}

# The placement group of the autosclaing group
resource "aws_placement_group" "my_pg" {
  name     = "my-pg"
  strategy = "spread"
}

# The autoscaling gorup
resource "aws_autoscaling_group" "my_asg" {
  name                      = "my-asg"
  max_size                  = 2
  min_size                  = 1
  desired_capacity          = 1

  health_check_type         = "EC2"
  health_check_grace_period = 300

  force_delete              = true
  placement_group           = aws_placement_group.my_pg.id
  launch_configuration      = aws_launch_configuration.my_launch_config.id
  vpc_zone_identifier       = my_subnets_ids


  tag {
    key                 = "Name"
    value               = "myInstance"
    propagate_at_launch = true
  }
}

When applying this terraform, I do get a capacity provider on my cluster but the instances are in the cluster default instead of my-cluster .应用此 terraform 时,我的集群上确实有一个容量提供程序,但实例位于集群default而不是my-cluster Some will say I just have to add有人会说我只需要添加

  user_data = <<EOF
    #!/bin/bash
    echo ECS_CLUSTER=${aws_ecs_cluster.my_cluster.name} >> /etc/ecs/ecs.config
  EOF

to the launch config, but I cannot reference the cluster in the launch config, because the cluster depends ont the capacity provider which depends on the autoscaling group which depends on the launch config.到启动配置,但我无法在启动配置中引用集群,因为集群依赖于容量提供程序,而容量提供程序依赖于依赖启动配置的自动缩放组。 So I would have a circular dependency.所以我会有一个循环依赖。 That being said, support for capacity provider in terraform seems completly useless if we cannot add the capacity provider after the cluster creation.话虽如此,如果我们不能在集群创建后添加容量提供者,那么在 terraform 中对容量提供者的支持似乎完全没有用。

The way I deal with this issue is based on the fact that your launch configuration (LC) requires only to know the cluster name .我处理这个问题的方式是基于这样一个事实,即您的启动配置 (LC) 只需要知道集群名称 At present you are hard-coding the name of the cluster in its definition:目前,您正在其定义中硬编码集群的名称:

name = "my-cluster"

Thus, the way I do it is to have a variable with the name:因此,我这样做的方法是使用名称variable

variable "cluster_name" {
   default = "my-cluster"
}

Now you can reference the name anywhere it is needed, without needing to actually create the cluster:现在您可以在任何需要的地方引用该名称,而无需实际创建集群:

# The ECS cluster
resource "aws_ecs_cluster" "my_cluster" {
  name = var.cluster_name
  capacity_providers = [aws_ecs_capacity_provider.my_cp.name]
}
  user_data = <<EOF
    #!/bin/bash
    echo ECS_CLUSTER=${var.cluster_name} >> /etc/ecs/ecs.config
  EOF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM