如何在swagger.yaml / openapi.yaml文件中访问AWS SAM映射

问题描述

我在StageMap文件中声明了一个名为sam.yaml的映射:


AWstemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31

Parameters:
  ProjectName:
    Type: String
  SubProjectName:
    Type: String
  Stage:
    Type: String
    AllowedValues:
      - dev
      - test
      - preprod
      - prod
...

Mappings:
  StageMap:
    dev:
      AuthorizerArn: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:auth-bk-main-dev-AuthorizerFunction-1RR2YJ5STBUB6/invocations
    test:
      AuthorizerArn: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:auth-bk-main-test-AuthorizerFunction-UQ1EQ2SP5W6G/invocations
    preprod:
      AuthorizerArn: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:auth-bk-main-preprod-AuthorizerFunction-UQ1W6EQ2SP5G/invocations
    prod:
      AuthorizerArn: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:auth-bk-main-prod-AuthorizerFunction-5STBUB61RR2YJ/invocations

我想在我的swagger.yaml中尝试使用以下映射:

...
x-amazon-apigateway-authorizer:
  type: request
  authorizerUri:
    Fn::FindInMap:
      - 'StageMap'
      - Ref: 'Stage'
      - 'AuthorizerArn

我也尝试过this solution,但遇到错误Every Mappings attribute must be a String or a List

能否让我知道如何访问swagger.yaml中映射中的值之一?谢谢!

解决方法

我在AWS SAM docs中发现了以下内容:

您不能在“映射”部分中包含参数,伪参数或内部函数。

所以我改变了:

AuthorizerArn: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:auth-bk-main-dev-AuthorizerFunction-1RR2YJ5STBUB6/invocations

针对:

AuthorizerFunctionName: auth-bk-main-dev-AuthorizerFunction-1RR2YJ5STBUB6

swagger.yaml中,我使用了以下内容:

x-amazon-apigateway-authorizer:
        type: request
        authorizerUri:
          Fn::Sub:
            - arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${AuthorizerFunctionName}/invocations
            - AuthorizerFunctionName:
                Fn::FindInMap:
                  - 'StageMap'
                  - Ref: 'Stage'
                  - 'AuthorizerFunctionName'