Skip to content

Commit

Permalink
infra: Improve clean up performance (#2928)
Browse files Browse the repository at this point in the history
Even at 5 min, this is timing out. This seems to help tremendously without causing rate limit issues.
  • Loading branch information
dnys1 committed Apr 25, 2023
1 parent 9762829 commit 8984d93
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions infra/lib/cleanup-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,35 @@ const { USER_POOL_ID } = process.env;
export const handler: lambda.Handler = async (event) => {
console.log(`Got event: ${JSON.stringify(event, null, 2)}`);

const users: UserType[] = [];
try {
console.log('Listing users...');

let nextToken: string | undefined;
do {
while (true) {
const usersResp = await CLIENT.listUsers({
UserPoolId: USER_POOL_ID!,
PaginationToken: nextToken,
});
for (const user of usersResp.Users ?? []) {
users.push(user);
const users: UserType[] = usersResp.Users ?? [];
if (users.length === 0) {
break;
}
nextToken = usersResp.PaginationToken;
} while (nextToken);
} catch (e) {
console.error(`Error listing users: ${e}`);
return;
}

console.log(`Got users: ${JSON.stringify(users, null, 2)}`);
console.log(`Got users: ${JSON.stringify(users, null, 2)}`);
await Promise.all(
users.map(async (user) => {
console.log(`Deleting user: ${user.Username}`);
await CLIENT.adminDeleteUser({
UserPoolId: USER_POOL_ID!,
Username: user.Username!,
});
}),
);

for (const user of users) {
console.log(`Deleting user: ${user.Username}`);
await CLIENT.adminDeleteUser({
UserPoolId: USER_POOL_ID!,
Username: user.Username!,
});
// Prevents `TooManyRequestsException`
console.log('Waiting 5 seconds...');
await new Promise((resolve) => setTimeout(resolve, 5000));
}
} catch (e) {
console.error(`Error deleting users: ${e}`);
return;
}
};

0 comments on commit 8984d93

Please sign in to comment.