我陷入了以下问题(terraform v0.12.8);
我要在其中使用以下代码作为我的模块代码,并将它们传递给以下资源;
module "nat_gateway" {
source = "../providers/aws/network/nat_gw/"
total_nat_gws = "${length(var.availability_zones)}"
eip_id = "${module.eip.*.eip_id}"
target_subnet_id = "${module.public_subnet.*.subnet_id}"
nat_gw_name_tag = "NAT-${var.stack_name}"
}
resource "aws_nat_gateway" "nat_gw" {
count = "${var.total_nat_gws}"
allocation_id = "${element("${var.eip_id}", count.index)}"
subnet_id = "${element("${var.target_subnet_id}", count.index)}"
tags = {
Name = "${var.nat_gw_name_tag}"
}
}
我希望,它将使用提供的多个EIP和子网创建多个NAT网关。但失败并显示以下错误;
Error: Incorrect attribute value type
on ../providers/aws/network/nat_gw/nat_gateway.tf line 12, in resource "aws_nat_gateway" "nat_gw":
12: allocation_id = "${element("${var.eip_id}", count.index)}"
Inappropriate value for attribute "allocation_id": string required.
Error: Incorrect attribute value type
on ../providers/aws/network/nat_gw/nat_gateway.tf line 12, in resource "aws_nat_gateway" "nat_gw":
12: allocation_id = "${element("${var.eip_id}", count.index)}"
Inappropriate value for attribute "allocation_id": string required.
Error: Incorrect attribute value type
on ../providers/aws/network/nat_gw/nat_gateway.tf line 13, in resource "aws_nat_gateway" "nat_gw":
13: subnet_id = "${element("${var.target_subnet_id}", count.index)}"
Inappropriate value for attribute "subnet_id": string required.
Error: Incorrect attribute value type
on ../providers/aws/network/nat_gw/nat_gateway.tf line 13, in resource "aws_nat_gateway" "nat_gw":
13: subnet_id = "${element("${var.target_subnet_id}", count.index)}"
有人可以帮助纠正我吗?
答案 0 :(得分:1)
您错误地使用嵌套括号将内容插入其中。
相反,它应如下所示:
resource "aws_nat_gateway" "nat_gw" {
count = "${var.total_nat_gws}"
allocation_id = "${element(var.eip_id, count.index)}"
subnet_id = "${element(var.target_subnet_id, count.index)}"
tags = {
Name = "${var.nat_gw_name_tag}"
}
}
由于您不依赖element
的行为,该行为允许通过选择长度大于长度的索引来循环遍历列表,因此可以简化为:
resource "aws_nat_gateway" "nat_gw" {
count = "${var.total_nat_gws}"
allocation_id = "${var.eip_id[count.index]}"
subnet_id = "${var.target_subnet_id[count.index]}"
tags = {
Name = "${var.nat_gw_name_tag}"
}
}
因为您使用的是Terraform 0.12,所以也可以直接使用变量,而不是使用插值语法来进一步扩展:
resource "aws_nat_gateway" "nat_gw" {
count = var.total_nat_gws
allocation_id = var.eip_id[count.index]
subnet_id = var.target_subnet_id[count.index]
tags = {
Name = var.nat_gw_name_tag
}
}