amazon-web-services - 如何解决错误 : "Argument names must not be quoted" in Terraform?

标签 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]
}
我得到:
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.
是什么赋予了?

最佳答案

这其实是解析器误会错误在哪里。它实际上是在提示它正在尝试阅读 response_modelsresponse_parameters作为块而不是属性。在 0.12 documentation 中有对此的进一步讨论.

The main difference between a map attribute and a nested block is that a map attribute will usually have user-defined keys, like we see in the tags example above, while a nested block always has a fixed set of supported arguments defined by the resource type schema, which Terraform will validate.


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

关于amazon-web-services - 如何解决错误 : "Argument names must not be quoted" in Terraform?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62651475/

相关文章:

php - AWS 上的 Cron(或一般的分布式系统)

amazon-web-services - 亚马逊AWS CLI删除ApiGateway,可能吗?

node.js - 使用 node-redis 将 AWS redis 连接到 Node

terraform - 拆分 terraform tfstate 文件

Azure:使用 Terraform 服务主要用户最佳实践

linux - EC2 实例的 SSL 证书

aws-lambda - 如何通过 lambda 代理集成云形成 API 网关资源

amazon-web-services - 部署 lambda 函数时出现 AccessDeniedException

kubernetes - 无法在 Terraform 中部署 Helm Chart(Kubernetes 集群无法访问)

websocket - 连接到 AWS API Gateway websocket 时返回连接 ID