无服务器框架忽略lambda代理设置中的“授权者”块

时间:2019-10-03 18:01:46

标签: aws-lambda aws-api-gateway amazon-cognito serverless-framework serverless

我们有一个serverless.yml,可以在APIGateway中创建一个lambda代理。

我已经看到了几种将认知用户池授权者附加到lambda代理的方法,但是它们都不起作用。在每种情况下,结果都是在Proxy或Proxy +上均未设置任何身份验证。 (例如,在API Gateway控制台中,“ Auth”始终显示为“ NONE”。)

示例:

    events:
      - http: ANY /
        integration: lambda-proxy
        authorizer:
          type: COGNITO_USER_POOLS
          authorizerId:
            Ref: CognitoUserPoolAuthorizer
      - http: ANY {proxy+}
        integration: lambda-proxy
        authorizer:
          arn: ${self:custom.userPools.arnBase}/${self:custom.userPools.ids.${self:custom.stage}}
          # The above results in the format: "arn:aws:cognito-idp:us-west-2:<account_id>:userpool/us-west-2_<user_pool_id>"

您可以在上方看到我正在尝试两种不同的方法来完成同一任务。 (让我们尝试将科学扔到墙上,看看有什么问题。) 两种方法都在不同的地方by the serverless framework进行了文档记录,但是关于lambda代理都没有文档记录,因此我不确定是否存在未记录的差异。

在第一种方法中(在资源上使用Ref),正确创建了资源,但是没有auth附加到端点。 (我没有在此处包括资源块,因为它按预期工作。)第二种方法具有相同的效果(但未创建授权者)。

我们的无服务器框架版本为1.52,符合this other SO post中所述的要求。

我也尝试过将integration设置为lambda,或者完全不使用该行。在所有情况下结果都是相同的。

已经可以通过在控制台中手动选择授权者来使其工作,但我们正在尝试消除这些手动步骤。

我们在这里想念什么?

1 个答案:

答案 0 :(得分:1)

乍一看,您的serverless.yml文件的缩进可能已关闭。您可以使用http数组项目下面的标签再次尝试吗?

此外,您将需要删除方法和路径的简写,而是分别使用它们。下面的代码段应该可以正常工作:

    events:
      - http:
          method: ANY
          path: /
          integration: lambda-proxy
          authorizer:
            type: COGNITO_USER_POOLS
            authorizerId:
              Ref: CognitoUserPoolAuthorizer
      - http:
          method: ANY
          path: /{proxy+}
          integration: lambda-proxy
          authorizer:
            arn: ${self:custom.userPools.arnBase}/${self:custom.userPools.ids.${self:custom.stage}}