【问题标题】:Use same Cognito UserPool with Lamda Function and Resources?将相同的 Cognito 用户池与 Lambda 函数和资源一起使用?
【发布时间】:2018-01-18 15:46:42
【问题描述】:

我正在使用无服务器框架,我需要为 Lambda 函数创建的 UserPool 覆盖一些默认值。正确的方法是什么? 我的serverless.yml 正在创建两个user-pool(同名),一个用于lambda 函数,另一个用于UserPool 资源:

service: userpool

custom:
  stage: dev
  poolName: user-pool

provider:
  name: aws
  runtime: nodejs6.10
  stage: ${opt:stage, self:custom.stage}

functions:
  preSignUp:
    handler: handler.preSignUp
    events:
      - cognitoUserPool:
          pool: ${self:custom.poolName}
          trigger: PreSignUp

resources:
  Resources:
    UserPool:
      Type: "AWS::Cognito::UserPool"
      Properties:
        UserPoolName: ${self:custom.poolName}
        AliasAttributes:
          - email
        AutoVerifiedAttributes:
          - email
        Schema:
          - Name: name
            AttributeDataType: String
            Mutable: true
            Required: true
          - Name: email
            AttributeDataType: String
            Mutable: false
            Required: true

【问题讨论】:

    标签: amazon-web-services lambda amazon-cognito serverless-framework


    【解决方案1】:

    根据https://serverless.com/framework/docs/providers/aws/guide/resources/#aws---resources 中的无服务器文档

    如果您创建了遵循此格式 CognitoUserPool{normalizedPoolId} 的 Cognito 用户池资源,则可以为每个 lambda 函数提供 normalizedPoolId

    在您的情况下,您已将 Cognito 用户池定义为“CognitoUserPoolTestPool”,它允许您在 lambdas 中使用 TestPool。

    【讨论】:

      【解决方案2】:

      它现在可以工作了,但是有人可以向我解释一下吗?下面的代码如何知道 Cognito 资源和事件创建的池是一样的?

      service: userpool
      
      custom:
        stage: dev
      
      environment:
        USER_POOL: userPool
      
      provider:
        name: aws
        runtime: nodejs6.10
        stage: ${opt:stage, self:custom.stage}
      
      functions:
        preSignUp:
          handler: handler.preSignUp
          events:
            - cognitoUserPool:
                pool: TestPool
                trigger: PreSignUp
      
      resources:
        Resources:
          CognitoUserPoolTestPool:
            Type: "AWS::Cognito::UserPool"
            Properties:
              AliasAttributes:
                - email
              AutoVerifiedAttributes:
                - email
              Schema:
                - Name: name
                  AttributeDataType: String
                  Mutable: true
                  Required: true
                - Name: email
                  AttributeDataType: String
                  Mutable: false
                  Required: true
      

      【讨论】:

      • 尝试删除整个堆栈和所有用户池(我认为 CF 不会自动删除它们)并重新部署,以确保其正常工作。
      【解决方案3】:

      这个问题在新版本中得到了更好的解决:

      Add support for existing Cognito User Pools

      您的函数事件应如下所示:

      functions:
        preSignUp:
          handler: handler.preSignUp
          events:
            - cognitoUserPool:
                pool: ${self:custom.poolName}
                trigger: PreSignUp
                existing: true
      

      无需在资源定义前添加额外的“CognitoUserPool”。

      【讨论】:

        猜你喜欢
        • 2021-05-05
        • 1970-01-01
        • 2020-04-22
        • 2018-10-13
        • 1970-01-01
        • 2022-10-21
        • 1970-01-01
        • 2018-11-22
        • 1970-01-01
        相关资源
        最近更新 更多