bref icon indicating copy to clipboard operation
bref copied to clipboard

BREF_BINARY_RESPONSES conflict with Api gateway CORS handler

Open christophe-mailfert opened this issue 4 years ago • 1 comments

Description:

Our API can return JSON, HTML or PDF content. We follow the documentation and make BREF_BINARY_RESPONSES to 1 and apiGateway configuration to / We don't want our lambda handle OPTION request but let Api Gateway manage it.

With this configuration we got this error on cloudwatch api gateway:

Method request body before transformations: [Binary Data]
Execution failed due to configuration error: Unable to transform request
Gateway response type: DEFAULT_5XX with status code: 500

I think we should be able to define a list of content type who will be return as binary to not be able to set /

Capture d’écran 2021-05-04 à 11 26 32

How to reproduce:

Framework : Symfony

Serverless configuration file:

provider:
    name: aws
    region: eu-west-3
    runtime: provided.al2
    stage: ${opt:stage, 'dev'}
    apiGateway:
        binaryMediaTypes:
            - '*/*'
    environment:
        APP_ENV: prod

plugins:
    - ./vendor/bref/bref

functions:
    api:
        handler: public/index.php
        description: 'Api'
        timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
        memorySize: 512
        layers:
            - 'arn:aws:lambda:eu-west-3:209497400698:layer:php-80-fpm:7'
        events:
          - http: 'ANY /'
          - http: 'ANY /{proxy+}'
        environment:
            BREF_BINARY_RESPONSES: 1

christophe-mailfert avatar May 04 '21 09:05 christophe-mailfert

@christophe-mailfert have you tried setting CORS options for your events manually in your serverless.yml?

Something like this:

provider:
    name: aws
    region: eu-west-3
    runtime: provided.al2
    stage: ${opt:stage, 'dev'}
    apiGateway:
        binaryMediaTypes:
            - '*/*'
    environment:
        APP_ENV: prod

plugins:
    - ./vendor/bref/bref

functions:
    api:
        handler: public/index.php
        description: 'Api'
        timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
        memorySize: 512
        layers:
            - 'arn:aws:lambda:eu-west-3:209497400698:layer:php-80-fpm:7'
        events:
          - http:
          	path: /
          	method: ANY
          	cors: true # enable CORS
          - http:
          	path: /{proxy+}
          	method: ANY
          	cors: true # enable CORS
        environment:
            BREF_BINARY_RESPONSES: 1

The serverless docs also show how to enable multiple predefined origins here: https://www.serverless.com/framework/docs/providers/aws/events/apigateway/#enabling-cors

Also you are trying to set binary responses to every mime/media type which is possibly not what you want if you return multiple types like JSON and HTML as they're not binaries.

Did you try specifying PDF in your binaryMediaTypes like:

    apiGateway:
        binaryMediaTypes:
            - 'application/pdf'

Please keep in your mind that you need to set the correct Content-Type for your response in symfony to make this work.

tomhatzer avatar May 30 '21 14:05 tomhatzer

Closing as the comment above seems to provide a solution.

mnapoli avatar Mar 28 '23 12:03 mnapoli