列出AWS Lambda上的Cognito用户池用户 [英] Listing cognito userpool users on AWS lambda

查看:114
本文介绍了列出AWS Lambda上的Cognito用户池用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的lambda函数中列出所有我的cognito用户,但是在返回中什么也没得到,好像没有执行回调.我在做什么错了?

I'm trying to list all of my cognito users in my lambda function, however i get nothing in the return as if the callback not getting executed. What am I doing wrong?

下面的代码输出仅在控制台向您打招呼.

The output of the code below just gives me a hello in the console.

var AWS = require("aws-sdk");

const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
export async function main() {
console.log("hello")
  var params = {
    UserPoolId: "myuserpoolid",
    AttributesToGet: ["username"]
  };

  cognitoidentityserviceprovider.listUsers(params, (err, data) => {
    if (err) {
      console.log(err, err.stack);
      return err;
    } else {
      console.log(data);
      return data;
    }
  });
}

推荐答案

首先,代码结构错误.Lambda函数的标头应具有一定的结构,可以使用异步函数也可以使用非异步函数.由于您在示例中使用的是非异步代码,因此我将在稍后向您展示如何进行操作.

First of all, the structure of the code is wrong. The header of Lambda function should have a certain structure, either using async function or non-async function. Since you are using non-async code in your example I will show you how to do the later.

var AWS = require("aws-sdk");

const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();

exports.handler =  function(event, context, callback) {
  console.log("hello")
  var params = {
    UserPoolId: "myuserpoolid",
    AttributesToGet: ["username"]
  };

  cognitoidentityserviceprovider.listUsers(params, (err, data) => {
    if (err) {
      console.log(err, err.stack);
      callback(err)        // here is the error return
    } else {
      console.log(data);
      callback(null, data) // here is the success return
    }
  });
}

在这种情况下,Lambda仅在调用 callback (或超时)时结束.

In this case, Lambda will finish only when callback is called (or when it times out).

类似地,您可以使用异步功能,但是您将需要相应地重组代码.这是取自官方文档的示例.请注意如何使用Promise包装器.

Similarly, you can use async function but you will need to restructure your code accordingly. Here is an example taken from official docs. Note how the promise wrapper is used.

const https = require('https')
let url = "https://docs.aws.amazon.com/lambda/latest/dg/welcome.html"

exports.handler = async function(event) {
  const promise = new Promise(function(resolve, reject) {
    https.get(url, (res) => {
        resolve(res.statusCode)
      }).on('error', (e) => {
        reject(Error(e))
      })
    })
  return promise
}

这篇关于列出AWS Lambda上的Cognito用户池用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆