当我尝试使用方法 OPTIONS 进行 CORS 预检 (Lambda) 时,AWS cloudformation template.yml 失败
我尝试为 OPTIONS 方法添加一个块,以接受并转发到我的 lambda 代理以及 OPTIONS 事件。但是 cloudformation 失败了 - 但我找不到详细原因。
这是我尝试过的块:
CorsPreflightEvent:
Type: Api
Properties:
Path: /
Method: options
Auth:
Authorizer: NONE
这是完整的块:
MyApp:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub 'awscodestar-${ProjectId}-lambda-01'
Handler: index.handler
Runtime: python3.9
Timeout: 10
Role:
Fn::GetAtt:
- LambdaExecutionRole
- Arn
Events:
CorsPreflightEvent:
Type: Api
Properties:
Path: /
Method: options
Auth:
Authorizer: NONE
GetEventAll:
Type: Api
Properties:
Path: /
Method: get
GetEventSectionCat:
Type: Api
Properties:
Path: /{subject}/{category}
Method: get
PostEvent:
Type: Api
Properties:
Path: /
Method: post
一般来说,我希望允许没有 CORS 的选项。所以我在 python 中添加了一个 OPTIONS 响应。我想应该可以。但它会对任何路径做出反应。这就是为什么我之前也尝试过这条路径:
/{proxy+}
我想对任何请求禁用 CORS,但是 chrome 抱怨,这就是为什么我尝试在我的 Python 中发送这个标头 - 这应该对所有类型的请求完成:
headers = {
'Content-Type': 'application/json',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
}
有人看到什么吗我的 template.yml 有问题,为什么“GenerateChangeSet”步骤可能会失败?
与此同时,我改用更简单的解决方案来禁用 cors。不是通过代码,而是在 template.yml 中我添加了此块:
Globals:
Api:
Cors:
AllowMethods: "'OPTIONS,POST,GET'"
AllowHeaders: "'*'"
AllowOrigin: "'*'"
I tried to add a block for method OPTIONS to accept and forward to my lambda proxy also OPTIONS events. But cloudformation fails - but I can not find details why.
This is the block I tried:
CorsPreflightEvent:
Type: Api
Properties:
Path: /
Method: options
Auth:
Authorizer: NONE
This is the full block:
MyApp:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub 'awscodestar-${ProjectId}-lambda-01'
Handler: index.handler
Runtime: python3.9
Timeout: 10
Role:
Fn::GetAtt:
- LambdaExecutionRole
- Arn
Events:
CorsPreflightEvent:
Type: Api
Properties:
Path: /
Method: options
Auth:
Authorizer: NONE
GetEventAll:
Type: Api
Properties:
Path: /
Method: get
GetEventSectionCat:
Type: Api
Properties:
Path: /{subject}/{category}
Method: get
PostEvent:
Type: Api
Properties:
Path: /
Method: post
In general I want to allow OPTIONS without CORS. So I added to my python a OPTIONS response. I guess it should work. But it shall react on any path. Thats why I also tried this path before:
/{proxy+}
I want to disable CORS for any requests, but chrome complains, that is why I try to send this header in my Python - which should be done for all kind of requests:
headers = {
'Content-Type': 'application/json',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
}
Does anyone see what is wrong with my template.yml and why the step "GenerateChangeSet" may fail?
Meanwhile I switched to an easier solution to disable cors. Not by code but in template.yml I added this block:
Globals:
Api:
Cors:
AllowMethods: "'OPTIONS,POST,GET'"
AllowHeaders: "'*'"
AllowOrigin: "'*'"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此线程帮助我找到了的问题。
提到了一个工具:cfn-lint,它能够给我一个有用的错误信息:
所以我删除了
Auth
部分。现在它正在经历管道过程。This thread helped me to find the issue.
There is a tool mentioned: cfn-lint, which was able to give me a useful error message:
So I removed the
Auth
part. Now it goes through pipeline process.