How to interpolate count.index in the tags resource of an aws_ebs_volume

187 views
Skip to first unread message

Jonah

unread,
Aug 30, 2018, 4:31:47 AM8/30/18
to Terraform
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.


Matthew Ceroni

unread,
Sep 2, 2018, 11:35:33 PM9/2/18
to Terraform
You set the tags to the tags of only the first instance resource. Just use element like you do for the attachment.

Miroslav E. Hadzhiev

unread,
Sep 4, 2018, 12:57:29 AM9/4/18
to Terraform
You also need to put spaces between the "+" sign and the other chars.
Reply all
Reply to author
Forward
0 new messages