我正在尝试使用terraform向ECS集群添加一个容量提供程序,这样它就可以自动运行。自动标度组需要知道集群才能在集群中创建实例,但是集群还需要通过其容量提供程序了解自动标度组。如何使用terraform和容量提供程序解决这个循环依赖关系?
下面是我创建集群的基础结构代码
# 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
}
}
}以下是自动标度组及其依赖项的基础结构代码
# 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
}
}在应用这个terraform时,我确实在集群上得到了一个容量提供程序,但是实例在集群default中而不是my-cluster中。有些人会说我只需要补充
user_data = <<EOF
#!/bin/bash
echo ECS_CLUSTER=${aws_ecs_cluster.my_cluster.name} >> /etc/ecs/ecs.config
EOF但是我不能在启动配置中引用集群,因为集群依赖于依赖于启动配置的自动标度组的容量提供程序。所以我会有一个循环依赖。尽管如此,如果我们不能在集群创建之后添加容量提供者,那么对terraform中的容量提供者的支持似乎完全无用。
发布于 2020-08-27 23:57:41
我处理这个问题的方式是基于以下事实:您的启动配置(LC)只需要知道集群名。目前,您是硬编码,在其定义中是集群的名称:
name = "my-cluster"因此,我这样做的方式是有一个名为variable的:
variable "cluster_name" {
default = "my-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
EOFhttps://stackoverflow.com/questions/63619198
复制相似问题