Terraform 属性“路线"的值不合适 [英] Terraform Inappropriate value for attribute "route"

查看:15
本文介绍了Terraform 属性“路线"的值不合适的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 terraform 来说相对较新,目前正尝试在 AWS 中构建云基础设施.当我使用资源 aws_route_table (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table)

relatively new to terraform and currently trying to build cloud infrastructure in AWS. I get an error when I use an official example (little bit changed) from the documentation for the resource 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 文档是否不正确,因为它们清楚地说明了第一种编写方式?

Adding all these attributes solves the error however this blows up the code massively. Writing it differently (see the following) results in no errors, is the terraform AWS documentation incorrect, as they clearly state the first way of writing it?

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"
提前致谢

I am using terraform v1.0.10 and aws provider version = "3.63.0"
Thanks in advance

推荐答案

该参数的文档提到它使用的是旧版 属性为块模式,这是 Terraform v0.12 的保留,适用于提供者依赖于能够在嵌套块语法中编写某些参数的情况(如您的第二个示例)和属性语法(如您的第一个示例).

The documentation for that argument mentions that it's using the legacy attributes as blocks mode, which is a holdover from Terraform v0.12 for some situations where providers were depending on being able to write certain arguments in both the nested block syntax (like in your second example) and in the attribute syntax (like in your first example).

文档示例中当前显示的语法 - 以及您问题中的第一个示例 - 与 关于如何编写固定值的建议(与动态值相反),所以确实是您展示的第二个示例就一般 Terraform 文档而言,这将是首选方式.

The syntax currently shown in the doc example -- and your first example in your question -- is contrary to the advice about how to write a fixed value (as opposed to a dynamic value), so indeed the second example you showed here would be the preferred way as far as the general Terraform documentation is concerned.

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 中的所有现有路由.

Possibly the AWS provider documentation author here used the attribute syntax in order to show symmetry with the special case of setting route = [] to explicitly state that there should be no routes at all, because entirely omitting this argument unfortunately (for historical reasons) means to ignore any existing routes in the remote API, rather than to remove all existing routes in the remote API.

关于您在后续部分中看到的行为的更多信息 带参数语法的任意表达式:

There's a little more about the behavior you saw in the subsequent section 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
  },
]

随着时间的推移,供应商将逐步淘汰这种传统模式,但必须谨慎行事,因为它可能对某些现有配置造成重大变化.在那之前,不幸的是,对于某些特定的提供者属性来说,这是一个令人困惑的粗糙边缘,尽管它们至少应该链接到我上面链接的相关文档页面,以注意它们的行为与正常的 Terraform 参数处理行为不匹配.

Over time providers will gradually phase out this legacy mode, but must do so cautiously because it can be a breaking change for some existing configurations. Until then this is unfortunately a confusing rough edge for some particular provider attributes, though they should at least all link to the relevant documentation page I linked above to note that their behavior doesn't match the normal Terraform argument-handling behaviors.

这篇关于Terraform 属性“路线"的值不合适的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆