Sub topic 11
Enabling CORS for browser clients
This chapter assumes you can deploy the SAM template from the previous chapters and that you know what an HTTP origin is (scheme, host and port).
Why browsers need CORS
A browser blocks JavaScript on https://example.com from reading a response from your API's domain unless the API opts in. This rule is called cross-origin resource sharing (CORS). The API opts in with Access-Control-Allow-* response headers. curl ignores CORS, so an API that works in curl can still fail in a browser.
For some requests, such as a POST with a content-type: application/json header or any request with an Authorization header, the browser first sends a preflight. A preflight is an OPTIONS request that asks which origins, methods and headers the API allows. The browser sends the real request only if the answer permits it.
Configuring CORS on the API
An HTTP API has built-in CORS support. You set it on the API resource instead of writing code in your handler:
ItemsApi:
Type: AWS::Serverless::HttpApi
Properties:
CorsConfiguration:
AllowOrigins:
- https://example.com
AllowMethods:
- GET
- POST
AllowHeaders:
- content-type
MaxAge: 600Once CORS is configured, API Gateway does two things for you:
- It answers preflight
OPTIONSrequests itself. You do not need anOPTIONSroute, and the request never invokes Lambda. - It adds the configured CORS headers to responses from your integration. It ignores any CORS headers your function returns, so setting them in the handler has no effect.
Testing the preflight
Send the request a browser would send and check the response headers:
API_URL="https://abc123.execute-api.us-east-1.amazonaws.com"
curl -i -X OPTIONS "$API_URL/items" \
-H 'Origin: https://example.com' \
-H 'Access-Control-Request-Method: POST' \
-H 'Access-Control-Request-Headers: content-type'A successful reply is a 204 with access-control-allow-origin: https://example.com and matching allow-methods and allow-headers values. Repeat with Origin: https://other.example and the allow-origin header should be missing.
Common mistakes
| Symptom in the browser console | Likely cause |
|---|---|
| No `Access-Control-Allow-Origin` header | The origin is not in `AllowOrigins`, or it differs in scheme, port or a trailing detail |
| Preflight fails on a header | A header such as `authorization` is missing from `AllowHeaders` |
| Preflight fails on a method | The method, such as `PUT` or `DELETE`, is missing from `AllowMethods` |
| Cookies are not sent or accepted | `AllowCredentials` is not `true`, or `AllowOrigins` is `*` |
Exercise
Exercise
Your React app at https://app.example.com calls POST /items with an Authorization header, and the browser reports a failed preflight. The API's CorsConfiguration allows origin https://app.example.com, methods GET and POST, and header content-type. What do you change, and why does adding CORS headers in the Lambda handler not fix it?
Show answer
Add authorization to AllowHeaders, then redeploy. The preflight lists authorization in Access-Control-Request-Headers, and API Gateway rejects it because the header is not allowed. API Gateway answers the preflight itself without invoking Lambda. Once CORS is configured, it also ignores CORS headers that the function returns.