I am new to serverless services, mainly the aws concept. I am trying to deploy my Single Page Application (SPA) with AWS S3 Bucket and CloudFront. Every static files are served properly, but inside SPA, there are some axios.POST(...) out of origin page to my API. The Api is correctly configured, and accepts requests from whoever (tested).
Now when I try:
curl -H "origin: ORIGIN_DOMAIN" -H "x-api-key: API_KEY" -v "API_URL"
The response is as predicted (200 OK with data)
But when I try to load my SPA, the browser says:
Access to XMLHttpRequest at 'https://47zb0mpexk.execute-api.eu-south-1.amazonaws.com/default/autostatistika' from origin 'http://d1b89hgibifv2r.cloudfront.net' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
My S3 bucket CORS policy:
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>HEAD</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
I found some stackoverflow questions, like this: Correct S3 + Cloudfront CORS Configuration? And did what exactly is in answer (and in aws documentation): https://stackoverflow.com/a/52383335/9156446
Please, Can someone help me how to figure it out? Thanks.
UPDATE: I've forgot to try SPA on local machine with settings from remote API and my app didn't work, even on local machine. After few hours I found, that my API is not responding with headers 'Access-Control-Allow-Origin' : '*' only within browser with axios.POST(). So the problem were in API, because it didn't properly responding with header 'Access-Control-Allow-Origin' (AWS Lambda + API Gateway) even if cURL command works. I solved it by hard coding header within response in AWS Lambda. Thx to @vaquar khan, for your time.