I have 3 autoscaling groups all using slightly different template files. The difference in the 3 template files I have is, each template file is attaching a different EBS volume upon startup of an instance in the autoscaling group. I am trying to figure out how I can pass in these different template files to each autoscaling group using count. Currently the Terraform code is setup to where one template file resource is using 1 file, but I need it to determine how to select the 2nd and 3rd template files. I've done some research on this Cloudinit Config resource that Terraform has integration with, but am not sure if something like this will help me. Any advice would be appreciated. Below is how my current Terraform code is setup.
Template File
data "template_file" "user_data" {
count = "${(var.enable ? 1 : 0) * var.number_of_zones}"
template = "${file("userdata.sh")}"
vars {
node = "Node${count.index + 1}"
}
}
Launch Configuration
resource "aws_launch_configuration" "launch_configuration" {
count = "${(var.enable ? 1 : 0) * var.number_of_zones}"
name = "${var.cluster_name}-launch_node_${count.index}"
key_name = "${var.key_name2}"
image_id = "${lookup(var.amis, "${var.aws_region}.${var.licensee_key == "" && var.licensee == "" ? "enterprise" : "byol"}")}"
user_data = "${element(data.template_file.user_data.*.rendered, count.index)}"
security_groups = [
"${aws_security_group.instance_security_group.id}",
]
instance_type = "${var.instance_type}"
iam_instance_profile = "${aws_iam_instance_profile.instance_host_profile.name}"
ebs_block_device {
device_name = "/dev/sdf"
no_device = true
}
lifecycle {
create_before_destroy = true
}
}
Userdata script
#!/bin/bash
# Attach the right EBS volume
aws ec2 attach-volume --volume-id vol-xxxxxxxxxxxxxxxxx --instance_id `curl http://169.254.169.254/latest/meta-data/instance-id` --device /dev/sdf
Each userdata script I have mounts a different volume on startup of an instance. Any advice on how I can pass in a different value without creating multiple template_file resource blocks would be helpful. Version of Terraform being used is 0.11.10.
You are passing
node
variable to youruser_data
template. You can reference it inside the template using interpolation: