Created
July 3, 2024 10:00
-
-
Save suzukieng/09a4038b32f993dcd9991e0c0b1ddae9 to your computer and use it in GitHub Desktop.
CORS Preflight Autoresponder CloudFront Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* CloudFront function for responding to CORS Preflight requests at the edge. | |
* | |
* See: https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request | |
*/ | |
async function handler(event) { | |
const request = event.request; | |
if (request.method === 'OPTIONS' && request.headers.origin) { | |
// see: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html#functions-event-structure-response | |
const response = { | |
statusCode: 200, | |
statusDescription: 'OK', | |
headers: { | |
// header names must be lower-case in CF functions | |
'access-control-allow-origin': {'value': request.headers.origin.value}, | |
'access-control-allow-methods': {'value': 'DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT'}, | |
'access-control-allow-headers': {'value': '*'}, | |
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#the_http_response_headers (Vary should be included) | |
'vary': {'value': 'Origin'} | |
} | |
}; | |
return response; | |
} | |
return request; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment