【发布时间】:2020-05-29 13:28:49
【问题描述】:
我的 REST API 有 OpenAPI 3.0 规范 (YAML),我需要在 Terraform 模板 (HCL) 中描述与 AWS API Gateway 资源相同的 REST API。
有什么工具可以帮我自动转换吗?
【问题讨论】:
标签: amazon-web-services swagger terraform aws-api-gateway openapi
我的 REST API 有 OpenAPI 3.0 规范 (YAML),我需要在 Terraform 模板 (HCL) 中描述与 AWS API Gateway 资源相同的 REST API。
有什么工具可以帮我自动转换吗?
【问题讨论】:
标签: amazon-web-services swagger terraform aws-api-gateway openapi
AWS 的API Gateway directly supports importing an OpenAPI 3.0 spec 定义 API 网关的所有相关部分。
使用 Terraform,这可以通过 aws_api_gateway_rest_api resource 和 body parameter 来完成。
tests for the resource中给出了一个例子:
resource "aws_api_gateway_rest_api" "test" {
name = "foo"
body = <<EOF
{
"swagger": "2.0",
"info": {
"title": "foo",
"version": "2017-04-20T04:08:08Z"
},
"schemes": [
"https"
],
"paths": {
"/test": {
"get": {
"responses": {
"200": {
"description": "200 response"
}
},
"x-amazon-apigateway-integration": {
"type": "HTTP",
"uri": "https://www.google.de",
"httpMethod": "GET",
"responses": {
"default": {
"statusCode": 200
}
}
}
}
}
}
}
EOF
}
您还可以使用file 函数直接从您的 YAML 文件中加载 OpenAPI 规范:
resource "aws_api_gateway_rest_api" "test" {
name = "foo"
body = file("${path.module}/api.yml")
}
【讨论】:
body 参数可以巧妙地跳过这一点,同时仍然使用 Terraform(而不是 SAM/Serverless 等)。
也许试试 AWS 资源本身。
根据documentation for aws_api_gateway_rest_api,您可以传递body 参数
一种 OpenAPI 规范,定义了作为 REST API 的一部分创建的路由和集成集。
然后,如果您需要对 OpenAPI 规范进行一些修改,您可以使用您最喜欢的 JSON 编程语言(例如node、java、甚至jq 等...)。
如需更多信息并查看哪些扩展可用,请参阅API Gateway docs。
【讨论】: