2

I'm creating my infrastructure using CloudFormation.

I'm hoping to create the API using "API Gateway Extensions to OpenAPI"

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

Relevant code segment looks like this.

        "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" 
              } 
          } 
       }

I need to pass "DNSName" of the "MorningApiloadBalancer" to the swagger file located in the S3 location. I cannot find a way to do this. Any help is appreciated.

Harsha Jayamanna
  • 2,148
  • 6
  • 25
  • 43
  • Did you ever figure out how to do this? – neuquen Mar 14 '22 at 17:50
  • We couldn't do this. I integrated swagger ui with an angular application. I changed the DNSName during runtime. We maintained three swagger applications for dev, pre-prod and prod. Swagger files were maintained independently as a part of documentation. We didn't use API gateway swagger extension at the end. – Harsha Jayamanna Mar 15 '22 at 02:18

1 Answers1

1

I haven't tried using swagger with API gateway before.

Using Fn::Transform

Using the Swagger file stored in S3 using the Fn::Tranform macro, it basically takes the content of the swagger file and tranforms into 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}"
            }
          }
        }
      }
    }
  }
}

Inline swagger definition

I saw an example where the swagger definition is embedded into the cloudformation template. If you do so, you can use intrinsic functions inside the swagger definition. In your case, you can use Ref to get the dns name of the load balancer.

"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"
                  ]
                ]
              }
            }
          }
        }
      }
    }
  }
}

Getting DNS Name

I think you know this already.

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

Hope this helps.

Arun Kamalanathan
  • 8,107
  • 4
  • 23
  • 39