Terraform Error: Error creating API Gateway Deployment: BadRequestException: No integration defined for method

Viewed 5290

Getting error while creating api_gateway using terraform, Below is my code and the error screenshot.. with this code am able to create REST API, but failing in deployment section... can anyone please help me in this

aws_api_gateway_deployment.api-deployment: Creating...

Error: Error creating API Gateway Deployment: BadRequestException: No integration defined for method

Screenshot of the logs

2 Answers

In your "aws_api_gateway_deployment" resource you will need to add a "depends_on" which will need to contain entries for:

  • aws_api_gateway_method
  • aws_api_gateway_integration

that are found in your terraform script, for example:

   resource "aws_api_gateway_deployment" "example" {
    
      depends_on = [
        aws_api_gateway_method.methodproxy,
        aws_api_gateway_integration.apilambda
      ]
      ...
   }

The problem it from either of the two resource not having been setup.

Double, triple, and quadruple check your JSON is a valid for the api body. This error is not very clear, but any issues with your JSON formatting or structure will also cause you to get the same result.

I personally did not define a terraform resource for aws_api_gateway_method or aws_api_gateway_integration in my configuration. Here is an example of what I had for a body and the subtle change that fixed the error.

For context, the goal here was to enable API Gateway to trigger a Lambda Function which means I also had a aws_lambda_permission resource defined that allows invocation of Lambda by API Gateway.

This was throwing Error: Error creating API Gateway Deployment: BadRequestException: No integration defined for method:

locals {
  body = jsonencode({
    swagger = "2.0"
    info = {
      title   = "Example"
      version = "1.0"
    }
    schemes = [
        "https"
    ]
    paths = {
      "/path" = {
        get = {
          responses = {
            "200" = {
              description = "200 response"
            }
          }
        }
        x-amazon-apigateway-integration = {
          type       = "AWS"
          uri        = module.lambda.invoke_arn
          httpMethod = "POST"
          responses = {
            default = {
              statusCode = 200
            }
          }
        }
      }
    }
  })
}

Correcting the JSON to this allowed the deployment to go through.

locals {
  body = jsonencode({
    swagger = "2.0"
    info = {
      title   = "Example"
      version = "1.0"
    }
    schemes = [
        "https"
    ]
    paths = {
      "/path" = {
        get = {
          responses = {
            "200" = {
              description = "200 response"
            }
          }
          x-amazon-apigateway-integration = {
            type       = "AWS"
            uri        = module.lambda.invoke_arn
            httpMethod = "POST"
            responses = {
              default = {
                statusCode = 200
              }
            }
          }
        }
      }
    }
  })
}
Related