Muk*_*h S 2 amazon-web-services node.js amazon-cognito aws-lambda
我正在使用AWS Lambda函数(使用nodejs)。
从APP发送到Cognito的任何请求以注册用户。然后,我设置了“ 预注册”触发器来验证用户的客户,并检查我们数据库中是否有可用的用户自定义属性。如果是,则返回错误,否则将新记录插入数据库并将事件返回给Cognito。
TimeoutInfo-5分钟。
它在请求中的某个时间发生,而不是一直发生。RequestId为不同的。(它将在某个时间触发3次,大部分时间两次)
Lambda触发代码如下。
用户/ index.js
const handler = async (event, context) => {
log.info('createUserLambda:start');
// immediately return once the call back is called to avoid
// lambda time out because of any open db connections
context.callbackWaitsForEmptyEventLoop = false;
return await preUserCreate(event);
};
Run Code Online (Sandbox Code Playgroud)
Exports.handler =处理程序; users / users.js
export const preUserCreate = async (event) => {
log.info('preUserCreate:Start');
let userAttributes = event.request.userAttributes;
const currentDate = moment().utc().format('YYYY-MM-DD HH:mm:ss');
try {
let userParams = {
'docStatus': 'VRF'
};
let docParams = [{
'docNumber': userAttributes['custom:document_number'] ? userAttributes['custom:document_number'] : '',
'createdDate': currentDate
}];
if (docParams.length && docParams[0].docNumber) {
let documentExit = await getDocs(docParams[0].docNumber);
if (documentExit.length) {
log.info('preUserCreate:Error');
throw new Error('Document number already exist.');;
}
}
let documentRs = await insertDocument(docParams);
userParams = {
'did': documentRs[0].id,
'id': event.userName,
'createdDate': currentDate,
'updatedDate': currentDate,
...userParams
};
let userRs = await insertUser([userParams]);
if (docParams.length && docParams[0].docNumber) {
let resultData = await getUserAccountFromAPI(docParams[0].docNumber);
if (resultData) {
let foramattedData = await formattedAccountsData(resultData, userRs[0].id, documentRs[0].id);
await insertUserAccounts(foramattedData);
}
}
log.info('preUserCreate:Success');
event.response = {
'autoConfirmUser': false,
'autoVerifyPhone': false,
'autoVerifyEmail': false
};
return event;
} catch (error) {
log.info('preUserCreate:Error', error);
throw (error);
}
}
Run Code Online (Sandbox Code Playgroud)
您有可能在 VPC 中运行 lambda 吗?我在冷启动时在 VPC 中运行的 Cognito 触发器中看到过类似的行为。一旦 lambda 变暖,问题就消失了
我的预感是,Cognito 内部有一个非常短的执行触发器的超时时间,如果触发器没有及时回复,它会自动重试。
我们最终必须向触发器添加逻辑来测试这种情况,这样我们就不会重复写入数据库。
之所以可能发生这种情况,是因为集成Lambda的Cognito施加的执行超时为5秒-并且无法更改。另请注意,Cognito尝试(重新)调用该函数的最大次数为3次。
在使用Lambda触发器自定义用户池工作流部分中,它指出:
重要 Amazon Cognito同步调用Lambda函数。调用时,您的Lambda函数必须在5秒内响应。如果不是,则Amazon Cognito重试该呼叫。尝试3次失败后,该功能超时。此5秒超时值无法更改。
因此,为减少执行时间,有必要考虑在可能的地方引入缓存。包括数据库连接等
但请注意,您几乎无法控制Lambda的重用与重新启动频率,因此您需要在预热时间方面牢记这一点。
| 归档时间: |
|
| 查看次数: |
683 次 |
| 最近记录: |