【问题标题】:Serverless duplicates user pools instead of reusing by name无服务器复制用户池而不是按名称重用
【发布时间】:2018-08-25 12:13:28
【问题描述】:

我正在使用无服务器来部署 AWS CloudFormation 模板和一些功能,这是我的 serverless.yml 文件的一部分:

resources:
  Resources:
    MyUserPool: #An inline comment
      Type: "AWS::Cognito::UserPool"
      Properties:
        UserPoolName: "MyUserPool"
        Policies:
          PasswordPolicy: 
            MinimumLength: 7
            RequireLowercase: false
            RequireNumbers: true
            RequireSymbols: false
            RequireUppercase: false
functions:
  preSignUp:
    handler: presignup.validate
    events:
      - cognitoUserPool:
          pool: "MyUserPool"
          trigger: PreSignUp

如您所见,两个用户池名称相同,但是当我运行无服务器部署时,会创建两个同名的用户池。

这是一个错误还是我遗漏了什么?

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation serverless-framework serverless


    【解决方案1】:

    起初我还发现这违反直觉且令人困惑。然而,这实际上是预期的(和记录的)行为。

    当您将 Cognito 事件作为触发器附加到函数时,Serverless 会为您创建一个用户池,甚至不会被询问。 Source:

    这将创建一个具有指定名称的 Cognito 用户池。

    因此,在您的情况下,cognitoUserPool 事件正在创建一个用户池,而您的 Resources 部分正在创建另一个用户池。 Resources 创建的那个是正确的(有自定义密码策略),lambda 触发器创建的那个有默认配置。该修复程序在 "Overriding a generated User Pool" 标题下进行了描述。

    您在资源部分的用户池键前面加上 CognitoUserPool,这将导致您的触发器和您的资源在生成的 CloudFormation 模板中引用同一个用户池。

    在你的情况下,这意味着简单地改变这个:

    resources:
      Resources:
        MyUserPool:
          Type: "AWS::Cognito::UserPool"
    

    到这里:

    resources:
      Resources:
        CognitoUserPoolMyUserPool:
          Type: "AWS::Cognito::UserPool"
    

    使用无服务器 1.26.0 测试

    【讨论】:

      【解决方案2】:

      执行此操作的正确方法是在您的函数 cognitoUserPool 属性上设置 existing: true,如下所示...

        createAuthChallenge:
          handler: services/auth/createAuthChallenge.handler
          events:
            - cognitoUserPool:
                pool: ${self:custom.stage}-user-pool
                trigger: CreateAuthChallenge
                existing: true
      

      Serverless added support 2019 年 7 月。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-23
        • 2020-04-01
        • 2020-10-12
        • 1970-01-01
        • 2017-03-02
        • 2018-04-23
        相关资源
        最近更新 更多