AWS Security ChangesHomeSearch

AWS code-library medium security documentation change

Service: code-library · 2026-05-01 · Security-related medium

File: code-library/latest/ug/bash_2_connect_code_examples.md

Summary

Updated AWS Connect bash script to improve security and reliability: added strict error handling, restricted log file permissions, AWS CLI validation, JSON parsing with jq, automatic resource cleanup, and secure credential handling.

Security assessment

Added chmod 600 for log files to prevent unauthorized access to sensitive data like passwords and credentials. Implemented automatic cleanup of AWS resources using ERR/EXIT traps to prevent resource leakage. These changes directly address security risks of accidental credential exposure and orphaned cloud resources.

Diff

diff --git a/code-library/latest/ug/bash_2_connect_code_examples.md b/code-library/latest/ug/bash_2_connect_code_examples.md
index 2023dd375..bbe5cdf17 100644
--- a//code-library/latest/ug/bash_2_connect_code_examples.md
+++ b//code-library/latest/ug/bash_2_connect_code_examples.md
@@ -56 +56,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    # Set up logging
+    set -euo pipefail
+    
+    # Set up logging with restricted permissions
@@ -57,0 +60,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+    touch "$LOG_FILE"
+    chmod 600 "$LOG_FILE"
@@ -61 +65 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    AWS_REGION="us-west-2"
+    AWS_REGION="${AWS_REGION:-us-west-2}"
@@ -63,0 +68,11 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+    # Validate AWS CLI is installed and credentials are available
+    if ! command -v aws &> /dev/null; then
+        echo "ERROR: AWS CLI is not installed" | tee -a "$LOG_FILE"
+        exit 1
+    fi
+    
+    if ! aws sts get-caller-identity &> /dev/null; then
+        echo "ERROR: AWS credentials are not configured or invalid" | tee -a "$LOG_FILE"
+        exit 1
+    fi
+    
@@ -66,2 +81,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        echo "$(date): Running command: $1" >> "$LOG_FILE"
-        eval "$1" 2>&1 | tee -a "$LOG_FILE"
+        local cmd="$1"
+        echo "$(date): Running command: $cmd" >> "$LOG_FILE"
+        eval "$cmd" 2>&1 | tee -a "$LOG_FILE"
@@ -88 +104 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        if [[ -n "$INSTANCE_ID" ]]; then
+        if [[ -n "${INSTANCE_ID:-}" ]]; then
@@ -90 +106 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            log_cmd "aws connect delete-instance --instance-id $INSTANCE_ID --region $AWS_REGION"
+            log_cmd "aws connect delete-instance --instance-id '$INSTANCE_ID' --region '$AWS_REGION'" || true
@@ -95,0 +112,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+    # Set trap to clean up on error
+    trap cleanup_on_error ERR EXIT
+    
@@ -108 +127,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            local result=$(log_cmd "aws connect describe-instance --instance-id $instance_id --region $AWS_REGION --output json")
+            local result
+            result=$(log_cmd "aws connect describe-instance --instance-id '$instance_id' --region '$AWS_REGION' --output json" 2>&1) || true
@@ -129 +149,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        local instances=$(log_cmd "aws connect list-instances --region $AWS_REGION --output json")
+        local instances
+        instances=$(log_cmd "aws connect list-instances --region '$AWS_REGION' --output json" 2>&1) || true
@@ -136 +157,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        local instance_count=$(echo "$instances" | grep -o '"Id":' | wc -l)
+        local instance_count
+        instance_count=$(echo "$instances" | jq '.InstanceSummaryList | length' 2>/dev/null || echo 0)
@@ -140 +162 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            echo "$instances" | grep -A 1 '"Id":' | tee -a "$LOG_FILE"
+            echo "$instances" | jq '.InstanceSummaryList[] | {Id, Alias}' 2>/dev/null | tee -a "$LOG_FILE" || true
@@ -147,2 +169 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            echo "Do you want to delete these instances to free up quota? (y/n): "
-            read -r DELETE_CHOICE
+            echo "Auto-deleting existing instances to free up quota..." | tee -a "$LOG_FILE"
@@ -150 +170,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            if [[ "$DELETE_CHOICE" =~ ^[Yy] ]]; then
@@ -154 +174,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                local instance_ids=($(echo "$instances" | grep -o '"Id": "[^"]*' | cut -d'"' -f4))
+            local instance_ids
+            instance_ids=$(echo "$instances" | jq -r '.InstanceSummaryList[].Id' 2>/dev/null || echo "")
@@ -156 +177,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                for id in "${instance_ids[@]}"; do
+            while IFS= read -r id; do
+                if [[ -n "$id" ]]; then
@@ -158,3 +180 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                    log_cmd "aws connect delete-instance --instance-id $id --region $AWS_REGION"
-                    
-                    if [[ $? -ne 0 ]]; then
+                    log_cmd "aws connect delete-instance --instance-id '$id' --region '$AWS_REGION'" || {
@@ -162,3 +182 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                    else
-                        echo "Successfully deleted instance $id" | tee -a "$LOG_FILE"
-                    fi
+                    }
@@ -168 +186,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                done
+                fi
+            done <<< "$instance_ids"
@@ -172,3 +190,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            else
-                echo "Keeping existing instances. Script may fail if quota is reached." | tee -a "$LOG_FILE"
-            fi
@@ -186 +202 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    INSTANCE_ALIAS="connect-instance-$(openssl rand -hex 6)"
+    INSTANCE_ALIAS="connect-instance-$(openssl rand -hex 6 2>/dev/null || date +%s%N)"
@@ -191 +207 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    INSTANCE_RESULT=$(log_cmd "aws connect create-instance --identity-management-type CONNECT_MANAGED --instance-alias $INSTANCE_ALIAS --inbound-calls-enabled --outbound-calls-enabled --region $AWS_REGION --output json")
+    INSTANCE_RESULT=$(log_cmd "aws connect create-instance --identity-management-type CONNECT_MANAGED --instance-alias '$INSTANCE_ALIAS' --inbound-calls-enabled --outbound-calls-enabled --region '$AWS_REGION' --output json" 2>&1) || true
@@ -198 +213,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        cleanup_on_error
@@ -202,3 +217,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    # Extract instance ID from the result
-    INSTANCE_ID=$(echo "$INSTANCE_RESULT" | grep -o '"Id": "[^"]*' | cut -d'"' -f4)
-    INSTANCE_ARN=$(echo "$INSTANCE_RESULT" | grep -o '"Arn": "[^"]*' | cut -d'"' -f4)
+    # Extract instance ID from the result using jq
+    INSTANCE_ID=$(echo "$INSTANCE_RESULT" | jq -r '.InstanceId' 2>/dev/null || echo "")
+    INSTANCE_ARN=$(echo "$INSTANCE_RESULT" | jq -r '.InstanceArn' 2>/dev/null || echo "")
@@ -217 +231,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        cleanup_on_error
@@ -223 +237 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    SECURITY_PROFILES=$(log_cmd "aws connect list-security-profiles --instance-id $INSTANCE_ID --region $AWS_REGION --output json")
+    SECURITY_PROFILES=$(log_cmd "aws connect list-security-profiles --instance-id '$INSTANCE_ID' --region '$AWS_REGION' --output json" 2>&1) || true
@@ -226 +239,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        cleanup_on_error
@@ -230,16 +243,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    # Save security profiles to a temporary file for easier processing
-    TEMP_FILE=$(mktemp)
-    echo "$SECURITY_PROFILES" > "$TEMP_FILE"
-    
-    # Extract Admin security profile ID using grep and awk
-    ADMIN_PROFILE_ID=""
-    while IFS= read -r line; do
-        if [[ "$line" =~ \"Name\":\ \"Admin\" ]]; then
-            # Found the Admin profile, now get the ID from previous lines
-            ADMIN_PROFILE_ID=$(grep -B 2 "$line" "$TEMP_FILE" | grep -o '"Id": "[^"]*' | head -1 | cut -d'"' -f4)
-            break
-        fi
-    done < "$TEMP_FILE"
-    
-    # Clean up
-    rm -f "$TEMP_FILE"
+    # Extract Admin security profile ID using jq
+    ADMIN_PROFILE_ID=$(echo "$SECURITY_PROFILES" | jq -r '.SecurityProfileSummaryList[] | select(.Name=="Admin") | .Id' 2>/dev/null | head -1 || echo "")
@@ -250,2 +249 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        echo "$SECURITY_PROFILES" | tee -a "$LOG_FILE"
-        cleanup_on_error
+        echo "$SECURITY_PROFILES" | jq '.SecurityProfileSummaryList[] | {Id, Name}' 2>/dev/null | tee -a "$LOG_FILE" || echo "$SECURITY_PROFILES" | tee -a "$LOG_FILE"
@@ -259 +257 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    ROUTING_PROFILES=$(log_cmd "aws connect list-routing-profiles --instance-id $INSTANCE_ID --region $AWS_REGION --output json")
+    ROUTING_PROFILES=$(log_cmd "aws connect list-routing-profiles --instance-id '$INSTANCE_ID' --region '$AWS_REGION' --output json" 2>&1) || true
@@ -262 +259,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        cleanup_on_error
@@ -266,2 +263,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    # Extract the first routing profile ID
-    ROUTING_PROFILE_ID=$(echo "$ROUTING_PROFILES" | grep -o '"Id": "[^"]*' | head -1 | cut -d'"' -f4)
+    # Extract the first routing profile ID using jq
+    ROUTING_PROFILE_ID=$(echo "$ROUTING_PROFILES" | jq -r '.RoutingProfileSummaryList[0].Id' 2>/dev/null || echo "")
@@ -271 +267,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        cleanup_on_error
@@ -281 +277 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    ADMIN_PASSWORD="Connect$(openssl rand -base64 12)"
+    ADMIN_PASSWORD="Connect$(openssl rand -base64 12 2>/dev/null || head -c 12 /dev/urandom | base64)"
@@ -283 +279 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    USER_RESULT=$(log_cmd "aws connect create-user --instance-id $INSTANCE_ID --username admin --password \"$ADMIN_PASSWORD\" --identity-info FirstName=Admin,LastName=User,[email protected] --phone-config PhoneType=DESK_PHONE,AutoAccept=true,AfterContactWorkTimeLimit=30,DeskPhoneNumber=+12065550100 --security-profile-ids $ADMIN_PROFILE_ID --routing-profile-id $ROUTING_PROFILE_ID --region $AWS_REGION --output json")
+    USER_RESULT=$(log_cmd "aws connect create-user --instance-id '$INSTANCE_ID' --username admin --password '$ADMIN_PASSWORD' --identity-info FirstName=Admin,LastName=User,[email protected] --phone-config PhoneType=DESK_PHONE,AutoAccept=true,AfterContactWorkTimeLimit=30,DeskPhoneNumber=+12065550100 --security-profile-ids '$ADMIN_PROFILE_ID' --routing-profile-id '$ROUTING_PROFILE_ID' --region '$AWS_REGION' --output json" 2>&1) || true
@@ -286 +281,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        cleanup_on_error
@@ -290,2 +285,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    # Extract user ID
-    USER_ID=$(echo "$USER_RESULT" | grep -o '"UserId": "[^"]*\|"Id": "[^"]*' | head -1 | cut -d'"' -f4)
+    # Extract user ID using jq
+    USER_ID=$(echo "$USER_RESULT" | jq -r '.UserId' 2>/dev/null || echo "")
@@ -295 +289,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        cleanup_on_error
@@ -300,0 +295 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+    chmod 600 "$LOG_FILE"
@@ -306 +301 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    EARLY_MEDIA_RESULT=$(log_cmd "aws connect update-instance-attribute --instance-id $INSTANCE_ID --attribute-type EARLY_MEDIA --value true --region $AWS_REGION")
+    EARLY_MEDIA_RESULT=$(log_cmd "aws connect update-instance-attribute --instance-id '$INSTANCE_ID' --attribute-type EARLY_MEDIA --value true --region '$AWS_REGION'" 2>&1) || true
@@ -309 +303,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        cleanup_on_error
@@ -314 +308 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    MULTI_PARTY_RESULT=$(log_cmd "aws connect update-instance-attribute --instance-id $INSTANCE_ID --attribute-type MULTI_PARTY_CONFERENCE --value true --region $AWS_REGION")
+    MULTI_PARTY_RESULT=$(log_cmd "aws connect update-instance-attribute --instance-id '$INSTANCE_ID' --attribute-type MULTI_PARTY_CONFERENCE --value true --region '$AWS_REGION'" 2>&1) || true
@@ -317 +310,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        cleanup_on_error
@@ -322 +315 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    MULTI_PARTY_CHAT_RESULT=$(log_cmd "aws connect update-instance-attribute --instance-id $INSTANCE_ID --attribute-type MULTI_PARTY_CHAT_CONFERENCE --value true --region $AWS_REGION")
+    MULTI_PARTY_CHAT_RESULT=$(log_cmd "aws connect update-instance-attribute --instance-id '$INSTANCE_ID' --attribute-type MULTI_PARTY_CHAT_CONFERENCE --value true --region '$AWS_REGION'" 2>&1) || true
@@ -325 +317,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        cleanup_on_error
@@ -335 +327 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    STORAGE_CONFIGS=$(log_cmd "aws connect list-instance-storage-configs --instance-id $INSTANCE_ID --resource-type CHAT_TRANSCRIPTS --region $AWS_REGION --output json")
+    STORAGE_CONFIGS=$(log_cmd "aws connect list-instance-storage-configs --instance-id '$INSTANCE_ID' --resource-type CHAT_TRANSCRIPTS --region '$AWS_REGION' --output json" 2>&1) || true
@@ -338 +329,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        cleanup_on_error
@@ -346 +337 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    INSTANCE_DETAILS=$(log_cmd "aws connect describe-instance --instance-id $INSTANCE_ID --region $AWS_REGION --output json")
+    INSTANCE_DETAILS=$(log_cmd "aws connect describe-instance --instance-id '$INSTANCE_ID' --region '$AWS_REGION' --output json" 2>&1) || true
@@ -349 +339,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        cleanup_on_error
@@ -357 +347 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    PHONE_NUMBERS=$(log_cmd "aws connect search-available-phone-numbers --target-arn $INSTANCE_ARN --phone-number-type TOLL_FREE --phone-number-country-code US --max-results 5 --region $AWS_REGION --output json")
+    PHONE_NUMBERS=$(log_cmd "aws connect search-available-phone-numbers --target-arn '$INSTANCE_ARN' --phone-number-type TOLL_FREE --phone-number-country-code US --max-results 5 --region '$AWS_REGION' --output json" 2>&1) || true
@@ -360 +349,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        cleanup_on_error
@@ -364,2 +353,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    # Extract the first phone number if available
-    PHONE_NUMBER=$(echo "$PHONE_NUMBERS" | grep -o '"PhoneNumber": "[^"]*' | head -1 | cut -d'"' -f4)
+    # Extract the first phone number if available using jq
+    PHONE_NUMBER=$(echo "$PHONE_NUMBERS" | jq -r '.AvailableNumbersList[0].PhoneNumber' 2>/dev/null || echo "")
@@ -370 +358,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru