首页 > 解决方案 > 创建 API 网关方法时出错响应:NotFoundException:指定的方法标识符无效

问题描述

我正在尝试使用 Terraform 创建一个 AWS API 网关,该网关具有资源和方法,但出现此错误:“指定的方法标识符无效”

完整错误(行号与下面的代码片段不匹配):

Error: Error creating API Gateway Method Response: NotFoundException: Invalid Method identifier specified

      on api-gateway/api-gateway.tf line 204, in resource "aws_api_gateway_method_response" "method-response-public-get-sites":
     204: resource "aws_api_gateway_method_response" "method-response-public-get-sites" {
    
    
    
    Error: Error creating API Gateway Integration: NotFoundException: Invalid Method identifier specified
    
      on api-gateway/api-gateway.tf line 214, in resource "aws_api_gateway_integration" "method-integration-public-get-sites":
     214: resource "aws_api_gateway_integration" "method-integration-public-get-sites" {

我不清楚“方法标识符”是什么——它是指定的 HTTP 方法(例如 GET、POST)吗?在这种情况下,我认为我已经正确定义了这些。

这是我的 Terraform 代码。我的目标是定义以下端点:GET /public/sites.

resource "aws_api_gateway_rest_api" "api" {
  name        = "${var.api_gateway_name}-api"
  description = "${var.api_gateway_name} api"
  policy      = data.aws_iam_policy_document.api_policy_doc.json

  endpoint_configuration {
    types = [
      "REGIONAL"
    ]
  }
}

resource "aws_api_gateway_resource" "resource-public" {
  rest_api_id = aws_api_gateway_rest_api.api.id
  parent_id   = aws_api_gateway_rest_api.api.root_resource_id
  path_part   = "public"
  depends_on = [aws_api_gateway_rest_api.api]
}

resource "aws_api_gateway_resource" "resource-public-sites" {
  rest_api_id = aws_api_gateway_rest_api.api.id
  parent_id   = aws_api_gateway_resource.resource-public.id
  path_part   = "sites"
  depends_on = [
    aws_api_gateway_resource.resource-public
  ]
}

resource "aws_api_gateway_method" "method-public-get-sites" {
  rest_api_id   = aws_api_gateway_rest_api.api.id
  resource_id   = aws_api_gateway_resource.resource-public.id
  http_method   = "GET"
  authorization = "NONE"
  depends_on = [
    aws_api_gateway_resource.resource-public-sites
  ]
}

resource "aws_api_gateway_method_response" "method-response-public-get-sites" {
  rest_api_id = aws_api_gateway_rest_api.api.id
  resource_id = aws_api_gateway_resource.resource-public-sites.id
  http_method = aws_api_gateway_method.method-public-get-sites.http_method
  status_code = "200"
  depends_on = [
    aws_api_gateway_method.method-public-get-sites
  ]
}

resource "aws_api_gateway_integration" "method-integration-public-get-sites" {
  rest_api_id = aws_api_gateway_rest_api.api.id
  resource_id = aws_api_gateway_resource.resource-public-sites.id
  http_method = aws_api_gateway_method.method-public-get-sites.http_method
  integration_http_method = "POST"
  type = "AWS_PROXY"
  uri = var.lambda_invoke_arn
  depends_on = [
    aws_api_gateway_method.method-public-get-sites
  ]
}

resource "aws_api_gateway_integration_response" "integration-response-public-get-sites" {
  rest_api_id = aws_api_gateway_rest_api.api.id
  resource_id = aws_api_gateway_resource.resource-public.id
  http_method = aws_api_gateway_method.method-public-get-sites.http_method
  status_code = aws_api_gateway_method_response.method-response-public-get-sites.status_code

  depends_on = [
    aws_api_gateway_integration.method-integration-public-get-sites,
    aws_api_gateway_method_response.method-response-public-get-sites
  ]
}

我已经阅读了一些关于 SO 和 Github 的帖子,其中建议实施depends_on,我已经这样做了,但不幸的是它没有产生效果。我已经在没有方法/资源/等(只是网关 rest api 资源)的情况下运行了一次 terraform 来创建它,然后使用资源/方法/等进行了第二次运行

谢谢

标签: amazon-web-servicesterraformaws-api-gatewayterraform-provider-aws

解决方案


推荐阅读