简体   繁体   English

有没有办法使用 lambda 从认知池中批量删除用户?

[英]Is there a way to batch delete users from cognito pool using lambda?

I am using congnito to authenticate users to my app, and i have a crud where i can handle them one by one, but there are cases when i want to delete them all at once in a single batch operation.我正在使用 congnito 对我的应用程序的用户进行身份验证,并且我有一个 crud,我可以在其中一个一个处理它们,但是在某些情况下,我想在单个批处理操作中一次删除它们。

The problem is that the docs say nothing about this and other A&Q sites say that there is not way for this.问题是文档对此一无所知,而其他 A&Q 网站则表示没有办法做到这一点。

I was thinking to use Promise.all() in an array of cognito clients to delete them in a single batch?我想在一组 cognito 客户端中使用Promise.all()来批量删除它们? but i don't know if it's possible or this may couse timeout in my lanmbda?但我不知道这是否可能,或者这可能会导致我的 lanmbda 超时?

something like this i had in mind.我想到了这样的事情。

// const data = req.body.users;
const aws = require('aws-sdk');
const CognitoIdentityServiceProvider = aws.CognitoIdentityServiceProvider;

const toDelete = [];
data.forEach((item) => {
    const client = new CognitoIdentityServiceProvider({ apiVersion: '2016-04-19', region: 'us-east-1' });
    toDelete.push(cliente.adminDeleteUser(/* data */));
})
Promise.all(toDelete).then(() => { /* something */ });

Could this work?这能行吗? is bad practice?是不好的做法吗? is there a better way?有没有更好的办法?

Another option without using the lambda functions is, if you have configured aws cli in windows, you can use the following script as a bat file to delete the users listed on a single page from the listing command output, repeat until all users are deleted.另一个不使用 lambda 函数的选项是,如果您在 windows 中配置了 aws cli,您可以使用以下脚本作为 bat 文件从列表命令输出中删除单个页面上列出的用户,重复直到所有用户都被删除。

You need to have JQ downloaded and its path added to system env path for the following to work.您需要下载 JQ 并将其路径添加到系统 env 路径中才能使以下内容正常工作。

---delete.bat--- ---删除.bat---

@echo off setlocal @echo off setlocal

for /f "delims=" %%I in ('aws cognito-idp list-users --user-pool-id $COGNITO_USER_POOL_ID ^| jq -r ".Users | .[] | .Username"') do ( aws cognito-idp admin-delete-user --user-pool-id $COGNITO_USER_POOL_ID --username %%I echo %%I deleted ) for /f "delims=" %%I in ('aws cognito-idp list-users --user-pool-id $COGNITO_USER_POOL_ID ^| jq -r ".Users | .[] | .Username"') do ( aws cognito-idp admin-delete-user --user-pool-id $COGNITO_USER_POOL_ID --username %%I echo %%I deleted )

---delete.bat--- ---删除.bat---

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

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