I have an ecs_cluster
module which defines an ECS cluster. I want the module to be re-usable so I can create various clusters with different configurations. Hence I want to be able to optionally specify whether to create and attach an EBS volume in the launch configuration of the ECS hosts.
I initially tried using count
in the ebs_block_device
inside the launch configuration e.g.
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}"
}
}
However this results in the following error:
module.ecs_cluster.aws_launch_configuration.launch_configuration: ebs_block_device.0: invalid or unknown key: count
I then tried specifying the launch_configuration resource twice, once with and once without the ebs block device e.g.
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}"
}
}
However Terraform then complains because the resource is defined twice.
I can't change the id of either of the resources as I have an auto scaling group which depends upon the name of the launch configuration e.g.
resource "aws_autoscaling_group" "autoscaling_group" {
name = "foo"
launch_configuration = "${aws_launch_configuration.launch_configuration.name}"
}
I guess I could conditionally define 2 autoscaling groups and map one to each launch configuration but this feels really messy. Also these resources themselves have dependent resources such as cloudwatch metric alarms etc. It feels very unDRY to repeat all of this code twice with 2 separate conditions. Am I missing a trick here?
Grateful for any relevant Terraform wisdom!