gpt4 book ai didi

amazon-web-services - Terraform:在应用之前无法确定数据源上的长度()?

转载 作者:行者123 更新时间:2023-12-04 13:41:08 29 4
gpt4 key购买 nike

我正在尝试动态声明多个 aws_nat_gateway通过 aws_subnet_ids 检索公共(public)子网列表来获取数据源数据源。但是,当我尝试设置 count 时参数等于子网 ID 的长度,我收到错误消息 The "count" value depends on resource attributes that cannot be determined until apply... .

这几乎与 example in their documentation 直接矛盾。 !。我该如何解决?他们的文件有错吗?

我正在使用 Terraform v0.12。

data "aws_vpc" "environment_vpc" {
id = var.vpc_id
}

data "aws_subnet_ids" "public_subnet_ids" {
vpc_id = data.aws_vpc.environment_vpc.id
tags = {
Tier = "public"
}
depends_on = [data.aws_vpc.environment_vpc]
}

data "aws_nat_gateway" "nat_gateway" {
count = length(data.aws_subnet_ids.public_subnet_ids.ids) # <= Error
subnet_id = data.aws_subnet_ids.public_subnet_ids.ids.*[count.index]
depends_on = [data.aws_subnet_ids.public_subnet_ids]
}

我希望能够成功应用此模板,但出现以下错误:
Error: Invalid count argument

on ../src/variables.tf line 78, in data "aws_nat_gateway" "nat_gateway":
78: count = "${length(data.aws_subnet_ids.public_subnet_ids.ids)}"

The "count" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the count depends on.

最佳答案

似乎您正在尝试获取尚未创建或无法确定的子网,terraform cmd 输出建议您添加 -target 标志以创建 VPC 和子网或先执行其他任务,之后,您将应用 nat_gateway 资源。我建议您使用 AZ 列表而不是子网 ID,我将在下面添加一个简单的示例。

variable "vpc_azs_list" {
default = [
"us-east-1d",
"us-east-1e"
]
}

resource "aws_nat_gateway" "nat" {
count = var.enable_nat_gateways ? length(var.azs_list) : 0
allocation_id = "xxxxxxxxx"
subnet_id = "xxxxxxxxx"
depends_on = [
aws_internet_gateway.main,
aws_eip.nat_eip,
]
tags = {
"Name" = "nat-gateway-name"
"costCenter" = "xxxxxxxxx"
"owner" = "xxxxxxxxx"
}
}
我希望对您和其他用户有用。

关于amazon-web-services - Terraform:在应用之前无法确定数据源上的长度()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56814103/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com