Hello,
I am creating an API and deploying to lambda using Serverless. Has worked great before. Now my API is allowed the POST but all GET requests are failing with CORS error message:
Access to XMLHttpRequest at 'https://za66fuaffd.execute-api.us-east-1.amazonaws.com/dev/projects' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Serverless config:
ProjectsCreate:
handler: function/ProjectsCreate.handler
events:
- http:
path: projects
method: post
cors:
origin: ${self:custom.cors.origin}
headers: ${self:custom.headers}
environment:
PROJECTS_TABLE_NAME: ${self:custom.table.projects}
CORS_ORIGIN: ${self:custom.cors.origin}
ProjectsGetAll:
handler: function/ProjectsGetAll.handler
events:
- http:
path: projects
method: get
cors:
origin: ${self:custom.cors.origin}
headers: ${self:custom.headers}
request:
parameters:
querystrings:
includeArchived: false
userId: true
environment:
PROJECTS_TABLE_NAME: ${self:custom.table.projects}
CORS_ORIGIN: ${self:custom.cors.origin}
callback code:
const DEFAULT_HEADERS = {
"Access-Control-Allow-Origin": CORS_ORIGIN,
"Access-Control-Allow-Credentials": false
};
// [...]
return {
statusCode: 200,
body: JSON.stringify({myjsonResponse: '123'}),
headers: Object.assign(DEFAULT_HEADERS, headers)
};
Any ideas what I may be doing wrong? This CORS issue is happening on cloudfront and localhost (react app using AXIOS for requests).