amazon-web-services - AWS APIgateway 响应映射

标签 amazon-web-services rest amazon-dynamodb aws-cloudformation aws-api-gateway

我有一个 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: "'*'"

关于amazon-web-services - AWS APIgateway 响应映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66750646/

相关文章:

amazon-web-services - 登录成功后 MediaWiki 不会重定向到页面

java - NetBeans 中出现认证错误,即使 ".CER"文件已添加到 KeyStore

amazon-web-services - DynamoDB - 如何通过非主键的内容进行查询

java - 创建一种方法来接收 zip 文件作为 Rest Api 的输入

eclipse - tomcat 上的 java web 服务 (jersey) 仅在从 eclipse 运行时才有效

python - 如何在Python中生成按时间排序的uid?

python - 本地主机端点到 DynamoDB 本地与 Boto3

python - 我的下一个 Amazon Web Services (Linux) 项目应该选择什么? Ruby/Python/其他、MySQL/PostgreSQL/其他

amazon-web-services - AWS 跨账户共享 AMI 标签未显示

amazon-web-services - 是否有适用于 AWS Personalize 的 Terraform 或完成该任务的方法?