Asynchronous 无法从AWS lambda调用AWS Cognito API,但相同的代码在local node.js中运行良好

Asynchronous 无法从AWS lambda调用AWS Cognito API,但相同的代码在local node.js中运行良好,asynchronous,async-await,aws-lambda,aws-sdk,amazon-cognito,Asynchronous,Async Await,Aws Lambda,Aws Sdk,Amazon Cognito,这是我在这里遇到的第一个问题:-) 我需要列出Cognito使用池中的用户。似乎这只能使用aws sdk CognitoIdentityServiceProvider API来完成。我从我的local node.js获得了下面的代码,可以完美地工作。它按预期列出所有用户 但是,相同的代码在放入AWS lambda函数时表现不同。它仍在运行,但它从不等待Cognoto listUsers()调用返回。它只是简单地完成了,就像“等待”根本就没有等待一样。没有从client.listUsers(参数、

这是我在这里遇到的第一个问题:-)

我需要列出Cognito使用池中的用户。似乎这只能使用aws sdk CognitoIdentityServiceProvider API来完成。我从我的local node.js获得了下面的代码,可以完美地工作。它按预期列出所有用户

但是,相同的代码在放入AWS lambda函数时表现不同。它仍在运行,但它从不等待Cognoto listUsers()调用返回。它只是简单地完成了,就像“等待”根本就没有等待一样。没有从client.listUsers(参数、函数(错误、数据)…..块调用任何console.log()

我直接在Lambda内部以及AWS API网关中对此进行了测试。返回的是相同的null。调用本身是成功的,只是没有返回任何数据。 见末尾的日志

顺便说一句,我确实创建了一个角色,并添加了一个策略,允许该角色完全访问cognito用户池

我错过了什么?谢谢你的帮助

马丁C

-----------------代码------------------------

async function getUserList() {
    console.log("enter LAMDA function**********");
    var aws = require('aws-sdk');
    aws.config.update({accessKeyId: 'xxxxxxxx', secretAccessKey: 'xxxxxxxxxxx'});
    var CognitoIdentityServiceProvider = aws.CognitoIdentityServiceProvider;
    var client = new CognitoIdentityServiceProvider({ apiVersion: '2016-04-19', region: 'us-east-2' });

    var params = {
         UserPoolId: 'us-east-xxxxxxxx', /* required */
          AttributesToGet: [
          'given_name','family_name','phone_number','email','profile'
        ],
        Filter: null,
        Limit: 0,
        PaginationToken: null
    };
    console.log("Right before call the listUser method");

    let result = await client.listUsers(params, function(err, data) {
     console.log("call back reached!");
     if (err) { 
              console.log(err, err.stack); // an error occurred
              const response = {
              statusCode: 500,
              body: JSON.stringify('An error occurred.'),
        }
        return response;
     }
     else  {
         console.log(data);   
         var count = data.Users.length;
             // successful response
             const response = {
             statusCode: 200,
             body: JSON.stringify("sucessful list users! User count="+count)
          }
          return response;
     }
    });

    console.log("no waiting here. async!!!")
}

getUserList();
***************Lambda原木*****************

**************从node.js调用时记录日志****************


getUserList
是lambda函数吗?我不知道你为什么自己调用它
getUserList()

我明白了,您使用的lambda运行时是nodejs version>8,您使用
wait
关键字和回调函数(fail)=>您不等待任何东西

当Lambda调用函数时,函数(异步函数)将在返回或运行到函数末尾(不返回)时完成,在您的情况下,函数将在执行
console.log(“no waiting here.async!!!”)
时完成。在本地环境中,函数将在
callstack
清除时完成(callstack中没有任何回调函数)

正确的方法是,使用aws sdk的promise版本,然后使用
wait
语法来获得结果


事实上,Promission类型是有效的!非常感谢!顺便说一句,getUserList()正是我从node.js调用异步函数的方式。getUserList()的主体转到lambda函数。
async function getUserList() {
    console.log("enter LAMDA function**********");
    var aws = require('aws-sdk');
    aws.config.update({ accessKeyId: 'xxxxxxxx', secretAccessKey: 'xxxxxxxxxxx' });
    var CognitoIdentityServiceProvider = aws.CognitoIdentityServiceProvider;
    var client = new CognitoIdentityServiceProvider({ apiVersion: '2016-04-19', region: 'us-east-2' });

    var params = {
        UserPoolId: 'us-east-xxxxxxxx', /* required */
        AttributesToGet: [
            'given_name', 'family_name', 'phone_number', 'email', 'profile'
        ],
        Filter: null,
        Limit: 0,
        PaginationToken: null
    };
    console.log("Right before call the listUser method");

    try {
        let result = await client.listUsers(params).promise(); // use Promise style
        console.log(data);
        var count = data.Users.length;
        // successful response
        const response = {
            statusCode: 200,
            body: JSON.stringify("sucessful list users! User count=" + count)
        }
        return response; // return to finish function
    } catch (err) {
        console.log(err, err.stack); // an error occurred
        const response = {
            statusCode: 500,
            body: JSON.stringify('An error occurred.'),
        }
        return response;
    }
}

getUserList(); // remove this line when deploy funtion to Lambda.