【问题标题】:Passing values from CloudFormation to Swagger file将值从 CloudFormation 传递到 Swagger 文件
【发布时间】:2020-01-04 17:25:39
【问题描述】:

我正在使用 CloudFormation 创建我的基础架构。

我希望使用“API Gateway Extensions to OpenAPI”来创建 API

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions.html

相关代码段如下所示。

        "MorningApi": { 
            "Type": "AWS::ApiGateway::RestApi", 
            "Properties": { 
                "Description": { 
                    "Fn::Sub": "${Stage}-MorningApi" 
                }, 
                "Name": { 
                    "Fn::Sub": "${Stage}-MorningApi" 
                }, 
                "EndpointConfiguration": { 
                    "Types": [ 
                        "REGIONAL" 
                    ] 
                },
                "BodyS3Location": {
                    "Bucket" : "cf-morning",
                    "Key" : "nested-templates-stack/MorningApiStatusStackSwagger.json"
                } 
            } 
        },
        "MorningApiloadBalancer": { 
          "Type": "AWS::ElasticLoadBalancingV2::LoadBalancer", 
          "Properties": { 
              "Name": { 
                  "Fn::Sub": "${Stage}-MorningApiloadBalancer" 
              }, 
              "Subnets": [ 
                  { 
                      "Ref": "PrivateASubnet" 
                  }, 
                  { 
                      "Ref": "PrivateBSubnet" 
                  }, 
                  { 
                      "Ref": "PrivateCSubnet" 
                  } 
              ], 
              "Type": { 
                  "Ref": "ELBType" 
              }, 
              "Scheme": "internal", 
              "IpAddressType": { 
                  "Ref": "ELBIpAddressType" 
              } 
          } 
       }

我需要将“MorningApiloadBalancer”的“DNSName”传递给位于 S3 位置的 swagger 文件。我找不到办法做到这一点。 任何帮助表示赞赏。

【问题讨论】:

  • 你有没有想过如何做到这一点?
  • 我们无法做到这一点。我将 swagger ui 与 Angular 应用程序集成在一起。我在运行时更改了 DNSName。我们为 dev、pre-prod 和 prod 维护了三个 swagger 应用程序。 Swagger 文件作为文档的一部分独立维护。最后我们没有使用 API 网关 swagger 扩展。

标签: amazon-web-services swagger amazon-cloudformation openapi


【解决方案1】:

我之前没有尝试过将 swagger 与 API 网关一起使用。

使用Fn::Transform

使用Fn::Tranform宏存储在S3中的Swagger文件,它基本上是把swagger文件的内容转换成cloudformation。

{
  "APIGateway": {
    "Type": "AWS::ApiGateway::RestApi",
    "Name": "myapi",
    "Properties": {
      "Body": {
        "Fn::Transform": {
          "Name": "AWS::Include",
          "Parameters": {
            "Location": {
              "Fn::Sub": "s3://${AWS::AccountId}-swagger-files/${SwaggerFile}"
            }
          }
        }
      }
    }
  }
}

内联招摇定义

我看到了一个示例,其中将 swagger 定义嵌入到 cloudformation 模板中。如果这样做,您可以在 swagger 定义中使用内部函数。在您的情况下,您可以使用 Ref 获取负载均衡器的 dns 名称。

"GreetingApi": {
  "Type": "AWS::ApiGateway::RestApi",
  "Properties": {
    "Name": "Greeting API",
    "Description": "API used for Greeting requests",
    "FailOnWarnings": true,
    "Body": {
      "swagger": "2.0",

      "paths": {
        "/greeting": {
          "get": {
            "x-amazon-apigateway-integration": {
              "uri": {
                "Fn::Join": [
                  "",
                  [
                    "arn:aws:apigateway:",
                    {
                      "Ref": "AWS::Region"
                    },
                    ":lambda:path/2015-03-31/functions/",
                    {
                      "Fn::GetAtt": [
                        "GreetingLambda",
                        "Arn"
                      ]
                    },
                    "/invocations"
                  ]
                ]
              }
            }
          }
        }
      }
    }
  }
}

获取 DNS 名称

我想你已经知道了。

"Fn::GetAtt" : [ "MorningApiloadBalancer" , "DNSName" ]

希望这会有所帮助。

【讨论】:

  • 这是您要找的吗?
  • 我想单独保留我的招摇规范并传递“DNSName”。感谢您提供的信息,但这并没有达到我的预期。
  • 我已经提到了这两个选项。您是否尝试过转换方法。不行吗?使用 transform 方法,您可以将 swagger 很好地分开
猜你喜欢
  • 2017-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多