我想使用一个摇摇欲坠的文件来描述我的api,并使用terraform来部署我的api网关。 swagger.yaml看起来像这样:
swagger: '2.0'
info:
version: '1.0'
title: "CodingTips"
schemes:
- https
paths:
"/api":
get:
description: "Get coding tips"
produces:
- application/json
x-amazon-apigateway-integration: ${apiIntegration}
responses:
'200':
description: "Codingtips were successfully requested"
Terraform给我一个BadRequestException
,说The REST API doesn't contain any methods
。
因此,我认为它正在尝试部署REST api,而不等待该api的方法和集成被创建。
这使我开始思考必须将DEPENDS_ON
添加到aws_api_gateway_deployment
的方向。但是我不知道要依赖什么,因为我没有使用swagger定义方法和集成资源。应该从大摇大摆的定义中自动扣除它们。
我是否在朝着正确的方向思考,如果是,我该如何使aws_api_gateway_deployment
赖以生存?还是我尝试部署此api的方式有其他问题。
我的apigateway.tf
文件如下:
resource "aws_api_gateway_rest_api" "codingtips-api-gateway" {
name = "ServerlessExample"
description = "Terraform Serverless Application Example"
body = "${data.template_file.codingtips_api_swagger.rendered}"
}
locals{
"get_codingtips_arn" = "${aws_lambda_function.get-tips-lambda.invoke_arn}"
"x-amazon-coding-tips-apigateway-integration" = <<EOF
#
uri = "${local.get_codingtips_arn}"
passthroughBehavior: when_no_match
httpMethod: POST
type: aws_proxy
credentials: "${aws_iam_role.api_gateway_role.arn}"
EOF
}
data "template_file" codingtips_api_swagger{
template = "${file("./swagger.yaml")}"
vars {
apiIntegration = "${indent(8, local.x-amazon-coding-tips-apigateway-integration)}"
}
}
resource "aws_api_gateway_deployment" "codingtips-api-gateway-deployment" {
rest_api_id = "${aws_api_gateway_rest_api.codingtips-api-gateway.id}"
stage_name = "test"
}
如何修复BadRequestException: The REST API doesn't contain any methods
?
答案 0 :(得分:2)
我发现了问题所在。这是locals{}
块中的语法错误。
uri =
应该是uri:
。使用冒号而不是等号。然后,该块如下所示:
locals{
"get_codingtips_arn" = "${aws_lambda_function.get-tips-lambda.invoke_arn}"
"x-amazon-codingtips-get-apigateway-integration" = <<EOF
# comment for new line
uri: "${aws_lambda_function.get-tips-lambda.invoke_arn}"
passthroughBehavior: when_no_match
httpMethod: POST
type: aws_proxy
EOF
}
对此进行了研究,我发现,当您在swagger.yaml中指定x-amazon-apigateway-integration
时,这样阅读起来会更容易:
swagger: '2.0'
info:
version: '1.0'
title: "CodingTips"
schemes:
- https
paths:
"/api":
get:
description: "Get coding tips"
produces:
- application/json
responses:
'200':
description: "The codingtips request was successful."
x-amazon-apigateway-integration:
uri: ${uri_arn}
passthroughBehavior: "when_no_match"
httpMethod: "POST"
type: "aws_proxy"
您的Terraform中的data{}
和locals{}
块如下所示:
data "template_file" codingtips_api_swagger{
template = "${file("swagger.yaml")}"
vars {
uri_arn = "${local.get_codingtips_arn}"
}
}
locals {
"get_codingtips_arn" = "${aws_lambda_function.get-tips-lambda.invoke_arn}"
}