gpt4 book ai didi

swagger - 使用基于 swagger 文件的 terraform 部署 api 网关

转载 作者:行者123 更新时间:2023-12-03 15:49:27 33 4
gpt4 key购买 nike

我想用 terraform 部署我的 api 网关,使用 swagger 文件来描述我的 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 给了我一个 BadRequestExceptionThe REST API doesn't contain any methods .

因此,我认为它正在尝试部署 REST api,而无需等待创建此 api 的方法和集成。

这让我想到了必须添加 DEPENDS_ON 的方向到 aws_api_gateway_deployment .但是我不知道该依赖什么,因为我没有使用 swagger 定义方法和集成资源。它们应该从 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 ?

最佳答案

我发现出了什么问题。这是 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
}

研究这个我发现当你指定 x-amazon-apigateway-integration 时它更容易阅读在 swagger.yaml 中是这样的:
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"
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}"
}

关于swagger - 使用基于 swagger 文件的 terraform 部署 api 网关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54047171/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com