Terraform:如何在根目录下创建 api 网关 POST 方法?

nea*_*us3 3 amazon-web-services terraform aws-api-gateway

我正在尝试在根 api 网关 URL 处使用 terraform 创建 POST 方法,例如https://somehash.execute-api.us-east-1.amazonaws.com/dev,其中将包含舞台。这是我们关注的 terraform 计划的一部分:

resource "aws_api_gateway_rest_api" "api" {
  name = "submit-dev-gateway-api"
}

resource "aws_api_gateway_resource" "resource" {
  rest_api_id = "${aws_api_gateway_rest_api.api.id}"
  parent_id = "${aws_api_gateway_rest_api.api.root_resource_id}"
  path_part = "submit"
}

resource "aws_api_gateway_method" "post_form" {
  rest_api_id   = "${aws_api_gateway_rest_api.api.id}"
  resource_id   = "${aws_api_gateway_resource.resource.id}"
  http_method   = "POST"
  authorization = "NONE"
}
...
Run Code Online (Sandbox Code Playgroud)

我尝试将 path_part 更改为“/”,但没有用。没有 aws_api_gateway_resource,我无法创建 aws_api_gateway_method 资源。我可以在没有 terraform 的情况下在 root 手动创建一个 POST,如下所示: 手动的

当我使用上面的 terraform 计划时,我得到了这个: 地形

如何使用 terraform 在 root 处创建 POST?

Mar*_*ins 12

“根资源”是在创建 API Gateway REST API 的过程中自动创建的。在 Terraform 中,该根资源的 id 作为REST API 资源实例root_resource_id属性公开。

因为该资源是作为 API 的一部分隐式创建的,所以我们不需要单独的资源。相反,我们可以直接将方法(和其他必要的下游对象)附加到现有的根资源上:

resource "aws_api_gateway_rest_api" "api" {
  name = "submit-dev-gateway-api"
}

resource "aws_api_gateway_method" "post_form" {
  rest_api_id   = aws_api_gateway_rest_api.api.id
  resource_id   = aws_api_gateway_rest_api.api.root_resource_id
  http_method   = "POST"
  authorization = "NONE"
}
Run Code Online (Sandbox Code Playgroud)