AWS API 网关响应映射

AWS APIgateway Response mapping

我有一个 API 网关集成,其中数据直接进入 dynamodbx-amazon-apigateway-integration 定义了响应代码映射,所以我的目的是在我的客户端获取从 dynamodb 抛出的类似响应代码。

我的apigateway-integration响应码映射如下图

      x-amazon-apigateway-integration:
        type: aws
        uri: !Sub arn:aws:apigateway:${AWS::Region}:dynamodb:action/PutItem
        credentials: !GetAtt ApiGatewayDynamoRole.Arn
        httpMethod: POST
        passthroughBehavior: when_no_match
        responses:
          "default":
            statusCode: "200"
            SelectionPattern: "2\d{2}"
            responseParameters:
              method.response.header.Access-Control-Allow-Methods: "'OPTIONS,POST'"
              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-Origin: "'*'"
          "BAD.*":
            statusCode: "400"
            SelectionPattern: "4\d{2}"
            responseParameters:
              method.response.header.Access-Control-Allow-Methods: "'OPTIONS,POST'"
              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-Origin: "'*'"
          "INT.*":
            statusCode: "500"
            SelectionPattern: "5\d{2}"
            responseParameters:
              method.response.header.Access-Control-Allow-Methods: "'OPTIONS,POST'"
              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-Origin: "'*'"
      responses:
        "200":
          description: The unique identifier of the new feedback
          headers:
            Access-Control-Allow-Origin:
              type: "string"
            Access-Control-Allow-Methods:
              type: string
            Access-Control-Allow-Headers:
              type: string
          schema:
            $ref: "#/definitions/NewFeedbackResponse"
        "400":
          description: Bad request
          headers:
            Access-Control-Allow-Origin:
              type: "string"
            Access-Control-Allow-Methods:
              type: string
            Access-Control-Allow-Headers:
              type: string
          schema:
            $ref: "#/definitions/Error"
        "500":
          description: Internal error
          headers:
            Access-Control-Allow-Origin:
              type: "string"
            Access-Control-Allow-Methods:
              type: string
            Access-Control-Allow-Headers:
              type: string
          schema:
            $ref: "#/definitions/Error"

但这不起作用,如下所示,400 响应映射到 200。知道我错过了什么吗?

Mon Mar 22 16:33:25 UTC 2021 : Sending request to https://dynamodb.us-west-2.amazonaws.com/?Action=PutItem
Mon Mar 22 16:33:25 UTC 2021 : Received response. Status: 400, Integration latency: 13 ms
Mon Mar 22 16:33:25 UTC 2021 : Endpoint response headers: {Server=Server, Date=Mon, 22 Mar 2021 16:33:25 GMT, Content-Type=application/x-amz-json-1.0, Content-Length=310, Connection=keep-alive, x-amzn-RequestId=XXXXXXXXXXXGBB7126RVV4KQNSO5AXXXXXXXXXX, x-amz-crc32=418088284}
Mon Mar 22 16:33:25 UTC 2021 : Endpoint response body before transformations: {"__type":"com.amazon.coral.service#AccessDeniedException","Message":"User: arn:aws:sts::XXXXXXX:assumed-role/dynamoapi-ApiGatewayDynamoRole-BBBFKOGK2NP5/BackplaneAssumeRoleSession is not authorized to perform: dynamodb:PutItem on resource: arn:aws:dynamodb:us-west-2:XXXXXXX:table/feedback_events2"}
Mon Mar 22 16:33:25 UTC 2021 : Method response body after transformations: {"__type":"com.amazon.coral.service#AccessDeniedException","Message":"User: arn:aws:sts::3XXXXXXX:assumed-role/dynamoapi-ApiGatewayDynamoRole-BBBFKOGK2NP5/BackplaneAssumeRoleSession is not authorized to perform: dynamodb:PutItem on resource: arn:aws:dynamodb:us-west-2:380672241378:table/feedback_events2"}
Mon Mar 22 16:33:25 UTC 2021 : Method response headers: {X-Amzn-Trace-Id=Root=1-6058c6d5-02f9e3bf2c1da22f6c7fdab6, Access-Control-Allow-Headers=Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token, Access-Control-Allow-Methods=OPTIONS,POST, Access-Control-Allow-Origin=*, Content-Type=application/json}
Mon Mar 22 16:33:25 UTC 2021 : Successfully completed execution
Mon Mar 22 16:33:25 UTC 2021 : Method completed with status: 200

如果有人有同样的疑问,对我有用的不是 SelectionPattern,而是 Response status pattern 属性,更多详细信息 here

  x-amazon-apigateway-integration:
    type: aws
    uri: !Sub arn:aws:apigateway:${AWS::Region}:dynamodb:action/PutItem
    credentials: !GetAtt ApiGatewayDynamoRole.Arn
    httpMethod: POST
    passthroughBehavior: when_no_match
    responses:
      "2\d{2}":
        statusCode: "200"
        SelectionPattern: "2\d{2}"
        responseParameters:
          method.response.header.Access-Control-Allow-Methods: "'OPTIONS,POST'"
          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-Origin: "'*'"
      "4\d{2}":
        statusCode: "400"
        SelectionPattern: "4\d{2}"
        responseParameters:
          method.response.header.Access-Control-Allow-Methods: "'OPTIONS,POST'"
          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-Origin: "'*'"
      "5\d{2}":
        statusCode: "500"
        SelectionPattern: "5\d{2}"
        responseParameters:
          method.response.header.Access-Control-Allow-Methods: "'OPTIONS,POST'"
          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-Origin: "'*'"