7

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

3 Answers 3

19

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.

Sign up to request clarification or add additional context in comments.

1 Comment

seems rubbish that Terraform doesn't create all the api resources itself before trying to deploy it?
1

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
              }
            }
          }
        }
      }
    }
  })
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.