将 CORS 与 AWS SAM 结合使用

将 CORS 与 AWS SAM 结合使用

问题描述:

我创建了一些由 API 网关事件触发的 Lambda 函数.

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

现在我想为这些端点启用 CORS,但它似乎不起作用.在 AWS SAM 的最后几个版本中,添加或更新了 CORS 功能,但我仍然无法使其正常工作.

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.

这是我试过的:

Gobals:
  Api:
    Cors: "'*'"

您也可以在 lambda 处理程序中指定这些(Python 示例):

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'
    }