|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +GROUP_NAME="participants" |
| 6 | +PASSWORD="Bespinian-1234!" |
| 7 | + |
| 8 | +usage() { |
| 9 | + echo "Usage: $0 <number_of_users>" |
| 10 | + echo " Creates IAM users named user-01, user-02, etc. and adds them to the '$GROUP_NAME' group." |
| 11 | + exit 1 |
| 12 | +} |
| 13 | + |
| 14 | +if [[ $# -ne 1 ]]; then |
| 15 | + usage |
| 16 | +fi |
| 17 | + |
| 18 | +if ! [[ "$1" =~ ^[0-9]+$ ]]; then |
| 19 | + echo "Error: Argument must be a positive integer" |
| 20 | + usage |
| 21 | +fi |
| 22 | + |
| 23 | +NUM_USERS=$1 |
| 24 | + |
| 25 | +if [[ $NUM_USERS -lt 1 ]]; then |
| 26 | + echo "Error: Number of users must be at least 1" |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +# Get list of existing users |
| 31 | +existing_users=$(aws iam list-users --query 'Users[].UserName' --output text) |
| 32 | + |
| 33 | +for i in $(seq 1 "$NUM_USERS"); do |
| 34 | + username=$(printf "user-%02d" "$i") |
| 35 | + |
| 36 | + if echo "$existing_users" | grep -qw "$username"; then |
| 37 | + echo "User $username already exists, skipping creation" |
| 38 | + else |
| 39 | + echo "Creating user $username" |
| 40 | + aws iam create-user --user-name "$username" |
| 41 | + fi |
| 42 | + |
| 43 | + # Create login profile for console access (skip if already exists) |
| 44 | + if aws iam get-login-profile --user-name "$username" &>/dev/null; then |
| 45 | + echo "Login profile for $username already exists, skipping" |
| 46 | + else |
| 47 | + echo "Creating login profile for $username" |
| 48 | + aws iam create-login-profile --user-name "$username" --password "$PASSWORD" --no-password-reset-required |
| 49 | + fi |
| 50 | + |
| 51 | + # Add user to group (idempotent - AWS doesn't error if already in group) |
| 52 | + echo "Adding $username to group $GROUP_NAME" |
| 53 | + aws iam add-user-to-group --user-name "$username" --group-name "$GROUP_NAME" |
| 54 | +done |
| 55 | + |
| 56 | +echo "Done. Created/verified $NUM_USERS users." |
0 commit comments