首页 > 解决方案 > 带有 AWS 扩展的 Swagger 文件存储在 S3 存储桶中,用于使用 Cloudformation 创建 API

问题描述

我正在尝试使用这样的 Cloudformation 模板创建 API 网关:

Resources:
 InvoiceApi:
  Type: AWS::ApiGateway::RestApi
  Properties:
    Description: an Api for our Invoicegen App
    Name: !Ref ApiName
    ApiKeySourceType: !Ref ApiKeySourceType
    BinaryMediaTypes:
    - !Ref binaryMediaType1
    - !Ref binaryMediaType2
    BodyS3Location:
     Bucket:
       Fn::ImportValue: !Sub ${EnvironmentName}-SwaggerApiBucket-Name
     Key: swaggertest.yaml
     ETag: !Ref ETag
     EndpointConfiguration:
     Types:
      - REGIONAL
     FailOnWarnings: true
     MinimumCompressionSize: !Ref minimumCompressionSize

S3 Bucket 上的 Swagger-yaml 文件如下所示:

  swagger: '2.0'
  info:
    version: '2016-08-17T18:08:34Z'
    title: InvoicegenAPI
  basePath: "/LATEST"
  schemes:
   - https
  paths:
    /greeting:
       get:
         summary: Get Greeting
         parameters:
          - name: name
            in: query
            required: false
            type: string
        produces:
          - application/json
        responses:
          '200':
            description: 200 response
        x-amazon-apigateway-integration:
          requestTemplates:
            application/json: '{"name": "$input.params(''name'')"}'
          uri:
            Fn::Join:
             - ''
             - - 'arn:aws:apigateway:'
               - Ref: AWS::Region
               - ":lambda:path/2015-03-31/functions/"
               - Fn::GetAtt:
                 - InvoiceLambda
                 - Arn
               - "/invocations"
         responses:
           default:
             statusCode: '200'
         httpMethod: POST
         type: aws

不幸的是,它会引发如下错误:

Unable to parse API definition because of a malformed integration at path /greeting. (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: 2cf08a97-e66f-11e8-afee-fb6b03568b64)

我仔细检查了 Swagger 文件,所有缩进看起来都很好。我错过了什么?

已经有一个线程在处理这个问题,但还没有产生任何解决方案。

将 ARN 引用从 CloudFormation 传递给 Swagger

提前谢谢

一个

标签: swaggeramazon-cloudformation

解决方案


我认为你的问题是使用BodyS3Location引用的 S3 文件的属性,它可能没有解析 YAML 文件,因此没有解析你的 instricic 函数。

我的建议是您更改为 Body + AWS::Include Transform,类似于将ARN 参考从 CloudFormation 传递到 Swagger中的建议。试试这个作为你的资源:

Resources:
 InvoiceApi:
  Type: AWS::ApiGateway::RestApi
  Properties:
    Description: an Api for our Invoicegen App
    Name: !Ref ApiName
    ApiKeySourceType: !Ref ApiKeySourceType
    BinaryMediaTypes:
    - !Ref binaryMediaType1
    - !Ref binaryMediaType2
    Body:
      Fn::Transform:
        Name: AWS::Include
        Parameters:
          Location: !Sub 's3://${EnvironmentName}-SwaggerApiBucket-Name/swaggertest.yaml'
    EndpointConfiguration:
    Types:
    - REGIONAL
    FailOnWarnings: true
    MinimumCompressionSize: !Ref minimumCompressionSize

推荐阅读