带有OpenAPI swagger资源定义的Terraform中的AWS API Gateway

我正在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导入每个资源并进行更新。有更好的整合方式吗?

摩atta摩

您需要使用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}"
}

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}

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

带有AWS WAF的AWS API Gateway

来自分类Dev

带有EC2 Api后端的AWS API Gateway

来自分类Dev

AWS API Gateway,带有CDK的默认基本映射

来自分类Dev

了解带有HTTP代理的AWS API Gateway

来自分类Dev

带有动态URL路径参数的AWS API Gateway

来自分类Dev

未创建在OpenAPI中为AWS API Gateway定义的参数

来自分类Dev

带有Python-EVE“位置”的AWS API Gateway在已部署的API中不起作用

来自分类Dev

不能在带有CORS的AWS API Gateway上使用自定义请求标头

来自分类Dev

AWS API Gateway:传递所有参数

来自分类Dev

AWS API Gateway访问另一个帐户中的私有API Gateway

来自分类Dev

从aws_api_gateway_rest_api迁移到aws_apigatewayv2_api松散的OpenAPI集成

来自分类Dev

带有Lambda HTTP GET请求的AWS API Gateway(Node.js)502错误的网关

来自分类Dev

从AWS API Gateway导出API定义

来自分类Dev

如何在AWS API Gateway中创建父资源?

来自分类Dev

具有外部身份验证的AWS API Gateway

来自分类Dev

带有Zuul的Spring Boot 2.4.2 Gateway API

来自分类Dev

AWS API GATEWAY - 使用 Swagger 模板导入和导出 API

来自分类Dev

从AWS API Gateway返回HTML

来自分类Dev

AWS Api Gateway与Dynamo的集成

来自分类Dev

AWS API Gateway HTTP API参数与Terraform的映射

来自分类Dev

捕获AWS Api Gateway中的超时错误

来自分类Dev

捕获AWS Api Gateway中的超时错误

来自分类Dev

使用AWS Api Gateway进行Api合成

来自分类Dev

消耗AWS API Gateway API的问题

来自分类Dev

AWS API Gateway专用API自定义域名

来自分类Dev

AWS API Gateway REST API是否没有设置来禁用CloudFormation模板中的execute-api终端节点?

来自分类Dev

创建aws_api_gateway_account资源将返回AccessDeniedException

来自分类Dev

Terraform添加aws_api_gateway_integration响应json

来自分类Dev

如何在AWS CDK中的API Gateway部署中使用现有阶段?

Related 相关文章

  1. 1

    带有AWS WAF的AWS API Gateway

  2. 2

    带有EC2 Api后端的AWS API Gateway

  3. 3

    AWS API Gateway,带有CDK的默认基本映射

  4. 4

    了解带有HTTP代理的AWS API Gateway

  5. 5

    带有动态URL路径参数的AWS API Gateway

  6. 6

    未创建在OpenAPI中为AWS API Gateway定义的参数

  7. 7

    带有Python-EVE“位置”的AWS API Gateway在已部署的API中不起作用

  8. 8

    不能在带有CORS的AWS API Gateway上使用自定义请求标头

  9. 9

    AWS API Gateway:传递所有参数

  10. 10

    AWS API Gateway访问另一个帐户中的私有API Gateway

  11. 11

    从aws_api_gateway_rest_api迁移到aws_apigatewayv2_api松散的OpenAPI集成

  12. 12

    带有Lambda HTTP GET请求的AWS API Gateway(Node.js)502错误的网关

  13. 13

    从AWS API Gateway导出API定义

  14. 14

    如何在AWS API Gateway中创建父资源?

  15. 15

    具有外部身份验证的AWS API Gateway

  16. 16

    带有Zuul的Spring Boot 2.4.2 Gateway API

  17. 17

    AWS API GATEWAY - 使用 Swagger 模板导入和导出 API

  18. 18

    从AWS API Gateway返回HTML

  19. 19

    AWS Api Gateway与Dynamo的集成

  20. 20

    AWS API Gateway HTTP API参数与Terraform的映射

  21. 21

    捕获AWS Api Gateway中的超时错误

  22. 22

    捕获AWS Api Gateway中的超时错误

  23. 23

    使用AWS Api Gateway进行Api合成

  24. 24

    消耗AWS API Gateway API的问题

  25. 25

    AWS API Gateway专用API自定义域名

  26. 26

    AWS API Gateway REST API是否没有设置来禁用CloudFormation模板中的execute-api终端节点?

  27. 27

    创建aws_api_gateway_account资源将返回AccessDeniedException

  28. 28

    Terraform添加aws_api_gateway_integration响应json

  29. 29

    如何在AWS CDK中的API Gateway部署中使用现有阶段?

热门标签

归档