Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Script to create AWS Identity Center identities from CSV and associate with a group
- # Usage: ./create-identities.sh <group-name> <csv-file>
- set -e
- # Colors for output
- RED='\033[0;31m'
- GREEN='\033[0;32m'
- YELLOW='\033[1;33m'
- NC='\033[0m' # No Color
- # Function to display help
- show_help() {
- cat << EOF
- Usage: $(basename "$0") <group-name> <csv-file>
- Creates AWS Identity Center identities from a CSV file and associates them with a specified group.
- Arguments:
- group-name Name of the Identity Center group to associate users with
- csv-file Path to CSV file containing user data
- CSV Format:
- - Comma-separated values
- - First row must be headers
- - Strings should be quoted
- - Required fields: name, username, email
- Example CSV:
- name,username,email
- "John Doe","jdoe","[email protected]"
- "Jane Smith","jsmith","[email protected]"
- Options:
- --help Display this help message
- Example:
- $(basename "$0") Developers users.csv
- Requirements:
- - AWS Identity Center must be configured
- - Email notifications should be enabled in Identity Center settings
- (Settings > Identity source > Configure > Email notifications)
- - Users will receive automatic invitation emails from AWS
- EOF
- exit 0
- }
- # Function to print error messages
- error() {
- echo -e "${RED}ERROR: $1${NC}" >&2
- exit 1
- }
- # Function to print success messages
- success() {
- echo -e "${GREEN}$1${NC}"
- }
- # Function to print warning messages
- warning() {
- echo -e "${YELLOW}WARNING: $1${NC}"
- }
- # Function to print info messages
- info() {
- echo -e "$1"
- }
- # Check for help flag
- if [[ "$1" == "--help" || "$1" == "-h" ]]; then
- show_help
- fi
- # Check number of arguments
- if [ "$#" -ne 2 ]; then
- error "Invalid number of arguments. Expected 2, got $#.\n\nUse --help for usage information."
- fi
- GROUP_NAME="$1"
- CSV_FILE="$2"
- # Validate CSV file exists
- if [ ! -f "$CSV_FILE" ]; then
- error "CSV file not found: $CSV_FILE"
- fi
- # Validate CSV file is readable
- if [ ! -r "$CSV_FILE" ]; then
- error "CSV file is not readable: $CSV_FILE"
- fi
- # Check if file is empty
- if [ ! -s "$CSV_FILE" ]; then
- error "CSV file is empty: $CSV_FILE"
- fi
- # Check if AWS CLI is installed
- if ! command -v aws &> /dev/null; then
- error "AWS CLI is not installed. Please install it first."
- fi
- info "Starting Identity Center user creation process..."
- info "Group: $GROUP_NAME"
- info "CSV File: $CSV_FILE"
- echo ""
- warning "NOTE: Ensure email notifications are enabled in Identity Center settings"
- warning " (AWS Console > IAM Identity Center > Settings > Identity source)"
- echo ""
- # Get Identity Store ID
- info "Retrieving Identity Store ID..."
- IDENTITY_STORE_ID=$(aws sso-admin list-instances --query 'Instances[0].IdentityStoreId' --output text)
- if [ -z "$IDENTITY_STORE_ID" ] || [ "$IDENTITY_STORE_ID" == "None" ]; then
- error "Could not retrieve Identity Store ID. Make sure Identity Center is configured."
- fi
- success "Identity Store ID: $IDENTITY_STORE_ID"
- echo ""
- # Check if group exists and get Group ID
- info "Checking if group '$GROUP_NAME' exists..."
- GROUP_ID=$(aws identitystore list-groups \
- --identity-store-id "$IDENTITY_STORE_ID" \
- --filters AttributePath=DisplayName,AttributeValue="$GROUP_NAME" \
- --query 'Groups[0].GroupId' \
- --output text)
- if [ -z "$GROUP_ID" ] || [ "$GROUP_ID" == "None" ]; then
- error "Group '$GROUP_NAME' not found in Identity Center."
- fi
- success "Group found: $GROUP_NAME (ID: $GROUP_ID)"
- echo ""
- # Process CSV file
- info "Processing CSV file..."
- line_number=0
- created_count=0
- skipped_count=0
- error_count=0
- # Read CSV file, skip header
- tail -n +2 "$CSV_FILE" | while IFS=, read -r name username email || [ -n "$name" ]; do
- line_number=$((line_number + 1))
- # Remove quotes from fields
- name=$(echo "$name" | sed 's/^"//;s/"$//')
- username=$(echo "$username" | sed 's/^"//;s/"$//' | xargs)
- email=$(echo "$email" | sed 's/^"//;s/"$//' | xargs)
- # Skip empty lines
- if [ -z "$name" ] && [ -z "$username" ] && [ -z "$email" ]; then
- continue
- fi
- # Validate required fields
- if [ -z "$name" ] || [ -z "$username" ] || [ -z "$email" ]; then
- warning "Line $((line_number + 1)): Missing required fields (name, username, or email). Skipping."
- skipped_count=$((skipped_count + 1))
- continue
- fi
- info "Processing: $name ($username) <$email>"
- # Split name into first and last name (simple split on first space)
- GIVEN_NAME=$(echo "$name" | awk '{print $1}')
- FAMILY_NAME=$(echo "$name" | awk '{$1=""; print $0}' | xargs)
- # If no family name, use given name as family name
- if [ -z "$FAMILY_NAME" ]; then
- FAMILY_NAME="$GIVEN_NAME"
- fi
- USERNAME="$username"
- # Check if user already exists
- EXISTING_USER=$(aws identitystore list-users \
- --identity-store-id "$IDENTITY_STORE_ID" \
- --filters AttributePath=UserName,AttributeValue="$USERNAME" \
- --query 'Users[0].UserId' \
- --output text 2>/dev/null || echo "")
- if [ -n "$EXISTING_USER" ] && [ "$EXISTING_USER" != "None" ]; then
- warning " User already exists: $USERNAME (ID: $EXISTING_USER)"
- USER_ID="$EXISTING_USER"
- SEND_EMAIL=false
- else
- # Create user
- USER_ID=$(aws identitystore create-user \
- --identity-store-id "$IDENTITY_STORE_ID" \
- --user-name "$USERNAME" \
- --display-name "$name" \
- --name Formatted="$name",GivenName="$GIVEN_NAME",FamilyName="$FAMILY_NAME" \
- --emails Value="$email",Primary=true \
- --query 'UserId' \
- --output text 2>&1)
- if [ $? -eq 0 ]; then
- success " ✓ User created: $USERNAME (ID: $USER_ID)"
- created_count=$((created_count + 1))
- SEND_EMAIL=true
- else
- warning " ✗ Failed to create user: $USERNAME"
- warning " Error: $USER_ID"
- error_count=$((error_count + 1))
- continue
- fi
- fi
- # Add user to group
- MEMBERSHIP_ID=$(aws identitystore create-group-membership \
- --identity-store-id "$IDENTITY_STORE_ID" \
- --group-id "$GROUP_ID" \
- --member-id UserId="$USER_ID" \
- --query 'MembershipId' \
- --output text 2>&1)
- if [ $? -eq 0 ]; then
- success " ✓ User added to group: $GROUP_NAME"
- else
- # Check if error is because user is already a member
- if echo "$MEMBERSHIP_ID" | grep -q "ConflictException"; then
- warning " → User already a member of group: $GROUP_NAME"
- else
- warning " ✗ Failed to add user to group"
- warning " Error: $MEMBERSHIP_ID"
- error_count=$((error_count + 1))
- fi
- fi
- # Send invitation email if this is a newly created user
- if [ "$SEND_EMAIL" = true ]; then
- info " → Sending invitation email to $email..."
- # For AWS Identity Center, we need to use the disable/enable user trick
- # or wait for automatic email. Let's try to trigger via password reset.
- # Note: AWS Identity Center with Identity Store automatically sends invitation
- # emails when users are created, but only if email notifications are enabled
- # in the IAM Identity Center settings.
- # Alternative: Use AWS SES or SNS to send custom invitation if needed
- # For now, we'll just note that AWS should send it automatically
- success " ✓ AWS Identity Center will send invitation email automatically"
- info " (Ensure email notifications are enabled in Identity Center settings)"
- fi
- echo ""
- done
- # Summary
- echo "================================"
- echo "Summary:"
- success "Users created: $created_count"
- if [ $skipped_count -gt 0 ]; then
- warning "Users skipped: $skipped_count"
- fi
- if [ $error_count -gt 0 ]; then
- warning "Errors encountered: $error_count"
- fi
- echo "================================"
- info "Process completed!"
Advertisement