gpt4 book ai didi

python-2.7 - 在 python 中使用 Lambda 触发器将用户迁移到 Cognito

转载 作者:行者123 更新时间:2023-12-01 00:19:43 24 4
gpt4 key购买 nike

我在 Python 中创建了一个 Lambda 函数来将用户从 RDS 迁移到 AWS Cognito。我面临的问题是我的函数的返回类型,以便 Cognito 创建用户。起初我返回JSON:

return {
"response": {
"userAttributes": {
"email": event["userName"],
},
"finalUserStatus": "CONFIRMED",
"messageAction": "SUPPRESS",
"desiredDeliveryMediums": "EMAIL",
"forceAliasCreation": "false"
}
}

这导致了异常:
enter image description here

我也试着关注唯一的 code Sample (第 109 页)他们介绍了如何通过 Lambda 迁移用户:
exports.handler = function (event, context) {
if (event.triggerSource == "UserMigration_Authentication") {
// authenticate the user with your existing user directory service
var user = authenticateUser(event.userName, event.request.password);
if (user) {
event.response.userAttributes = {
"email": user.emailAddress,
"email_verified": "true"
};
event.response.finalUserStatus = "CONFIRMED";
event.response.messageAction = "SUPPRESS";
context.succeed(event);
} else {
context.fail("Bad password");
}
} else if (event.triggerSource == "UserMigration_ForgotPassword") {
// Lookup the user in your existing user directory service
var user = lookupUser(event.userName);
if (user) {
event.response.userAttributes = {
"email": user.emailAddress,

// required to enable password-reset code to be sent to user
"email_verified": "true"
};
event.response.messageAction = "SUPPRESS";
context.succeed(event);
} else {
context.fail("Bad password");
}
} else {
context.fail("Bad triggerSource " + event.triggerSource);
}
};

在这个例子中,我假设我应该在向它添加新值后返回“事件”对象,下面是我的 Python 代码:
event["response"] = {
"userAttributes": {
"email": event["userName"],
"email_verified": "true"
},
"finalUserStatus": "CONFIRMED",
"messageAction": "SUPPRESS",
"desiredDeliveryMediums": "EMAIL",
"forceAliasCreation": "false"
}

return event

但这也不起作用并引发了同样的异常。在 Cognito 中创建新用户的正确返回类型是什么?

最佳答案

问题出在事件对象中,这是我为使其正常工作所做的更改:
删除了该行

"desiredDeliveryMediums": "EMAIL",

因为这与参数冲突 - "messageAction": "SUPPRESS"。


"forceAliasCreation": "false" 

没有必要,因为 false 是默认值。还应该进行测试以检查用户池中是否不存在用户名/电子邮件。

我在我的用户池中使用以下代码进行了测试,它有效。
def lambda_handler(event, context):
## print("migrateUserLambda Python")

event["response"] = {
"userAttributes": {
"email": event["userName"],
"email_verified": "true"
},
"finalUserStatus": "CONFIRMED",
"messageAction": "SUPPRESS"
}

return event

关于python-2.7 - 在 python 中使用 Lambda 触发器将用户迁移到 Cognito,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49745219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com