具有 OpenAPI swagger 资源定义的 terraform 中的 AWS API Gateway

Ali*_*Ali 3 api amazon-web-services swagger terraform aws-api-gateway

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

我需要将它与 terraform 集成,但是当我尝试应用时,我必须多次从 swagger 创建的 AWS 中导入每个资源。只有 API Gateway 配置应该是 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"
  }
}
Run Code Online (Sandbox Code Playgroud)

为所有 API 和更新导入所有资源非常烦人,几乎不可能。有没有更好的整合方式?

Mah*_*tam 5

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

然后只需aws_api_gateway_rest_api通过将渲染的 swagger 文件作为正文传递来使用资源。要创建集成,您需要将其添加到 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}"
}
Run Code Online (Sandbox Code Playgroud)

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}
Run Code Online (Sandbox Code Playgroud)

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