Hi all,
I'm trying to use count.index to add tags to volumes attached to some aws instance created with count = N. This is the complete Terraform code:
resource "aws_instance" "prod-db" {
ami = "ami-xxxxxxxx"
instance_type = "r4.2xlarge"
key_name = "operation-prod"
availability_zone = "${var.availability-zones[count.index%2]}"
subnet_id = "${var.prod-db-subnets[var.availability-zones[count.index%2]]}"
vpc_security_group_ids = ["sg-xxxxxx", "sg-yyyyyy", "sg-zzzzzz", "sg-kkkkkk"]
ebs_optimized = "true"
tags = "${merge(
local.common_tags,
map(
"Name", "prod-db${count.index+1}",
"Product", "xx",
"Role", "yy",
)
)}"
iam_instance_profile = "prod-db"
count = 2
}
resource "aws_ebs_volume" "prod-db_opt" {
availability_zone = "${element(aws_instance.prod-db.*.availability_zone, count.index)}"
size = 20
tags = "${aws_instance.prod-db.0.tags}"
count = 2
}
This code is syntactically correct but the tag Name will be "prod-db1" for all the volumes. I couldn't find a way to replace "aws_instance.prod-db.0.tags" with "aws_instance.prod-db.N.tags" where N is the expansion of ${count.index} so that each volume has the same name of the attached instance.