【问题标题】:AWS Cognito User Migration Trigger not workingAWS Cognito 用户迁移触发器不起作用
【发布时间】:2022-08-10 04:33:27
【问题描述】:

我们有两个 AWS Cognito 用户池,并且将从一个迁移到另一个。为此,我们编写了以下 lambda 用户迁移触发器。

一切正常,除了没有在新用户池中创建新用户。

def lambda_handler(event, context):

if (event[\'triggerSource\'] == \'UserMigration_Authentication\'):

    # lookup the user in consumer1 user pool
    user, msg = authenticateUser(event[\'userName\'], event[\'request\'][\'password\'])

    if user != None:
        event[\'response\'][\'userAttributes\'] = user
        event[\'response\'][\'finalUserStatus\'] = \"CONFIRMED\"
        event[\'response\'][\'messageAction\'] = \"SUPPRESS\"
        return event
    else:
        callback(\"Bad username or password\");

elif (event[\'triggerSource\'] == \'UserMigration_ForgotPassword\'):
    # lookup the user in consumer1 user pool
    user, msg = authenticateUser(event[\'userName\'], event[\'request\'][\'password\'])

    if user != None:
        event[\'response\'][\'userAttributes\'] = user
        event[\'response\'][\'finalUserStatus\'] = \"CONFIRMED\"
        event[\'response\'][\'messageAction\'] = \"SUPPRESS\"
        return event
    else:
        callback(\"Bad username or password\");

else:
    callback(\'Bad triggerSource\' + event[\'triggerSource\']);

在测试模式下运行此 lambda 时,在 lambda 函数的 AWS 控制台中,获取以下输出:

Test Event Name
AuthTest

Response
{
  \"version\": \"1\",
  \"triggerSource\": \"UserMigration_Authentication\",
  \"region\": \"us-east-1\",
  \"userPoolId\": \"us-east-1_pe4xVIDEN\",
  \"userName\": \"+17202702628\",
  \"callerContext\": {
    \"awsSdkVersion\": \"aws-sdk-unknown-unknown\",
    \"clientId\": \"6u7omas12g2m85je4to2hx12\"
  },
  \"request\": {
    \"password\": \"redacted\",
    \"validationData\": null,
    \"userAttributes\": null
  },
  \"response\": {
    \"userAttributes\": [
      {
        \"Name\": \"email_verified\",
        \"Value\": \"true\"
      },
      {
        \"Name\": \"phone_number_verified\",
        \"Value\": \"true\"
      },
      {
        \"Name\": \"phone_number\",
        \"Value\": \"+17202702628\"
      },
      {
        \"Name\": \"given_name\",
        \"Value\": \"Randy\"
      },
      {
        \"Name\": \"family_name\",
        \"Value\": \"Leonard\"
      },
      {
        \"Name\": \"email\",
        \"Value\": \"rleonard@4thiq.com\"
      },
      {
        \"Name\": \"username\",
        \"Value\": \"test1234!\"
      },
      {
        \"Name\": \"preferred_username\",
        \"Value\": \"test1234!\"
      }
    ],
    \"forceAliasCreation\": null,
    \"finalUserStatus\": \"CONFIRMED\",
    \"messageAction\": \"SUPPRESS\",
    \"desiredDeliveryMediums\": null
  }
}

Function Logs
START RequestId: 18a225d7-72fa-4b84-8199-4ecbd9d4d158 Version: $LATEST
{\'version\': \'1\', \'triggerSource\': \'UserMigration_Authentication\', \'region\': \'us-east-1\', \'userPoolId\': \'us-east-1_pe4xVIDEN\', \'userName\': \'+17202702628\', \'callerContext\': {\'awsSdkVersion\': \'aws-sdk-unknown-unknown\', \'clientId\': \'6u7omas12g2m85je4to2hx12\'}, \'request\': {\'password\': \'P@ssword1\', \'validationData\': None, \'userAttributes\': None}, \'response\': {\'userAttributes\': None, \'forceAliasCreation\': None, \'finalUserStatus\': None, \'messageAction\': None, \'desiredDeliveryMediums\': None}}
END RequestId: 18a225d7-72fa-4b84-8199-4ecbd9d4d158
REPORT RequestId: 18a225d7-72fa-4b84-8199-4ecbd9d4d158  Duration: 2542.38 ms    Billed Duration: 2543 ms    Memory Size: 128 MB Max Memory Used: 68 MB  Init Duration: 230.92 ms

Request ID
18a225d7-72fa-4b84-8199-4ecbd9d4d158

从 Cognito 的新 Cognito 用户池的托管 UI 运行时,获得相同的输出。然而,新用户并未出现在新的 Cognito 用户池中。

好奇我错过了什么,感谢反馈。

兰迪

  • 同样的问题,没找到任何网上资料。你想通了吗?
  • 不幸的是,没有,但需要尽快重新审视这一点。

标签: amazon-web-services aws-lambda amazon-cognito


【解决方案1】:

我认为您的问题是响应中userAttributes 的格式。

根据您共享的输出,返回一个键/值对数组

  "response": {
    "userAttributes": [
      {
        "Name": "email_verified",
        "Value": "true"
      },
      // other records here...
    ]
  }

根据AWS docs,userAttributes 字段应该包含键/值对。

 "response": {
    "userAttributes": {
        "email_verified": true,
        "phone_number_verified": true,
        // other records here...
      }
    ]
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 2020-12-23
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 2018-09-09
    • 2021-04-16
    相关资源
    最近更新 更多