每个循环的Terraform



所以我想为路由表指定gateway_id,但我得到的是:

Error: Missing resource instance key
on modules/vpc/main.tf line 55, in resource "aws_route_table" "RT":
55:       gateway_id = aws_internet_gateway.igw.id

Because aws_internet_gateway.igw has "for_each" set, its attributes must be accessed on
specific instances.

For example, to correlate with indices of a referring resource, use:
aws_internet_gateway.igw[each.key]`

变量.tf

variable "vpc" {
type = map(object({
cidr = string
tags = map(string)
}))
default = {
"main" = {
cidr = "10.0.0.0/16"
tags = {
"Name" = "Main-Vpc"
}
}
}
}
# Subnets
variable "subnets" {
type = map(object({
cidr = string
tags = map(string)
}))
# Privates
default = {
"Private1" = {
cidr = "10.0.10.0/24"
tags = {
"Name" = "Private1"
}
}
"Private2" = {
cidr = "10.0.20.0/24"
tags = {
"Name" = "Private2"
}
}
#Publcs 
"Public1" = {
cidr = "10.0.1.0/24"
tags = {
"Name" = "Public1"
}
}
"Public2" = {
cidr = "10.0.2.0/24"
tags = {
"Name" = "Public2"
}
}
}
}
# Route tables
variable "route-tables" {
type = map(object({
cidr_block = string
tags  = map(string)
}))
default = {
"Public1" = {
cidr_block = "0.0.0.0/0"
tags = {
"Name" = "Public1"
}
}
"Public2" = {
cidr_block = "0.0.0.0/0"
tags = {
"Name" = "Public2"
}
}
"Private1" = {
cidr_block = "0.0.0.0/0"
tags = {
"Name" = "Private1"
}
}
"Private2" = {
cidr_block = "0.0.0.0/0"
tags = {
"Name" = "Private2"
}
}
}
}

主.tf

resource "aws_vpc" "main" {
for_each = var.vpc
cidr_block = each.value["cidr"]
tags = each.value["tags"]
}


# Creting Privates and Public
resource "aws_subnet" "subnets" {
vpc_id = aws_vpc.main["main"].id
for_each = var.subnets
cidr_block = each.value["cidr"]
tags = each.value["tags"]
depends_on = [
aws_vpc.main
]
}
# Gateways and Elastic ip
resource "aws_internet_gateway" "igw" {
for_each = aws_vpc.main
vpc_id = aws_vpc.main["main"].id
}
resource "aws_eip" "elastic" {
vpc = true
}
resource "aws_nat_gateway" "nat" {
allocation_id = aws_eip.elastic.id
subnet_id = aws_subnet.subnets["Public1"].id
tags = {
"Name" = "nat-gateway"
}
depends_on = [
aws_internet_gateway.igw
]
}
# Route tables
resource "aws_route_table" "RT" {
for_each = var.route-tables
tags = each.value["tags"]
vpc_id = aws_vpc.main["main"].id
dynamic "route" {
for_each = var.route-tables
content {
cidr_block = route.value.cidr_block
gateway_id = aws_internet_gateway.igw["main"].id
}
}
}

我试图获得创建的资源igw的价值,但当我使用for_each时,我不知道如何做到这一点。此外,我不确定如何调试它来检查internet_gateway资源的输出!

编辑:如果需要分析,我会发布我的完整代码,也许会有所帮助!

要调试此类问题,请在终端上运行Terraform console,键入aws_internet_gateway.igw并运行enter

它将向你展示这个资源创建了多少个项目,并让你知道如何引用你的igw ID,

根据你的代码,我认为gateway_id = aws_internet_gateway.igw.["main"].id引用你的igw ID会起作用,但我不确定,

使用Terrafrom console进行确认。

更新:

它之所以有效,是因为您没有传递变量var.vpc的值,所以terraform使用了默认值,因此只创建了一个带有关键字"main"的vpc资源

如果你运行地形计划,你会看到:

aws_vpc.main["main"] will be created
+ resource "aws_vpc" "main" {
+ arn                                  = (known after apply)
+ cidr_block                           = "10.0.0.0/16"
+ default_network_acl_id               = (known after apply)
+ default_route_table_id               = (known after apply)
+ default_security_group_id            = (known after apply)
+ dhcp_options_id                      = (known after apply)

当您在igw资源上执行for_each = aws_vpc.main时,for_each接收到一个具有关键字"的元素的映射;"main";,因此,它只创建了一个具有vpc相同密钥的igw资源,即main因此我们能够通过aws_internet_gateway.igw.["main"].id参考它

如果你在var.vpc变量上有多个项目,比如

variable "vpcs" {
type = map(object({
cidr = string
tags = map(string)
}))
default = {
"main" = {
cidr = "10.0.0.0/16"
tags = {
"Name" = "Main-Vpc"
}
}
"boo" = {
cidr = "10.0.0.0/16"
tags = {
"Name" = "Main-Vpc"
}
}
}
}

这将创建两个具有密钥mainboo的vpc,并且当您使用var.vpcs作为igw资源的for_each值时,它将创建具有相同密钥mainboo的两个igw

相关内容

  • 没有找到相关文章

最新更新