5
votes

I've created a few Lambda functions that get triggered by API-Gateway events.

Now I want to enable CORS for these endpoints, but it doesn't seem to work. In the last few versions of AWS SAM the CORS functionality was added or updated, but I still can't get it to work.

This is what I tried:

Gobals:
  Api:
    Cors: "'*'"
2

2 Answers

6
votes

You forgot to add the AllowOrigin property.

It needs to look like this:

Globals:
  Api:
    Cors:
      AllowOrigin: "'*'"
4
votes

You can also specify these in your lambda handlers (Python example):

return {
        'statusCode': status_code,
        'headers': {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Origin': '*'
        },
        'body': status_message if status_code == 200 else 'Failure'
    }