Skip to content

Commit 8376c4c

Browse files
committed
Add create users script
1 parent 4983f44 commit 8376c4c

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

.cleanup/cloud-nuke.yml renamed to .admin/cloud-nuke.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
# Cleanup config to be run with https://github.com/gruntwork-io/cloud-nuke
22
# Command:
3-
# AWS_PROFILE=bespinian-serverless-workshop cloud-nuke aws --config .cleanup/cloud-nuke.yml
3+
# AWS_PROFILE=bespinian-serverless-workshop cloud-nuke aws --config .admin/cloud-nuke.yml
44

5-
IAMUsers:
6-
exclude:
7-
names_regex:
8-
- -bespinian$
95
IAMGroups:
106
exclude:
117
names_regex:
128
- ^participants$
13-
- ^facilitators$
149
IAMRoles:
1510
exclude:
1611
names_regex:

.admin/create-users.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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

Comments
 (0)