【问题标题】:How do I resolve the error: "Argument names must not be quoted" in Terraform?如何解决错误:Terraform 中的“不得引用参数名称”?
【发布时间】:2020-10-20 09:42:10
【问题描述】:

我在本地运行 Terraform 0.12.24

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

我正在尝试使用 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]
}

我得到:

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.

什么给了?

【问题讨论】:

标签: amazon-web-services terraform aws-api-gateway terraform-provider-aws terraform0.12+


【解决方案1】:

这其实是解析器误会了错误在哪里。它实际上是在抱怨它试图将response_modelsresponse_parameters 作为块而不是属性来读取。 0.12 documentation 对此有进一步的讨论。

地图属性和嵌套块之间的主要区别在于,地图属性通常具有用户定义的键,就像我们在上面的标签示例中看到的那样,而嵌套块总是有一组固定的支持参数,由Terraform 将验证的资源类型架构。

在 0.11 中,您可以互换地使用块语法(只是花括号,例如 response_parameters { ... })作为属性,但在 0.12 中,类型更严格,因此不再可能。 The code in the Medium post you linked to 作为一个工作示例是 0.11 代码,在 0.12 中无效。如果您仔细查看the GitHub code you also linked,您会发现它使用属性语法而不是块语法,因此是有效的。

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

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]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2012-03-03
    • 2017-08-13
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 2017-10-27
    相关资源
    最近更新 更多