I think this is related to this issue: https://github.com/serverless/serverless/issues/1960
I am trying to organize my CRUD functionalities into one function. However, I am having problems with CORS. I always get this error:
If you’re routing everything through a single Lambda then you can simplify the config by dealing with invalid method/path combinations in the Lambda. This reduces your config to:
users:
handler: handler.main
events:
- http:
path: v1/users
method: any
cors: true
- http:
path: v1/users/{id}
method: any
cors: true
I wrote a post back in December about doing exactly what you’re trying to do. It didn’t have CORS support then but I’ve just added that. You get view it here including the source/settings (all tested).
If you’re still stuck then can you post your Lambda source?
The real problem is the 502 error from your API. Something is going wrong in your handler and the API Gateway is generating the response rather than your Lambda so the headers aren’t being set. Sort that out and it should work.
Here are a few things I can see:
Your main function shouldn’t be async.
export async function main(event, context, callback) {
Your main function doesn’t return a result (by calling callback) under all circumstances. If you’re using lambda proxy integration then it’s your responsibility to return an error.
async function listItems(event, context, callback) {
but listItems is async so you should await the result. Having said that, I actually think you don’t want it to be async either as you’re returning via the callback.