【问题标题】:AWS SAM template for serverless::api not creating cognito user pool authorizer用于 serverless::api 的 AWS SAM 模板未创建认知用户池授权方
【发布时间】:2021-10-01 18:44:16
【问题描述】:

我不明白为什么在部署此模板后,我在 AWS 控制台的“授权者”选项卡下看不到此 API 的任何授权者。

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  Description here

Globals:
  Function:
    Timeout: 3

Resources:
 
  ProductGet:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: ./
      Handler: product-get.lambda_handler
      Runtime: python3.8
      Role: "particular role here"
      Events:
        ProductGet:
          Type: Api 
          Properties:
            Path: /product-get
            Method: post
            Auth:
              Authorizers:
                MyCognitoAuth:
                 UserPoolArn: "user pool arn here"
                 AuthType: "COGNITO_USER_POOLS"
              DefaultAuthorizer: MyCognitoAuth

【问题讨论】:

    标签: aws-serverless aws-sam


    【解决方案1】:

    想通了。 您不能在“事件”部分定义授权人。 如果您的 API 需要授权方,您必须将该 API 定义为单独的资源,并使用 APIid 将其链接到事件。

    下面的示例代码。

    MyApi:
        Type: AWS::Serverless::Api
        Properties:
          StageName: Prod
          Auth:
            DefaultAuthorizer: MyCognitoAuth # OPTIONAL
            Authorizers:
              MyCognitoAuth:
                Type: COGNITO_USER_POOLS
                # Can also accept an array
                UserPoolArn: "user pool arn here"
    
      ProductGet:
        Type: AWS::Serverless::Function         Properties:
          CodeUri: ./
          Handler: product-get.lambda_handler
          Runtime: python3.8
          Role: 'role ARN here'
          Events:
            ProductGet:
              Type: Api 
              Properties:
                Path: /product-get
                Method: post
                RestApiId: !Ref MyApi #This is how you need to refer to your API
                Auth:
                  Authorizer: MyCognitoAuth
    

    【讨论】:

    • 即使我也遇到了类似的问题。你是怎么想出来的?你有没有找到任何关于它的文档?
    • 不,没有找到直接导致此问题的文档,但发现一些行说,如果您需要高级或详细的配置,请使用单独的定义。
    猜你喜欢
    • 2018-01-24
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 2021-06-23
    • 2018-04-15
    • 2017-02-22
    • 2019-05-22
    • 2021-09-25
    相关资源
    最近更新 更多