当我尝试使用方法 OPTIONS 进行 CORS 预检 (Lambda) 时,AWS cloudformation template.yml 失败

发布于 2025-01-16 19:21:13 字数 1874 浏览 2 评论 0原文

我尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

金橙橙 2025-01-23 19:21:13

此线程帮助我找到了的问题。

提到了一个工具:cfn-lint,它能够给我一个有用的错误信息:

E0001 转换模板时出错:具有 id 的资源
[我的应用程序]无效。具有 id 的事件 [CorsPreflightEven
t] 无效。无法在 API 方法 [选项] 上设置授权者
路径 [/] 因为“NONE”仅在 DefaultAuthorizer 时才是有效值
在 API 上指定。

所以我删除了 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:

E0001 Error transforming template: Resource with id
[MyApp] is invalid. Event with id [CorsPreflightEven
t] is invalid. Unable to set Authorizer on API method [options] for
path [/] because 'NONE' is only a valid value when a DefaultAuthorizer
on the API is specified.

So I removed the Auth part. Now it goes through pipeline process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文