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

Viewed 1224

I'm trying to create an AWS API Gateway using Terraform, that has a resource and a method but am getting this error: "Invalid Method identifier specified"

The full error (line number don't match the code snippet below):

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" {

I'm unclear what a "Method identifier" is - is it the HTTP method specified (e.g. GET, POST)? In which case, I think I have these defined correctly.

Here is my Terraform code. My aim is to define the following endpoint: 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
  ]
}

I've read a few posts on SO and Github, which suggest implementing depends_on, which I have done but unfortunately it hasn't had an effect. I've run terraform once without the methods/resources/etc (just the gateway rest api resource) to create that first, then made a second run with the resource/method/etc

Thank you

0 Answers
Related