API GW OPTIONS pre-flight request removes my CORS

2020-05-09 18:49发布

问题:

I am in contact with AWS Support but they have not solved my issue as of yet.

My setup is AWS API GW v2 -> AWS Lambda (nodejs12 express)

I can act on my API using Postman, I get the correct headers and responses from every call with the methods GET,PUT,POST,DELETE, except my OPTIONS call.

The options call returns this:

My code has a really simple log output in the beginning (during debug purposes):

exports.handler = (event, context) => {
  console.log('httpMethod', event.httpMethod)
  return awsServerlessExpress.proxy(server, event, context, 'PROMISE').promise.then(data => {
    console.log('returning data \n', data)
    return data
  })
}

I am invoking my lambda service locally using this:

sam local invoke "Test" -e event.json

This is the terminal output:

Invoking index.handler (nodejs10.x)

Fetching lambci/lambda:nodejs10.x Docker container image......
Mounting /Users/simon/Git/server-service as /var/task:ro,delegated inside runtime container
START RequestId: 5c30f5a3-8006-1ba2-6e99-0c96ba3e7c76 Version: $LATEST
2020-04-14T18:00:24.398Z    5c30f5a3-8006-1ba2-6e99-0c96ba3e7c76    INFO    httpMethod OPTIONS
2020-04-14T18:00:24.421Z    5c30f5a3-8006-1ba2-6e99-0c96ba3e7c76    INFO    returning data
 { statusCode: 200,
  body: 'PUT,POST,GET,HEAD,DELETE',
  headers:
   { 'x-powered-by': 'Express',
     'access-control-allow-origin': '*',
     'access-control-allow-credentials': 'false',
     'access-control-allow-methods': '*',
     'access-control-allow-headers':
      'Content-Type, Authorization, cache-control, X-Frame-Options, If-Modified-Since',
     allow: 'PUT,POST,GET,HEAD,DELETE',
     'content-type': 'text/html; charset=utf-8',
     'content-length': '24',
     etag: 'W/"18-NOIbTUNpYRs55uZrUoLiPa8oj0c"',
     date: 'Tue, 14 Apr 2020 18:00:24 GMT',
     connection: 'close' },
  isBase64Encoded: false }

Therefor I assume that my API GW integration is removing these headers.

This is my API GW Resource:

ApiGatewayV2:
    Type: AWS::ApiGatewayV2::Api
    Properties:
      Name: Lambda Proxy
      Description: "Lambda Proxy Manual API"
      ProtocolType: HTTP
      CorsConfiguration:
        AllowCredentials: false
        AllowHeaders:
          - "Authorization"
          - "*"
        AllowMethods:
          - "*"
        AllowOrigins:
          - "*"
        ExposeHeaders:
          - "*"
        MaxAge: 300

My client is showing this error:

EDIT:

I read on multiple places that if I configure CORS on my AWS resource, it will respond with these without going to the lambda, however it still hits the lambda?

I also removed the CORS config from my API GW resource:

ApiGatewayV2:
    Type: AWS::ApiGatewayV2::Api
    Properties:
      Name: Lambda Proxy
      Description: "Lambda Proxy Manual API"
      ProtocolType: HTTP

In hope that my lambda should deliver the headers instead. To no luck.