gpt4 book ai didi

amazon-web-services - 具有 OpenAPI swagger 资源定义的 terraform 中的 AWS API Gateway

转载 作者:行者123 更新时间:2023-12-04 02:43:17 25 4
gpt4 key购买 nike

我正在 AWS API Gateway 中创建 API。 AWS 中的所有基础设施都通过使用 terraform 进行管理。为了继续同样的操作,我想在 terraform 中添加 API 配置。我在应用程序中添加了由 swagger 依赖工具生成的 swagger 中的 API 资源定义。

我需要将它与 Terraform 集成,但是当我尝试应用时,我必须多次从 AWS 中导入由 swagger 创建的每个资源。只有 API 网关配置应该是 terraform,资源定义应该来自 swagger,有什么办法可以实现。此外,我需要自动化 100 个 API 的流程,请建议如何完成。

请分享任何相关的 github 链接

到目前为止,这是我尝试过的,

resource "aws_api_gateway_rest_api" "api" {
name = "Hello-API"
description = "Proxy to handle requests to our API"
body = "${file("api_swagger_example.json")}"
}


//Resource created by swagger
data "aws_api_gateway_resource" "helloApp" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
path = "/api/v1/hello"
}


//Need to import from first, since it was created using swagger
resource "aws_api_gateway_method" "helloApp-POST" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
resource_id = "${data.aws_api_gateway_resource.helloApp.id}"
http_method = "POST"
authorization = "NONE"
}

//Importing first
resource "aws_api_gateway_method_response" "response_200" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
resource_id = "${data.aws_api_gateway_resource.helloApp.id}"
http_method = "${aws_api_gateway_method.helloApp-POST.http_method}"
status_code = "200"
response_parameters = "${var.method_response_parameters}"
}

//Importing first
resource "aws_api_gateway_method_response" "response_401" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
resource_id = "${data.aws_api_gateway_resource.helloApp.id}"
http_method = "${aws_api_gateway_method.helloApp-POST.http_method}"
status_code = "401"
response_parameters = "${var.method_response_parameters}"
}

resource "aws_api_gateway_integration_response" "helloApp-ok" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
resource_id = "${data.aws_api_gateway_resource.helloApp.id}"
http_method = "${aws_api_gateway_method.helloApp-POST.http_method}"
status_code = "${aws_api_gateway_method_response.response_200.status_code}"
response_parameters = "${var.integration_response_parameters}"
}

resource "aws_api_gateway_integration_response" "helloApp-401" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
resource_id = "${data.aws_api_gateway_resource.helloApp.id}"
http_method = "${aws_api_gateway_method.helloApp-POST.http_method}"
status_code = "${aws_api_gateway_method_response.response_401.status_code}"
selection_pattern = "4\\d{2}"
response_parameters = "${var.integration_response_parameters}"
}



//Importing first
resource "aws_api_gateway_method_response" "helloApp_response_200" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
resource_id = "${data.aws_api_gateway_resource.helloApp.id}"
http_method = "${aws_api_gateway_method.helloApp-POST.http_method}"
status_code = "200"
response_parameters = "${var.method_response_parameters}"
}

//Importing first
resource "aws_api_gateway_method_response" "helloApp_response_401" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
resource_id = "${data.aws_api_gateway_resource.helloApp.id}"
http_method = "${aws_api_gateway_method.helloApp-POST.http_method}"
status_code = "401"
response_parameters = "${var.method_response_parameters}"
}


resource "aws_api_gateway_integration" "helloAppIntegration" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
resource_id = "${data.aws_api_gateway_resource.helloApp.id}"
http_method = "${aws_api_gateway_method.helloApp-POST.http_method}"
type = "HTTP"
integration_http_method = "POST"
connection_type = "VPC_LINK"
connection_id = "${data.aws_api_gateway_vpc_link.hello_vpc_link.id}"
uri = "${var.hello-endpoint-url}"
request_templates = {
"application/json" = <<REQUEST_TEMPLATE
$input.json('$')
REQUEST_TEMPLATE
}
}


resource "aws_api_gateway_integration_response" "helloApp-ok" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
resource_id = "${data.aws_api_gateway_resource.helloApp.id}"
http_method = "${aws_api_gateway_method.helloApp-POST.http_method}"
status_code = "${aws_api_gateway_method_response.helloApp_response_200.status_code}"
response_parameters = "${var.integration_response_parameters}"
}

resource "aws_api_gateway_integration_response" "helloApp-401" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
resource_id = "${data.aws_api_gateway_resource.helloApp.id}"
http_method = "${aws_api_gateway_method.helloApp-POST.http_method}"
status_code = "${aws_api_gateway_method_response.helloApp_response_401.status_code}"
selection_pattern = "4\\d{2}"
response_parameters = "${var.integration_response_parameters}"
}

resource "aws_api_gateway_deployment" "deploy-dev" {
depends_on = [
"aws_api_gateway_integration.helloAppIntegration",
"aws_api_gateway_method.helloApp-POST"
]
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
stage_name = "dev"
}

resource "aws_api_gateway_stage" "dev" {
stage_name = "dev"
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
deployment_id = "${aws_api_gateway_deployment.deploy-dev.id}"
}

resource "aws_api_gateway_usage_plan" "dev-usage-plan" {
name = "hello-usage-plan"
description = "hello API Basic Usage Plan"

api_stages {
api_id = "${aws_api_gateway_rest_api.api.id}"
stage = "${aws_api_gateway_deployment.deploy-dev.stage_name}"
}

throttle_settings {
burst_limit = 5
rate_limit = 10
}
}
resource "aws_api_gateway_method_settings" "helloApp-POST" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
stage_name = "${aws_api_gateway_stage.dev.stage_name}"
method_path = "${data.aws_api_gateway_resource.helloApp.path_part}/${aws_api_gateway_method.helloApp-POST.http_method}"

settings {
metrics_enabled = true
logging_level = "INFO"
}
}

为所有 API 和更新导入所有资源实在是太烦人了,而且几乎不可能。有更好的集成方式吗?

最佳答案

您需要使用 template_file 资源,它将通过读取源 swagger 文件为 AWS API 网关创建一个 swagger 文件模板。

然后通过将呈现的 swagger 文件作为正文传递来使用 aws_api_gateway_rest_api 资源。要创建集成,您需要将其添加到 swagger 文件本身。

 data "template_file" "aws_api_swagger" 
{
template = "${file(var.swagger-file-path)}"

#Pass the varible value if needed in swagger file
vars = {
connectionType = "${var.connectiontype}"
type = "${var.type}"
backend_uri = "https://api.endpoint.url"
}
}

resource "aws_api_gateway_rest_api" "api-gateway"
{
name = "${var.name}"
description = "${var.description}"
body = "${data.template_file.aws_api_swagger.rendered}"
}

要引用的 Swagger 文件片段

paths:
/:
get:
tags:
- sample
description: sample
responses:
"200":
description: Success
x-amazon-apigateway-integration:
uri: ${backend_url}/health-check
connectionType: ${connectionType}
passthroughBehavior: "when_no_match"
httpMethod: "GET"
type: ${type}

另请参阅 here在 swagger 文件中详细使用 x-amazon-apigateway-integration

关于amazon-web-services - 具有 OpenAPI swagger 资源定义的 terraform 中的 AWS API Gateway,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58290406/

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