Narasimha
Narasimha

Reputation: 71

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

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

Upvotes: 7

Views: 20785

Answers (3)

Justin
Justin

Reputation: 745

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"
            }
          }                      <-- the change is here
          x-amazon-apigateway-integration = {
            type       = "AWS"
            uri        = module.lambda.invoke_arn
            httpMethod = "POST"
            responses = {
              default = {
                statusCode = 200
              }
            }
          }
        }
      }
    }
  })
}

Upvotes: 1

Rahul Kodumuru
Rahul Kodumuru

Reputation: 99

I faced this error; the problem was that a dev created a new resource (endpoint) via the AWS console in the same API terraform trying to redeploy. He hasn't integrated that endpoint with any of the services.

Once we removed that partial resource, terraform apply worked fine without any issues.

Upvotes: 0

devguy
devguy

Reputation: 771

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.

Upvotes: 17

Related Questions