Inline for_each with tag

I’m trying to use for each with tags, would this work? My goal is to setup a new tag as variable for each vpc. Thats why I’m using for_each with tags.
Is there a better method for tags to be iterate via list?

list doesn’t work

resource “aws_vpc” “vpc-cidr” {

# count = length(var.vpc_cidr)

# cidr_block = var.vpc_cidr[count.index]

cidr_block = var.vpc_cidr

dynamic “tag” {

for_each = var.vpc-tag_map

content {

key = tag.key

value = tag.value

}

}

}

Hi naqvisn!

If tag is a block, it is generally easier to use for_each with a map. If the tag attribute is an argument (which is the case in some of the AWS resources), it is simpler to use a list to iterate as per the 0.12 examples here. There is an example on the bottom that uses list comprehension add values with the list of maps that you can try!

Let us know how it goes.

In addition to @joatmon08’s suggestions, it seems like your var.vpc-tag_map is already structured the way that the tags argument for aws_vpc expects, so it could work to just assign it directly if you don’t need to do any further transformation of the map:

resource "aws_vpc" "vpc_cidr" {
  # ...

  tags = var.vpc-tag_map
}