【问题标题】:Terraform Error: Error creating API Gateway Deployment: BadRequestException: No integration defined for methodTerraform 错误:创建 API 网关部署时出错:BadRequestException:没有为方法定义集成
【发布时间】:2021-04-22 12:28:34
【问题描述】:
使用 terraform 创建 api_gateway 时出错,下面是我的代码和错误截图。
使用此代码可以创建 REST API,但在部署部分失败...任何人都可以帮我解决这个问题
aws_api_gateway_deployment.api-deployment: Creating...
Error: Error creating API Gateway Deployment: BadRequestException: No integration defined for method
Screenshot of the logs
【问题讨论】:
标签:
aws-api-gateway
terraform-provider-aws
【解决方案1】:
在您的“aws_api_gateway_deployment”资源中,您需要添加一个“depends_on”,其中需要包含以下条目:
- aws_api_gateway_method
- aws_api_gateway_integration
在您的 terraform 脚本中找到,例如:
resource "aws_api_gateway_deployment" "example" {
depends_on = [
aws_api_gateway_method.methodproxy,
aws_api_gateway_integration.apilambda
]
...
}
这两个资源中的任何一个都没有设置的问题。
【解决方案2】:
双重、三重和四重检查您的 JSON 对于 api 主体是否有效。这个错误不是很清楚,但是您的 JSON 格式或结构的任何问题也会导致您得到相同的结果。
我个人没有在我的配置中为aws_api_gateway_method 或aws_api_gateway_integration 定义一个terraform 资源。下面是我对 body 的示例以及修复错误的细微更改。
就上下文而言,这里的目标是使 API Gateway 能够触发 Lambda 函数,这意味着我还定义了一个 aws_lambda_permission 资源,允许 API Gateway 调用 Lambda。
这是在扔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
}
}
}
}
}
})
}
将 JSON 更正为此允许部署通过。
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
}
}
}
}
}
}
})
}