Skip to content

Add headers to 4xx and 5xx when enabling CORS #1178

@honglu

Description

@honglu

Description:

When using SAM to add CORS support to API GW, it works well for 2xx errors. According to API GW doc, the Lambda function is responsible to return the Access-Control-Allow-Origin header. However, in the case when API GW returns 4xx (access denied or input validation failure etc.) or 5xx directly without going through the integrated Lambda function, this header is missing. In order to make this work for 4xx and 5xx, I have to add the following in my swagger:

x-amazon-apigateway-gateway-responses:
  DEFAULT_4XX:
    responseParameters:
      gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
  DEFAULT_5XX:
    responseParameters:
      gatewayresponse.header.Access-Control-Allow-Origin: "'*'"

It would be nice if SAM can add these to the swagger if I add cors in AWS::Serverless::Api resource.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions