[英]Unable to fetch user information from AWS cognito using AWS Lambda service
This is my first lambda being written in JS.这是我用 JS 编写的第一个 lambda。 I am trying to fetch the user information from AWS Cognito using lambda function.
我正在尝试使用 lambda 函数从 AWS Cognito 获取用户信息。 For this, I am using the Javascript SDK provided by AWS.
为此,我使用了 AWS 提供的 Javascript SDK。 My code is as follows
我的代码如下
'use strict';
exports.handler = async (event) => {
const AWS = require('aws-sdk');
var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider({apiVersion: '2016-04-18'});
var params = {
AccessToken: 'my-access-token'
};
console.log('Getting info from cognito ${cognitoidentityserviceprovider}');
cognitoidentityserviceprovider.getUser(params, function(err, data) {
if (err) {
console.log('In error stack of cognito call');
console.log(err, err.stack); // an error occurred
}
else{
console.log('In success stack of cognito call');
console.log(data); // successful response
}
});
console.log('completed call from cognito');
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};
My function logs are as follows我的功能日志如下
Function logs:
START RequestId: func-Id1 Version: $LATEST
2020-09-07T14:18:05.647Z func-Id1 INFO Start the lambda function
2020-09-07T14:18:10.531Z func-Id1 INFO Getting info from cognito ${cognitoidentityserviceprovider}
2020-09-07T14:18:10.893Z func-Id1 INFO completed call from cognito
END RequestId: func-Id1
Now, the issue that I am facing is that I am not able to retrieve any information from cognito.现在,我面临的问题是我无法从 cognito 中检索任何信息。 I neither receive the success response nor the error message.
我既没有收到成功响应,也没有收到错误消息。 Could anyone please suggest as to what I am missing in my lambda function ?
任何人都可以就我的 lambda 函数中缺少的内容提出建议吗? TIA
TIA
I think your lambda is timing out.我认为你的 lambda 超时了。
'use strict'; const AWS = require('aws-sdk'); var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider({apiVersion: '2016-04-18','us-east-1'}); exports.handler = async (event) => { var params = { AccessToken: 'my-access-token' }; console.log('Getting info from cognito ${cognitoidentityserviceprovider}'); cognitoidentityserviceprovider.getUser(params, function(err, data) { if (err) { console.log('In error stack of cognito call'); console.log(err, err.stack); // an error occurred } else{ console.log('In success stack of cognito call'); console.log(data); // successful response } }); console.log('completed call from cognito'); const response = { statusCode: 200, body: JSON.stringify('Hello from Lambda!'), }; return response;
'use strict'; const AWS = require('aws-sdk'); var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider({apiVersion: '2016-04-18','us-east-1'}); exports.handler = async (event) => {
var params = { AccessToken: 'my-access-token' }; console.log('Getting info from cognito ${cognitoidentityserviceprovider}'); cognitoidentityserviceprovider.getUser(params, function(err, data) { if (err) { console.log('In error stack of cognito call'); console.log(err, err.stack); // an error occurred } else{ console.log('In success stack of cognito call'); console.log(data); // successful response } }); console.log('completed call from cognito'); const response = { statusCode: 200, body: JSON.stringify('Hello from Lambda!'), }; return response;
'use strict'; const AWS = require('aws-sdk'); var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider({apiVersion: '2016-04-18','us-east-1'}); exports.handler = async (event) => { var params = { AccessToken: 'my-access-token' }; console.log('Getting info from cognito ${cognitoidentityserviceprovider}'); cognitoidentityserviceprovider.getUser(params, function(err, data) { if (err) { console.log('In error stack of cognito call'); console.log(err, err.stack); // an error occurred } else{ console.log('In success stack of cognito call'); console.log(data); // successful response } }); console.log('completed call from cognito'); const response = { statusCode: 200, body: JSON.stringify('Hello from Lambda!'), }; return response;
};
Sorry about the formatting I tried and tried but this code refused to be markedup!对我尝试过的格式感到抱歉,但此代码拒绝标记!
I think your Lambda function is returning the response and completing before the cognitoidentityserviceprovider.getUser()
function's callback is executed on return of the response.我认为您的 Lambda 函数正在返回响应并在
cognitoidentityserviceprovider.getUser()
函数的回调在响应返回时执行之前完成。
The call to cognitoidentityserviceprovider.getUser()
is non-blocking.对
cognitoidentityserviceprovider.getUser()
的调用是非阻塞的。 In other words, it won't wait for the callback to execute, but will rather continue to execute the rest of the code.换句话说,它不会等待回调执行,而是继续执行其余的代码。
You can do one of two things depending on how you decide to handle your asynchronous code:根据您决定如何处理异步代码,您可以执行以下两项操作之一:
Use Async/Await使用异步/等待
Your Lambda function is currently using the async
keyword, which expects you to use the corresponding await
keyword to handle asynchronous requests, eg:您的 Lambda 函数当前正在使用
async
关键字,它希望您使用相应的await
关键字来处理异步请求,例如:
try {
const user = await cognitoidentityserviceprovider.getUser(params).promise();
return {
statusCode: 200,
user: user
};
} catch(error) {
return {
statusCode: 500,
error: error
};
}
or even just:甚至只是:
const AWS = require('aws-sdk');
const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider({apiVersion: '2016-04-18','us-east-1'});
exports.handler = async (event) => {
const params = {
AccessToken: 'my-access-token'
};
return cognitoidentityserviceprovider.getUser(params).promise();
};
Using Callbacks使用回调
Remove the async
keyword and add 2 more parameters to your Lambda function definition:删除
async
关键字并向您的 Lambda 函数定义添加另外 2 个参数:
exports.handler = (events, context, callback) => {
...
}
then use the Lambda's callback
parameter to return the response when your cognitoidentityserviceprovider.getUser()
request's callback returns.然后使用 Lambda 的
callback
参数在您的cognitoidentityserviceprovider.getUser()
请求的回调返回时返回响应。
cognitoidentityserviceprovider.getUser(params, (err, data) => {
if (err) {
console.log('In error stack of cognito call');
done(err);
} else {
console.log('In success stack of cognito call');
done(null, data);
}
});
... no need for a return statement ...
For more information see this page on handlers in the Lambda docs有关更多信息, 请参阅 Lambda 文档中有关处理程序的此页面
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.