我有一个ecs_cluster
定义 ECS 集群的模块。我希望该模块可以重复使用,这样我就可以创建具有不同配置的各种集群。因此,我希望能够选择指定是否在 ECS 主机的启动配置中创建和附加 EBS 卷。
我最初尝试使用count
in the ebs_block_device
在启动配置中,例如
variable "ebs_volume_device_name" { type = "string", default = "" }
variable "ebs_volume_type" { type = "string", default = "" }
variable "ebs_volume_size" { type = "string", default = "" }
resource "aws_launch_configuration" "launch_configuration" {
name_prefix = "foo"
image_id = "bar"
# Irrelevant stuff removed for brevity...
ebs_block_device {
count = "${length(var.ebs_volume_device_name) > 0 ? 1 : 0}"
device_name = "${var.ebs_volume_device_name }"
volume_type = "${var.ebs_volume_type}"
volume_size = "${var.ebs_volume_size}"
}
}
但这会导致以下错误:
module.ecs_cluster.aws_launch_configuration.launch_configuration: ebs_block_device.0: invalid or unknown key: count
然后,我尝试指定 launch_configuration 资源两次,一次使用 ebs 块设备,一次不使用 ebs 块设备,例如
variable "ebs_volume_device_name" { type = "string", default = "" }
variable "ebs_volume_type" { type = "string", default = "" }
variable "ebs_volume_size" { type = "string", default = "" }
resource "aws_launch_configuration" "launch_configuration" {
count = "${length(var.ebs_volume_device_name) == 0 ? 1 : 0}"
name_prefix = "foo"
image_id = "bar"
# Irrelevant stuff removed for brevity...
# No specification of ebs_block_device
}
resource "aws_launch_configuration" "launch_configuration" {
count = "${length(var.ebs_volume_device_name) > 0 ? 1 : 0}"
name_prefix = "foo"
image_id = "bar"
# Irrelevant stuff removed for brevity...
ebs_block_device {
device_name = "${var.ebs_volume_device_name }"
volume_type = "${var.ebs_volume_type}"
volume_size = "${var.ebs_volume_size}"
}
}
然而,Terraform 随后会抱怨,因为资源被定义了两次。
我无法更改任一资源的 ID,因为我有一个自动缩放组,该组取决于启动配置的名称,例如
resource "aws_autoscaling_group" "autoscaling_group" {
name = "foo"
launch_configuration = "${aws_launch_configuration.launch_configuration.name}"
}
我想我可以有条件地定义 2 个自动缩放组,并将一个组映射到每个启动配置,但这感觉真的很混乱。此外,这些资源本身也有依赖的资源,例如 cloudwatch 指标警报等。使用 2 个单独的条件重复所有这些代码两次感觉非常不干。我在这里错过了一个技巧吗?
感谢任何相关的 Terraform 智慧!