如何解决错误:Terraform 中的“不得引用参数名称”?

Bec*_*cks 3 amazon-web-services terraform aws-api-gateway terraform-provider-aws terraform0.12+

我在本地运行 Terraform 0.12.24

我正在尝试部署与 Lambda 的 API 网关集成

我正在尝试使用 Terraform 启用 AWS API GW CORS。

对于 OPTIONS 方法响应,我有以下资源:

resource "aws_api_gateway_method_response" "options_200" {
    rest_api_id   = aws_api_gateway_rest_api.scout-approve-api-gateway.id
    resource_id   = aws_api_gateway_resource.proxy.id
    http_method   = aws_api_gateway_method.options_method.http_method
    status_code   = "200"

    response_models {
      "application/json" = "Empty"
    }

    response_parameters {
        "method.response.header.Access-Control-Allow-Headers" = true,
        "method.response.header.Access-Control-Allow-Methods" = true,
        "method.response.header.Access-Control-Allow-Origin" = true
    }
    depends_on = [aws_api_gateway_method.options_method]
}
Run Code Online (Sandbox Code Playgroud)

我得到:

Error: Invalid argument name

  on main.tf line 48, in resource "aws_api_gateway_method_response" "options_200":
  48:       "application/json" = "Empty"

Argument names must not be quoted.
Run Code Online (Sandbox Code Playgroud)

是什么赋予了?

yda*_*coR 7

这实际上是解析器误解错误在哪里。它实际上是在抱怨它试图将response_models和读取response_parameters为块而不是属性。在0.12 文档中有对此的进一步讨论。

map 属性和嵌套块之间的主要区别在于 map 属性通常具有用户定义的键,就像我们在上面的标签示例中看到的那样,而嵌套块始终具有由资源类型定义的一组固定的受支持参数模式,Terraform 将对其进行验证。

在 0.11 中,您可以交替使用块语法(只是大括号,例如response_parameters { ... })作为属性,但在 0.12 中,它对类型更加严格,因此不再可能。作为工作示例链接到的 Medium 帖子中的代码是 0.11 代码,在 0.12 中无效。如果您仔细查看您还链接的 GitHub 代码,您会发现它使用属性语法而不是块语法,因此是有效的。

通过添加 切换到使用属性语法=将使这项工作按预期进行:

resource "aws_api_gateway_method_response" "options_200" {
  rest_api_id = aws_api_gateway_rest_api.scout-approve-api-gateway.id
  resource_id = aws_api_gateway_resource.proxy.id
  http_method = aws_api_gateway_method.options_method.http_method
  status_code = "200"

  response_models = {
    "application/json" = "Empty"
  }

  response_parameters = {
    "method.response.header.Access-Control-Allow-Headers" = true,
    "method.response.header.Access-Control-Allow-Methods" = true,
    "method.response.header.Access-Control-Allow-Origin"  = true
  }

  depends_on = [aws_api_gateway_method.options_method]
}
Run Code Online (Sandbox Code Playgroud)