Terraform 在 for_each 上获取列表索引 [英] Terraform get list index on for_each
本文介绍了Terraform 在 for_each 上获取列表索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Terraform 新手在这里.我想使用 for_each
来迭代一个列表,但似乎键和值是相同的:
Terraform newbie here.
I'd like to iterate a list using for_each
, but it seems like the key and value are the same:
provider "aws" {
profile = "default"
region = "us-east-1"
}
variable "vpc_cidrs" {
default = ["10.0.0.0/16", "10.1.0.0/16"]
}
resource "aws_vpc" "vpc" {
for_each = toset(var.vpc_cidrs)
cidr_block = each.value
enable_dns_hostnames = true
tags = { Name = "Company0${each.key}" }
}
我希望标签名称为 "Name" = "Company01"
和 "Name" = "Company02"
但根据 terraform apply代码>,我得到:
"Name" = "Company010.0.0.0/16"
和 "Name" = "Company010.1.0.0/16"
我错过了什么?
I'd like the tag Name to be "Name" = "Company01"
and "Name" = "Company02"
but according to terraform apply
, I get:
"Name" = "Company010.0.0.0/16"
and "Name" = "Company010.1.0.0/16"
What am I missing?
推荐答案
使用 索引函数:
tags = { Name = "Company0${index(var.vpc_cidrs, each.value) + 1}" }
这篇关于Terraform 在 for_each 上获取列表索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文