Terraform 属性 "route" 的值不合适
Terraform Inappropriate value for attribute "route"
对 Terraform 比较陌生,目前正在尝试在 AWS 中构建云基础设施。
当我使用资源 aws_route_table (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table)
文档中的官方示例(稍作更改)时出现错误
resource "aws_route_table" "prod-route-table" {
vpc_id = aws_vpc.prod-vpc.id
route = [{
# Route all Traffic to the internet gateway
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
},{
ipv6_cidr_block = "::/0"
gateway_id = aws_internet_gateway.gw.id
}]
}
我收到以下错误消息
Error: Incorrect attribute value type
│ Inappropriate value for attribute "route": element 0: attributes "carrier_gateway_id",
│ "destination_prefix_list_id", "egress_only_gateway_id", "instance_id", "ipv6_cidr_block",
│ "local_gateway_id", "nat_gateway_id", "network_interface_id", "transit_gateway_id", "vpc_endpoint_id",
│ and "vpc_peering_connection_id" are required.
添加所有这些属性可以解决错误,但这会大量破坏代码。
以不同的方式编写(见下文)不会导致错误,是不是 terraform AWS 文档不正确,因为他们清楚地说明了第一种编写方式?
resource "aws_route_table" "prod-route-table" {
vpc_id = aws_vpc.prod-vpc.id
route {
# Route all Traffic to the internet gateway
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
route{
ipv6_cidr_block = "::/0"
gateway_id = aws_internet_gateway.gw.id
}
}
我正在使用 terraform v1.0.10 和 aws provider version = "3.63.0"
提前致谢
该参数的文档提到它使用的是旧版 attributes as blocks mode,这是 Terraform v0.12 的遗留版本,适用于提供者依赖于能够在两个嵌套块中写入某些参数的某些情况语法(如第二个示例)和属性语法(如第一个示例)。
文档示例中当前显示的语法——以及您问题中的第一个示例——与 the advice about how to write a fixed value(与动态值相反)相反,因此您在此处显示的第二个示例确实会就一般 Terraform 文档而言,这是首选方式。
resource "aws_route_table" "example" {
vpc_id = aws_vpc.example.id
route {
cidr_block = "10.0.1.0/24"
gateway_id = aws_internet_gateway.example.id
}
route {
ipv6_cidr_block = "::/0"
egress_only_gateway_id = aws_egress_only_internet_gateway.example.id
}
tags = {
Name = "example"
}
}
可能 AWS 提供商文档的作者在这里使用属性语法来显示与设置 route = []
的特殊情况的对称性以明确声明根本不应该有路由,因为不幸的是完全省略了这个参数(出于历史原因)意味着忽略远程 API 中的任何现有路由,而不是删除远程 API.
中的所有现有路由
关于您在后续部分中看到的行为的更多信息 arbitrary expressions with argument syntax:
Because of the rule that argument declarations like this fully override any default value, when creating a list-of-objects expression directly the usual handling of optional arguments does not apply, so all of the arguments must be assigned a value, even if it's an explicit null:
example = [
{
# Cannot omit foo in this case, even though it would be optional in the
# nested block syntax.
foo = null
},
]
随着时间的推移,提供商将逐步淘汰这种遗留模式,但必须谨慎行事,因为它可能对某些现有配置造成破坏性变化。不幸的是,直到那时,对于某些特定的提供者属性来说,这是一个令人困惑的边缘,尽管它们至少应该 link 到我上面 link 编辑的相关文档页面,以注意它们的行为与正常情况不匹配Terraform 参数处理行为。
对 Terraform 比较陌生,目前正在尝试在 AWS 中构建云基础设施。 当我使用资源 aws_route_table (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table)
文档中的官方示例(稍作更改)时出现错误resource "aws_route_table" "prod-route-table" {
vpc_id = aws_vpc.prod-vpc.id
route = [{
# Route all Traffic to the internet gateway
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
},{
ipv6_cidr_block = "::/0"
gateway_id = aws_internet_gateway.gw.id
}]
}
我收到以下错误消息
Error: Incorrect attribute value type
│ Inappropriate value for attribute "route": element 0: attributes "carrier_gateway_id",
│ "destination_prefix_list_id", "egress_only_gateway_id", "instance_id", "ipv6_cidr_block",
│ "local_gateway_id", "nat_gateway_id", "network_interface_id", "transit_gateway_id", "vpc_endpoint_id",
│ and "vpc_peering_connection_id" are required.
添加所有这些属性可以解决错误,但这会大量破坏代码。 以不同的方式编写(见下文)不会导致错误,是不是 terraform AWS 文档不正确,因为他们清楚地说明了第一种编写方式?
resource "aws_route_table" "prod-route-table" {
vpc_id = aws_vpc.prod-vpc.id
route {
# Route all Traffic to the internet gateway
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
route{
ipv6_cidr_block = "::/0"
gateway_id = aws_internet_gateway.gw.id
}
}
我正在使用 terraform v1.0.10 和 aws provider version = "3.63.0"
提前致谢
该参数的文档提到它使用的是旧版 attributes as blocks mode,这是 Terraform v0.12 的遗留版本,适用于提供者依赖于能够在两个嵌套块中写入某些参数的某些情况语法(如第二个示例)和属性语法(如第一个示例)。
文档示例中当前显示的语法——以及您问题中的第一个示例——与 the advice about how to write a fixed value(与动态值相反)相反,因此您在此处显示的第二个示例确实会就一般 Terraform 文档而言,这是首选方式。
resource "aws_route_table" "example" {
vpc_id = aws_vpc.example.id
route {
cidr_block = "10.0.1.0/24"
gateway_id = aws_internet_gateway.example.id
}
route {
ipv6_cidr_block = "::/0"
egress_only_gateway_id = aws_egress_only_internet_gateway.example.id
}
tags = {
Name = "example"
}
}
可能 AWS 提供商文档的作者在这里使用属性语法来显示与设置 route = []
的特殊情况的对称性以明确声明根本不应该有路由,因为不幸的是完全省略了这个参数(出于历史原因)意味着忽略远程 API 中的任何现有路由,而不是删除远程 API.
关于您在后续部分中看到的行为的更多信息 arbitrary expressions with argument syntax:
Because of the rule that argument declarations like this fully override any default value, when creating a list-of-objects expression directly the usual handling of optional arguments does not apply, so all of the arguments must be assigned a value, even if it's an explicit null:
example = [ { # Cannot omit foo in this case, even though it would be optional in the # nested block syntax. foo = null }, ]
随着时间的推移,提供商将逐步淘汰这种遗留模式,但必须谨慎行事,因为它可能对某些现有配置造成破坏性变化。不幸的是,直到那时,对于某些特定的提供者属性来说,这是一个令人困惑的边缘,尽管它们至少应该 link 到我上面 link 编辑的相关文档页面,以注意它们的行为与正常情况不匹配Terraform 参数处理行为。