带有OpenAPI挥杆资源定义的Terraform中的AWS API网关

问题描述 投票:0回答:1

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

我需要将其与terraform集成,但是当我尝试应用时,必须从swagger多次创建的AWS导入每个资源。仅API网关配置应采用terraform,资源定义应自如而来。是否有任何方法可以实现。另外,我需要自动执行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导入每个资源并进行更新。还有更好的整合方式吗?

amazon-web-services api swagger terraform aws-api-gateway
1个回答
0
投票

您需要使用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在详细文件中使用x-amazon-apigateway-integration

© www.soinside.com 2019 - 2024. All rights reserved.