Ad

Preflight CORS Issue On Serverless AWS Lamba Fucntions.

I have tried as official documentation for CORS fix by serverless & different solutions, but the issue still persists.

What i have done so far is,

1, Set CORS true on all functions.

 events:
  - http:
      path: /api/v1/user/login
      method: post
      cors: true

2, Set authorizer fix for CORS.

 GatewayResponseDefault4XX:
  Type: 'AWS::ApiGateway::GatewayResponse'
  Properties:
    ResponseParameters:
      gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
      gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
      gatewayresponse.header.Access-Control-Allow-Methods: "'*'"
    ResponseType: DEFAULT_4XX
    RestApiId:
      Ref: 'ApiGatewayRestApi'

3, In response header, i have set as,

headers: {
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*',
                "Access-Control-Allow-Headers": '*',
                'Access-Control-Allow-Credentials': true,
            }

Still am getting issue based on CORS

Error message:

Access to XMLHttpRequest at ’https://xxxxxx.execute-api.us-east-1.amazonaws.com/dev/api/v1/user/login' from origin ‘http://localhost:4200’ has
been blocked by CORS policy: Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response.
Ad

Answer

I dont know if this is the best or right way to fix this. But this worked.

I changed default cors value into custom values.

events:
- http:
  path: /api/v1/user/login
  method: post
  cors: true

to this,

events:
  - http:
      path: /api/v1/user/login
      method: post
      cors:
        origin: '*'
        headers:
          - Content-Type
          - X-Amz-Date
          - Authorization
          - X-Api-Key
          - X-Amz-Security-Token
          - X-Amz-User-Agent
          - Access-Control-Allow-Origin 
          - Access-Control-Allow-Credentials
          - Access-Control-Allow-Methods
          - Access-Control-Allow-Headers
        allowCredentials: true
        cacheControl: 'max-age=600, s-maxage=600, proxy-revalidate'

and it worked!

Ad
source: stackoverflow.com
Ad