4

我在本地运行 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.

是什么赋予了?

4

1 回答 1

9

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

地图属性和嵌套块之间的主要区别在于地图属性通常具有用户定义的键,就像我们在上面的标签示例中看到的那样,而嵌套块总是具有由资源类型定义的一组固定的支持参数架构,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]
}
于 2020-06-30T06:43:50.470 回答