import { CognitoIdentityProviderClient, AdminSetUserPasswordCommand } from "@aws-sdk/client-cognito-identity-provider"; const REGION = "us-east-1"; const USER_POOL_ID = "us-east-1_xxxxxx"; const TEMP_PASSWORD = function(){ // generate a random password with 8 characters, including at least one uppercase letter, one lowercase letter, one number, and one special character const length = 12; const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+~`|}{[]:;?><,./-="; let password = ""; for (let i = 0, n = charset.length; i < length - 4; ++i) { password += charset.charAt(Math.floor(Math.random() * n)); } password += "ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(Math.floor(Math.random() * 26)); // at least one uppercase password += "abcdefghijklmnopqrstuvwxyz".charAt(Math.floor(Math.random() * 26)); // at least one lowercase password += "0123456789".charAt(Math.floor(Math.random() * 10)); // at least one number password += "!@#$%^&*()_+~`|}{[]:;?><,./-=".charAt(Math.floor(Math.random() * 20)); // at least one special character return password; }; const CONCURRENCY = 3; // same as xargs -P 5 const MAX_RETRIES = 3; const client = new CognitoIdentityProviderClient({ region: REGION }); // Read all usernames from file const usernames = [ '1.1@1.com' ] // Function to set temporary password for one user async function setTempPassword(username, attempt = 1) { let password = TEMP_PASSWORD(); const cmd = new AdminSetUserPasswordCommand({ UserPoolId: USER_POOL_ID, Username: username, Password: password, Permanent: false, // equivalent to --no-permanent }); try { await client.send(cmd); console.log(`✅ [${attempt}] Set temporary password ${password} for ${username}`); } catch (err) { console.error(`❌ [${attempt}] Error for ${username}: ${err.message}`); if (attempt < MAX_RETRIES) { const delay = 1000 * attempt; // exponential backoff console.log(`🔁 Retrying ${username} in ${delay / 1000}s...`); await new Promise(res => setTimeout(res, delay)); return setTempPassword(username, attempt + 1); } else { console.error(`🚨 Failed after ${MAX_RETRIES} attempts: ${username}`); } } } // Run tasks with concurrency limit async function run() { const queue = [...usernames]; const workers = Array(CONCURRENCY).fill(null).map(async () => { while (queue.length) { const username = queue.shift(); await setTempPassword(username); } }); await Promise.all(workers); console.log("✅ All done"); } run();