简体   繁体   English

AWS:在 Lambda 函数中列出 Cognito 用户

[英]AWS: List Cognito Users within Lambda Function

I'm trying to access the AWS Cognito UserPool from a Lambda function.我正在尝试从 Lambda 函数访问 AWS Cognito UserPool。 The function is configured as the pool's PreSignUp trigger.该函数被配置为池的PreSignUp触发器。 Here is my lambda code (note, that I'm developing with TypeScript):这是我的 lambda 代码(注意,我是用 TypeScript 开发的):

import { CognitoUserPoolEvent, Handler, Context, Callback } from 'aws-lambda';
import { CognitoIdentityServiceProvider } from 'aws-sdk';

export const onPreSignUp: Handler =
  (event: CognitoUserPoolEvent, context: Context, cb: Callback | undefined) => {
    context.callbackWaitsForEmptyEventLoop = false;
    const userAttr = event.request.userAttributes || undefined;
    console.log(userAttr);

    if (cb) {
      const cognitoPoolId = process.env.COGNITO_USER_POOL_ID;
      const email = userAttr.email || userAttr['cognito:email_alias'];

      if (!cognitoPoolId) {
        console.warn('No user pool id defined', cognitoPoolId);
        return cb(new Error('Can not create user'));
      }

      const identityService = new CognitoIdentityServiceProvider();

      const params = {
        UserPoolId: cognitoPoolId,
        Filter: `email = "${email}"`,
      };

      console.log("try to list users", params);
      identityService.listUsers(params, (err, data) => {
        console.log('list users');
        if (err) {
          console.warn('listUsers Error', err);
          return cb(new Error('Can not create user'));
        }
        console.log("data", data);
        return cb(null, 'todo');
      });
    }
  }

Unfortunately the callback of listUsers never returns.不幸的是, listUsers的回调永远不会返回。 If I pass an invalid params object, the callback returns immediately.如果我传递了一个无效的params对象,回调会立即返回。

I've also tried to set the lambdas timeout to max (5 minutes) and increasing the RAM.我还尝试将 lambdas 超时设置为最大(5 分钟)并增加 RAM。 Nothing helps.没有任何帮助。 The lambda execution role has the AmazonCognitoReadOnly , which gives full read access. lambda 执行角色具有AmazonCognitoReadOnly ,可提供完全读取访问权限。 I would also expect a authorization error if this would be a problem.如果这会成为一个问题,我也希望出现授权错误。

EDIT: Right after I had posted this I've solved my problem: the lambda was configured to run within a VPC.编辑:在我发布这篇文章之后,我解决了我的问题:lambda 被配置为在 VPC 内运行。 Setting VPC to none solved it.将 VPC 设置为 none 解决了它。

This code works for me:这段代码对我有用:

module.exports.getUserByAttribute = async (attributeName, attributeValue) => {
  const params = {
      UserPoolId: process.env.userPoolId,
      Filter: `${attributeName} = "${attributeValue}"`,
  }
  try {
      const data = await cognitoIdentityService.listUsers(params).promise()
      const existingUser = data.Users.filter(user => user.UserStatus !== 'EXTERNAL_PROVIDER')[0]
      if (existingUser == null) {
          console.log('Error', 'User not found')
      }
      return existingUser
  } catch (error) {
      console.log('Error: getUserByAttribute', error)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM