【问题标题】:Issue while configuring API routes with path parameter using serverless framework使用无服务器框架配置带有路径参数的 API 路由时出现问题
【发布时间】:2022-07-19 06:37:02
【问题描述】:

我对 AWS 无服务器和无服务器框架比较陌生。我创建了以下 API 路由并使用离线插件对其进行了测试,它按预期工作。但是,当我尝试使用 sls deploy 将其部署到我的 AWS 时,它会抛出以下错误。

Error:
CREATE_FAILED: ApiGatewayResourceNoteTimestampVar (AWS::ApiGateway::Resource)
Resource handler returned message: "A sibling ({note_id}) of this resource already has a variable path part -- only one is allowed (Service: ApiGateway, Status Code: 400, Request ID: c12acedd-d270-4988-bb01-427d058aa76c, Extended Request ID: null)" (RequestToken: 803cf4a6-5bc4-48c5-6648-2b0de46528f6, HandlerErrorCode: InvalidRequest)

我的 serverless.yml 看起来像这样

functions:
  add-note:
    handler: apis/add-note.handler
    description: POST /note
    events:
      - http:
          path: note
          method: post
          cors:
            origins: "*"
            headers: ${self:custom.allowedHeaders}
  update-note:
    handler: apis/update-note.handler
    description: PATCH /note
    events:
      - http:
          path: note
          method: patch
          cors:
            origins: "*"
            headers: ${self:custom.allowedHeaders}
  delete-note:
    handler: apis/delete-note.handler
    description: DELETE /note/{timestamp}
    events:
      - http:
          path: note/{timestamp}
          method: delete
          cors:
            origins: "*"
            headers: ${self:custom.allowedHeaders}
  get-notes:
    handler: apis/get-notes.handler
    description: GET /note
    events:
      - http:
          path: note
          method: get
          cors:
            origins: "*"
            headers: ${self:custom.allowedHeaders}
  get-note:
    handler: apis/get-note.handler
    description: GET /note/{note_id}
    events:
      - http:
          path: note/{note_id}
          method: get
          cors:
            origins: "*"
            headers: ${self:custom.allowedHeaders}

我不知道代码有什么问题,因为它在本地运行良好? 我很感激这里的帮助。

【问题讨论】:

    标签: serverless serverless-framework aws-serverless


    【解决方案1】:

    如果您将路由从一个 lambda 函数更改为另一个,就会发生这种情况。这是 API Gateway 和 Lambda 交互的结果。

    您需要先从函数中删除路由,部署堆栈,然后将路由添加到新函数。

    它可以在本地工作,因为您的 API Gateway 模拟器实际上并不使用 CloudFormation - 这是 Serverless 用来部署您的应用程序的东西。

    【讨论】:

      【解决方案2】:

      嗨,也许您需要在 serverless.yml 上的路径 note_id 中添加参数 有些人喜欢这个例子。

              request:          
                  parameters:       
                    paths: 
                      note_id: true
      

      【讨论】:

        猜你喜欢
        • 2020-04-25
        • 2016-12-30
        • 2021-10-28
        • 2011-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        相关资源
        最近更新 更多