带有 Swagger 的 CloudFormation API 给出“指定的映射表达式参数无效:method.response.header.Access-Control-Allow-Headers”

问题描述 投票:0回答:1

我尝试向我的 Api 添加选项端点来解决 CORS 问题。然而,下面的CloudFormation代码给出了一个非常奇怪的错误消息:

LogUserActivityApi:
Type: AWS::Serverless::Api
DependsOn: LogUserActivityFunction
Properties:
  Name: !Join [ "", [ "log-user-activity-", !Ref environment ]]
  Description: General api to log user activity
  StageName: !Join [ "", [ !Ref environment, "stage"]]
  Auth:
    DefaultAuthorizer: MyCognitoAuthorizer
    Authorizers:
      MyCognitoAuthorizer:
        UserPoolArn: !Ref CognitoUserPoolArn
  DefinitionBody:
    swagger: 2.0
    info:
      version: "1.0"
      title: "log-user-activity"
    basePath: !Join [ "", [ "/", !Ref environment, "stage" ]]
    schemes:
    - "https"
    paths:
      /:
        get:
          responses: {}
          x-amazon-apigateway-integration:
            uri:
              # You need to build up the ARN with this pattern - you can't just use a !Ref or !GetAtt.
              Fn::Sub: arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LogUserActivityFunction.Arn}/invocations
            passthroughBehavior: "when_no_match"
            httpMethod: "POST"
            type: "aws_proxy"
      /log-user-activity:
        post:
          consumes:
            - application/json
          produces:
            - application/json
          responses: {}
          x-amazon-apigateway-integration:
            uri:
              # You need to build up the ARN with this pattern - you can't just use a !Ref or !GetAtt AFAIK.
              Fn::Sub: arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LogUserActivityFunction.Arn}/invocations
            passthroughBehavior: "when_no_match"
            httpMethod: "POST"
            type: "aws_proxy"
        options:
          consumes:
            - application/json
          produces:
            - application/json
          responses:
            '200':
              description: Default response
              headers:
                Access-Control-Allow-Headers:
                  schema:
                    type: string
                Access-Control-Allow-Methods:
                  schema:
                    type: string
                Access-Control-Allow-Origin:
                  schema:
                    type: string
          x-amazon-apigateway-integration:
            type: mock
            requestTemplates:
              application/json: |
                {"statusCode" : 200}
            responses:
              default:
                statusCode: '200'
                responseParameters:
                  method.response.header.Access-Control-Allow-Headers: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"
                  method.response.header.Access-Control-Allow-Methods: "'OPTIONS,POST,GET,PUT,DELETE,PATCH'"
                  method.response.header.Access-Control-Allow-Origin: "'*'"

我收到错误:

Resource handler returned message: "Errors found during import: Unable to put integration response on 'OPTIONS' for resource at path '/log-user-activity': Invalid mapping expression specified: Validation Result: warnings : [], errors : [Invalid mapping expression parameter specified: method.response.header.Access-Control-Allow-Headers] 

您能发现任何错误吗?或者是否有更简单的方法在 CloudFormation 中为我的 Api 启用 CORS?

我已经尝试使用小写字母作为标头名称,并仅传递“'*'”,甚至尝试跳过一些,在这种情况下它无法解决 CORS 问题。

aws-lambda cors swagger aws-cloudformation aws-api-gateway
1个回答
0
投票

看起来您正在使用 OpenAPI 3 版本的定义选项,然后还指定 swagger 2。我会切换到

openapi: "3.0.1"
并删除
swagger: 2.0
或切换到添加选项的 2 方法。您可以在此处查看docs,了解如何使用 2.x 版本执行选项。

© www.soinside.com 2019 - 2024. All rights reserved.