Search code examples
amazon-web-servicescorsaws-lambdaaws-api-gateway

Configure CORS response headers on AWS Lambda?


I'm trying to create a new service using AWS API Gateway, but I found out the browser automatically calls OPTIONS method in order to obtain CORS information.

The problem is that AWS API Gateway does not offer a native way to configure CORS headers.

Is it possible to create a Lambda Script in order to respond to OPTIONS method?


Solution

  • If you're using {proxy+} endpoint, you must handle CORS HTTP requests in the Lambda function. The implementation depends on the framework you're using. For Express, the easiest solution is to simply use Express CORS middleware.

    If you don't want to handle CORS requests by Lambda, try changing the settings of your Lambda Method to handle CORS on the API Gateway level.

    Here's a detailed official tutorial for CORS setup on AWS API Gateway.

    It's also critical that you allow header X-Api-Key in Access-Control-Allow-Headers otherwise auth won't work and you'll get errors.

    EDIT: In November 2015 the API Gateway team added a new feature to simplify CORS setup.