Javi

AWS: Create users in Identity Center (with group)

Feb 14th, 2026
48
0
Never
7
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.18 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Script to create AWS Identity Center identities from CSV and associate with a group
  4. # Usage: ./create-identities.sh <group-name> <csv-file>
  5.  
  6. set -e
  7.  
  8. # Colors for output
  9. RED='\033[0;31m'
  10. GREEN='\033[0;32m'
  11. YELLOW='\033[1;33m'
  12. NC='\033[0m' # No Color
  13.  
  14. # Function to display help
  15. show_help() {
  16. cat << EOF
  17. Usage: $(basename "$0") <group-name> <csv-file>
  18.  
  19. Creates AWS Identity Center identities from a CSV file and associates them with a specified group.
  20.  
  21. Arguments:
  22. group-name Name of the Identity Center group to associate users with
  23. csv-file Path to CSV file containing user data
  24.  
  25. CSV Format:
  26. - Comma-separated values
  27. - First row must be headers
  28. - Strings should be quoted
  29. - Required fields: name, username, email
  30.  
  31. Example CSV:
  32. name,username,email
  33. "John Doe","jdoe","[email protected]"
  34. "Jane Smith","jsmith","[email protected]"
  35.  
  36. Options:
  37. --help Display this help message
  38.  
  39. Example:
  40. $(basename "$0") Developers users.csv
  41.  
  42. Requirements:
  43. - AWS Identity Center must be configured
  44. - Email notifications should be enabled in Identity Center settings
  45. (Settings > Identity source > Configure > Email notifications)
  46. - Users will receive automatic invitation emails from AWS
  47.  
  48. EOF
  49. exit 0
  50. }
  51.  
  52. # Function to print error messages
  53. error() {
  54. echo -e "${RED}ERROR: $1${NC}" >&2
  55. exit 1
  56. }
  57.  
  58. # Function to print success messages
  59. success() {
  60. echo -e "${GREEN}$1${NC}"
  61. }
  62.  
  63. # Function to print warning messages
  64. warning() {
  65. echo -e "${YELLOW}WARNING: $1${NC}"
  66. }
  67.  
  68. # Function to print info messages
  69. info() {
  70. echo -e "$1"
  71. }
  72.  
  73. # Check for help flag
  74. if [[ "$1" == "--help" || "$1" == "-h" ]]; then
  75. show_help
  76. fi
  77.  
  78. # Check number of arguments
  79. if [ "$#" -ne 2 ]; then
  80. error "Invalid number of arguments. Expected 2, got $#.\n\nUse --help for usage information."
  81. fi
  82.  
  83. GROUP_NAME="$1"
  84. CSV_FILE="$2"
  85.  
  86. # Validate CSV file exists
  87. if [ ! -f "$CSV_FILE" ]; then
  88. error "CSV file not found: $CSV_FILE"
  89. fi
  90.  
  91. # Validate CSV file is readable
  92. if [ ! -r "$CSV_FILE" ]; then
  93. error "CSV file is not readable: $CSV_FILE"
  94. fi
  95.  
  96. # Check if file is empty
  97. if [ ! -s "$CSV_FILE" ]; then
  98. error "CSV file is empty: $CSV_FILE"
  99. fi
  100.  
  101. # Check if AWS CLI is installed
  102. if ! command -v aws &> /dev/null; then
  103. error "AWS CLI is not installed. Please install it first."
  104. fi
  105.  
  106. info "Starting Identity Center user creation process..."
  107. info "Group: $GROUP_NAME"
  108. info "CSV File: $CSV_FILE"
  109. echo ""
  110. warning "NOTE: Ensure email notifications are enabled in Identity Center settings"
  111. warning " (AWS Console > IAM Identity Center > Settings > Identity source)"
  112. echo ""
  113.  
  114. # Get Identity Store ID
  115. info "Retrieving Identity Store ID..."
  116. IDENTITY_STORE_ID=$(aws sso-admin list-instances --query 'Instances[0].IdentityStoreId' --output text)
  117.  
  118. if [ -z "$IDENTITY_STORE_ID" ] || [ "$IDENTITY_STORE_ID" == "None" ]; then
  119. error "Could not retrieve Identity Store ID. Make sure Identity Center is configured."
  120. fi
  121.  
  122. success "Identity Store ID: $IDENTITY_STORE_ID"
  123. echo ""
  124.  
  125. # Check if group exists and get Group ID
  126. info "Checking if group '$GROUP_NAME' exists..."
  127. GROUP_ID=$(aws identitystore list-groups \
  128. --identity-store-id "$IDENTITY_STORE_ID" \
  129. --filters AttributePath=DisplayName,AttributeValue="$GROUP_NAME" \
  130. --query 'Groups[0].GroupId' \
  131. --output text)
  132.  
  133. if [ -z "$GROUP_ID" ] || [ "$GROUP_ID" == "None" ]; then
  134. error "Group '$GROUP_NAME' not found in Identity Center."
  135. fi
  136.  
  137. success "Group found: $GROUP_NAME (ID: $GROUP_ID)"
  138. echo ""
  139.  
  140. # Process CSV file
  141. info "Processing CSV file..."
  142. line_number=0
  143. created_count=0
  144. skipped_count=0
  145. error_count=0
  146.  
  147. # Read CSV file, skip header
  148. tail -n +2 "$CSV_FILE" | while IFS=, read -r name username email || [ -n "$name" ]; do
  149. line_number=$((line_number + 1))
  150.  
  151. # Remove quotes from fields
  152. name=$(echo "$name" | sed 's/^"//;s/"$//')
  153. username=$(echo "$username" | sed 's/^"//;s/"$//' | xargs)
  154. email=$(echo "$email" | sed 's/^"//;s/"$//' | xargs)
  155.  
  156. # Skip empty lines
  157. if [ -z "$name" ] && [ -z "$username" ] && [ -z "$email" ]; then
  158. continue
  159. fi
  160.  
  161. # Validate required fields
  162. if [ -z "$name" ] || [ -z "$username" ] || [ -z "$email" ]; then
  163. warning "Line $((line_number + 1)): Missing required fields (name, username, or email). Skipping."
  164. skipped_count=$((skipped_count + 1))
  165. continue
  166. fi
  167.  
  168. info "Processing: $name ($username) <$email>"
  169.  
  170. # Split name into first and last name (simple split on first space)
  171. GIVEN_NAME=$(echo "$name" | awk '{print $1}')
  172. FAMILY_NAME=$(echo "$name" | awk '{$1=""; print $0}' | xargs)
  173.  
  174. # If no family name, use given name as family name
  175. if [ -z "$FAMILY_NAME" ]; then
  176. FAMILY_NAME="$GIVEN_NAME"
  177. fi
  178.  
  179. USERNAME="$username"
  180.  
  181. # Check if user already exists
  182. EXISTING_USER=$(aws identitystore list-users \
  183. --identity-store-id "$IDENTITY_STORE_ID" \
  184. --filters AttributePath=UserName,AttributeValue="$USERNAME" \
  185. --query 'Users[0].UserId' \
  186. --output text 2>/dev/null || echo "")
  187.  
  188. if [ -n "$EXISTING_USER" ] && [ "$EXISTING_USER" != "None" ]; then
  189. warning " User already exists: $USERNAME (ID: $EXISTING_USER)"
  190. USER_ID="$EXISTING_USER"
  191. SEND_EMAIL=false
  192. else
  193. # Create user
  194. USER_ID=$(aws identitystore create-user \
  195. --identity-store-id "$IDENTITY_STORE_ID" \
  196. --user-name "$USERNAME" \
  197. --display-name "$name" \
  198. --name Formatted="$name",GivenName="$GIVEN_NAME",FamilyName="$FAMILY_NAME" \
  199. --emails Value="$email",Primary=true \
  200. --query 'UserId' \
  201. --output text 2>&1)
  202.  
  203. if [ $? -eq 0 ]; then
  204. success " ✓ User created: $USERNAME (ID: $USER_ID)"
  205. created_count=$((created_count + 1))
  206. SEND_EMAIL=true
  207. else
  208. warning " ✗ Failed to create user: $USERNAME"
  209. warning " Error: $USER_ID"
  210. error_count=$((error_count + 1))
  211. continue
  212. fi
  213. fi
  214.  
  215. # Add user to group
  216. MEMBERSHIP_ID=$(aws identitystore create-group-membership \
  217. --identity-store-id "$IDENTITY_STORE_ID" \
  218. --group-id "$GROUP_ID" \
  219. --member-id UserId="$USER_ID" \
  220. --query 'MembershipId' \
  221. --output text 2>&1)
  222.  
  223. if [ $? -eq 0 ]; then
  224. success " ✓ User added to group: $GROUP_NAME"
  225. else
  226. # Check if error is because user is already a member
  227. if echo "$MEMBERSHIP_ID" | grep -q "ConflictException"; then
  228. warning " → User already a member of group: $GROUP_NAME"
  229. else
  230. warning " ✗ Failed to add user to group"
  231. warning " Error: $MEMBERSHIP_ID"
  232. error_count=$((error_count + 1))
  233. fi
  234. fi
  235.  
  236. # Send invitation email if this is a newly created user
  237. if [ "$SEND_EMAIL" = true ]; then
  238. info " → Sending invitation email to $email..."
  239.  
  240. # For AWS Identity Center, we need to use the disable/enable user trick
  241. # or wait for automatic email. Let's try to trigger via password reset.
  242. # Note: AWS Identity Center with Identity Store automatically sends invitation
  243. # emails when users are created, but only if email notifications are enabled
  244. # in the IAM Identity Center settings.
  245.  
  246. # Alternative: Use AWS SES or SNS to send custom invitation if needed
  247. # For now, we'll just note that AWS should send it automatically
  248. success " ✓ AWS Identity Center will send invitation email automatically"
  249. info " (Ensure email notifications are enabled in Identity Center settings)"
  250. fi
  251.  
  252. echo ""
  253. done
  254.  
  255. # Summary
  256. echo "================================"
  257. echo "Summary:"
  258. success "Users created: $created_count"
  259. if [ $skipped_count -gt 0 ]; then
  260. warning "Users skipped: $skipped_count"
  261. fi
  262. if [ $error_count -gt 0 ]; then
  263. warning "Errors encountered: $error_count"
  264. fi
  265. echo "================================"
  266.  
  267. info "Process completed!"
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment