I am currently struggling with my CORS configuration in AWS.
My setup is as follows:
i have one proxy-lambda function for which i activated the function URL. In the lambda there i’m spinning up a nestjs application with enableCors()
set.
But as the function-URL is not really nice, i created a cloudfront distribution with my lambda as an origin and added a CNAME entry to it.
So far things are working fine and i can call my lambda function via the url i defined as a CNAME.
But now i realized, that all requests coming in are ALWAYS performing a preflight (OPTIONS) request beforehand… This means the double amount of requests coming in and so a noticeable higher bill.
Now i’m trying to cache the preflight response, but this is where i am stuck.
In the behaviour of the cloudfront distribution i already tried several combinations of cache-policies and response-header-policies…
But always failing. Either i then get the error that “Access-Control-Allow-Origin” is not present. Or if i activate CORS in the lambda-function via the console, it states there are multiple values present for “Access-Control-Allow-Origin” (“*, *”). Here i guess this is because my nestjs application already sets that header.
In the end the only thing i want to achieve, is to have the preflight-requests being cached so that the lambda-executions are not doubled.
Can anyone help here?