Automating IAM resource management in AWS is critical as teams scale. This project script handles the creation of IAM users, user groups, and permission assignment using the AWS CLI, packaged inside a reusable shell script.
- AWS CLI installed (
aws --version) - AWS CLI configured (
aws configure) - IAM permissions to manage users, groups, and policies
- Completion of Linux and shell scripting fundamentals
#!/bin/bash
# AWS IAM Manager Script for CloudOps Solutions
# Automates user, group creation, and permission assignment
IAM_USER_NAMES=("user1" "user2" "user3" "user4" "user5")
create_iam_users() {
echo "Starting IAM user creation..."
for user in "${IAM_USER_NAMES[@]}"; do
aws iam get-user --user-name "$user" &>/dev/null
if [ $? -ne 0 ]; then
aws iam create-user --user-name "$user" && echo "Created $user"
else
echo "User $user already exists"
fi
done
}
create_admin_group() {
echo "Creating admin group and attaching policy..."
aws iam get-group --group-name "admin" &>/dev/null
if [ $? -ne 0 ]; then
aws iam create-group --group-name "admin" && echo "Group created"
else
echo "Group already exists"
fi
aws iam attach-group-policy \
--group-name admin \
--policy-arn arn:aws:iam::aws:policy/AdministratorAccess
}
add_users_to_admin_group() {
echo "Adding users to admin group..."
for user in "${IAM_USER_NAMES[@]}"; do
aws iam add-user-to-group \
--user-name "$user" \
--group-name admin
echo "Added $user to admin group"
done
}
main() {
echo "Starting AWS IAM automation..."
if ! command -v aws &>/dev/null; then
echo "Error: AWS CLI not installed."
exit 1
fi
create_iam_users
create_admin_group
add_users_to_admin_group
echo "✅ IAM automation complete"
}
mainThe extented script can be found here AWS IAM manager
After saving the script file, make it executable and run it:
chmod +x aws-iam-manager.sh
./aws-iam-manager.shTo make the script safe and idempotent (i.e., can run multiple times without breaking), the following patterns are used:
Skip user creation if they exist:
aws iam get-user --user-name "$user" &>/dev/null || aws iam create-user --user-name "$user"Skip group creation if it already exists:
aws iam get-group --group-name admin &>/dev/null || aws iam create-group --group-name adminCapture success or failure of policy attachment:
if aws iam attach-group-policy --group-name admin \
--policy-arn arn:aws:iam::aws:policy/AdministratorAccess; then
echo "Policy attached."
else
echo "Failed to attach policy."
fiThese checks make the script safe to re-run and eliminate common failure scenarios during AWS IAM provisioning.
To troubleshoot issues in real-time:
#!/bin/bash
set -x # Enables debuggingDisable debugging when needed:
set +x # Stops command tracingThis traces each line as it's executed—great for identifying incorrect arguments or failed commands in AWS CLI calls.
| Problem | Cause | Solution |
|---|---|---|
aws: command not found |
AWS CLI not installed | Install using: sudo apt install awscli |
Unable to locate credentials |
AWS CLI not configured | Run: aws configure |
EntityAlreadyExists errors |
User or group already exists | Script handles this—outputs message and skips |
AccessDenied errors |
Missing IAM permissions | Attach IAM full-access policy to the AWS CLI user |
Always verify the existence of users or groups using:
aws iam get-user --user-name "user1"Avoids script failure when entities already exist.
The array structure makes bulk user creation and management clean and repeatable:
IAM_USER_NAMES=("user1" "user2" "user3")#!/bin/bash- Guarantees Bash shell execution.
- Promotes portability across environments where
/bin/bashis available. - Avoids ambiguous behavior with system default shells.
Enable line-by-line execution tracing:
set -x # Start debugging
# your logic
set +x # Stop debuggingHelps in identifying which line fails during script execution.
-
✅ Fully documented script:
aws-iam-manager.sh -
✅ Image evidence of:
- AWS CLI installed & configured
- Script execution and IAM user/group creation
- Policy attachment and group assignment
-
✅ GitHub or cloud link to script for review
This project extends basic shell scripting into real-world AWS IAM automation using arrays, loops, functions, and CLI integration. The script is reusable, handles errors gracefully, and promotes safe cloud operations at scale.



