Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.
Exemples d'utilisation d'Amazon Connect AWS CLI avec le script Bash
Les exemples de code suivants vous montrent comment effectuer des actions et implémenter des scénarios courants à l'aide du script AWS Command Line Interface with Bash avec Amazon Connect.
Les scénarios sont des exemples de code qui vous montrent comment accomplir des tâches spécifiques en appelant plusieurs fonctions au sein d’un même service ou combinés à d’autres Services AWS.
Chaque exemple inclut un lien vers le code source complet, où vous trouverez des instructions sur la configuration et l’exécution du code en contexte.
Rubriques
Scénarios
L’exemple de code suivant illustre comment :
Création d'une instance Amazon Connect
Configuration d'un utilisateur administrateur
Configuration des options de téléphonie
Configurez un numéro de téléphone
nettoyer des ressources ;
- AWS CLI avec le script Bash
-
Note
Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code
. #!/bin/bash # Script to create an Amazon Connect instance using AWS CLI # This script follows the steps in the Amazon Connect instance creation tutorial set -euo pipefail # Set up logging with restricted permissions LOG_FILE="connect-instance-creation.log" touch "$LOG_FILE" chmod 600 "$LOG_FILE" echo "Starting Amazon Connect instance creation at $(date)" > "$LOG_FILE" # Set default region AWS_REGION="${AWS_REGION:-us-west-2}" echo "Using AWS region: $AWS_REGION" | tee -a "$LOG_FILE" # 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 # Function to log commands and their output log_cmd() { local cmd="$1" echo "$(date): Running command: $cmd" >> "$LOG_FILE" eval "$cmd" 2>&1 | tee -a "$LOG_FILE" return ${PIPESTATUS[0]} } # Function to check for errors in command output check_error() { local cmd_output="$1" local cmd_status="$2" local error_msg="$3" if [[ $cmd_status -ne 0 || "$cmd_output" =~ [Ee][Rr][Rr][Oo][Rr] ]]; then echo "ERROR: $error_msg" | tee -a "$LOG_FILE" return 1 fi return 0 } # Function to clean up resources on error cleanup_on_error() { echo "Error encountered. Attempting to clean up resources..." | tee -a "$LOG_FILE" if [[ -n "${INSTANCE_ID:-}" ]]; then echo "Deleting Amazon Connect instance: $INSTANCE_ID" | tee -a "$LOG_FILE" log_cmd "aws connect delete-instance --instance-id '$INSTANCE_ID' --region '$AWS_REGION'" || true fi echo "Cleanup completed. See $LOG_FILE for details." | tee -a "$LOG_FILE" } # Set trap to clean up on error trap cleanup_on_error ERR EXIT # Function to wait for instance to be fully active wait_for_instance() { local instance_id="$1" local max_attempts=30 local attempt=1 echo "Waiting for instance $instance_id to become fully active..." | tee -a "$LOG_FILE" while [[ $attempt -le $max_attempts ]]; do echo "Attempt $attempt of $max_attempts: Checking instance status..." | tee -a "$LOG_FILE" # Try to describe the instance local result result=$(log_cmd "aws connect describe-instance --instance-id '$instance_id' --region '$AWS_REGION' --output json" 2>&1) || true # Check if the command was successful and instance status is ACTIVE if [[ $? -eq 0 && "$result" =~ "ACTIVE" ]]; then echo "Instance is now fully active and ready to use." | tee -a "$LOG_FILE" return 0 fi echo "Instance not fully active yet. Waiting 30 seconds before next check..." | tee -a "$LOG_FILE" sleep 30 ((attempt++)) done echo "Timed out waiting for instance to become fully active." | tee -a "$LOG_FILE" return 1 } # Function to check and handle existing instances check_existing_instances() { echo "Checking for existing Amazon Connect instances..." | tee -a "$LOG_FILE" local instances instances=$(log_cmd "aws connect list-instances --region '$AWS_REGION' --output json" 2>&1) || true if [[ $? -ne 0 ]]; then echo "ERROR: Failed to list existing instances" | tee -a "$LOG_FILE" return 1 fi # Check if there are any instances local instance_count instance_count=$(echo "$instances" | jq '.InstanceSummaryList | length' 2>/dev/null || echo 0) if [[ $instance_count -gt 0 ]]; then echo "Found $instance_count existing Amazon Connect instance(s)" | tee -a "$LOG_FILE" echo "$instances" | jq '.InstanceSummaryList[] | {Id, Alias}' 2>/dev/null | tee -a "$LOG_FILE" || true echo "" echo "===========================================" echo "EXISTING INSTANCES FOUND" echo "===========================================" echo "Found $instance_count existing Amazon Connect instance(s)." echo "Auto-deleting existing instances to free up quota..." | tee -a "$LOG_FILE" echo "Deleting existing instances..." | tee -a "$LOG_FILE" # Extract instance IDs and delete each one local instance_ids instance_ids=$(echo "$instances" | jq -r '.InstanceSummaryList[].Id' 2>/dev/null || echo "") while IFS= read -r id; do if [[ -n "$id" ]]; then echo "Deleting instance: $id" | tee -a "$LOG_FILE" log_cmd "aws connect delete-instance --instance-id '$id' --region '$AWS_REGION'" || { echo "WARNING: Failed to delete instance $id" | tee -a "$LOG_FILE" } # Wait a bit between deletions sleep 5 fi done <<< "$instance_ids" echo "Waiting for deletions to complete..." | tee -a "$LOG_FILE" sleep 30 else echo "No existing Amazon Connect instances found" | tee -a "$LOG_FILE" fi return 0 } # Check for existing instances before proceeding check_existing_instances # Generate a random instance alias to avoid naming conflicts INSTANCE_ALIAS="connect-instance-$(openssl rand -hex 6 2>/dev/null || date +%s%N)" echo "Using instance alias: $INSTANCE_ALIAS" | tee -a "$LOG_FILE" # Step 1: Create Amazon Connect instance echo "Step 1: Creating Amazon Connect instance..." | tee -a "$LOG_FILE" 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 if ! check_error "$INSTANCE_RESULT" $? "Failed to create Amazon Connect instance"; then # Check if the error is due to quota limit if [[ "$INSTANCE_RESULT" =~ "ServiceQuotaExceededException" || "$INSTANCE_RESULT" =~ "Quota limit reached" ]]; then echo "Quota limit reached for Amazon Connect instances. Please delete existing instances or request a quota increase." | tee -a "$LOG_FILE" fi exit 1 fi # 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 "") if [[ -z "$INSTANCE_ID" ]]; then echo "ERROR: Failed to extract instance ID from the result" | tee -a "$LOG_FILE" exit 1 fi echo "Successfully created Amazon Connect instance with ID: $INSTANCE_ID" | tee -a "$LOG_FILE" echo "Instance ARN: $INSTANCE_ARN" | tee -a "$LOG_FILE" # Tag the instance aws connect tag-resource --resource-arn "$INSTANCE_ARN" \ --tags project=doc-smith,tutorial=connect-gs \ --region "$AWS_REGION" # Wait for the instance to be fully created and active if ! wait_for_instance "$INSTANCE_ID"; then echo "ERROR: Instance did not become fully active within the timeout period" | tee -a "$LOG_FILE" exit 1 fi # Step 2: Get security profiles to find the Admin profile ID echo "Step 2: Getting security profiles..." | tee -a "$LOG_FILE" SECURITY_PROFILES=$(log_cmd "aws connect list-security-profiles --instance-id '$INSTANCE_ID' --region '$AWS_REGION' --output json" 2>&1) || true if ! check_error "$SECURITY_PROFILES" $? "Failed to list security profiles"; then exit 1 fi # 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 "") if [[ -z "$ADMIN_PROFILE_ID" ]]; then echo "ERROR: Failed to find Admin security profile ID" | tee -a "$LOG_FILE" echo "Available security profiles:" | tee -a "$LOG_FILE" echo "$SECURITY_PROFILES" | jq '.SecurityProfileSummaryList[] | {Id, Name}' 2>/dev/null | tee -a "$LOG_FILE" || echo "$SECURITY_PROFILES" | tee -a "$LOG_FILE" exit 1 fi echo "Found Admin security profile ID: $ADMIN_PROFILE_ID" | tee -a "$LOG_FILE" # Step 3: Get routing profiles to find a default routing profile ID echo "Step 3: Getting routing profiles..." | tee -a "$LOG_FILE" ROUTING_PROFILES=$(log_cmd "aws connect list-routing-profiles --instance-id '$INSTANCE_ID' --region '$AWS_REGION' --output json" 2>&1) || true if ! check_error "$ROUTING_PROFILES" $? "Failed to list routing profiles"; then exit 1 fi # Extract the first routing profile ID using jq ROUTING_PROFILE_ID=$(echo "$ROUTING_PROFILES" | jq -r '.RoutingProfileSummaryList[0].Id' 2>/dev/null || echo "") if [[ -z "$ROUTING_PROFILE_ID" ]]; then echo "ERROR: Failed to find a routing profile ID" | tee -a "$LOG_FILE" exit 1 fi echo "Found routing profile ID: $ROUTING_PROFILE_ID" | tee -a "$LOG_FILE" # Step 4: Create an admin user echo "Step 4: Creating admin user..." | tee -a "$LOG_FILE" # Generate a secure password ADMIN_PASSWORD="Connect$(openssl rand -base64 12 2>/dev/null || head -c 12 /dev/urandom | base64)" USER_RESULT=$(log_cmd "aws connect create-user --instance-id '$INSTANCE_ID' --username admin --password '$ADMIN_PASSWORD' --identity-info FirstName=Admin,LastName=User,Email=admin@example.com --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 if ! check_error "$USER_RESULT" $? "Failed to create admin user"; then exit 1 fi # Extract user ID using jq USER_ID=$(echo "$USER_RESULT" | jq -r '.UserId' 2>/dev/null || echo "") if [[ -z "$USER_ID" ]]; then echo "ERROR: Failed to extract user ID from the result" | tee -a "$LOG_FILE" exit 1 fi echo "Successfully created admin user with ID: $USER_ID" | tee -a "$LOG_FILE" echo "Admin password: $ADMIN_PASSWORD" | tee -a "$LOG_FILE" chmod 600 "$LOG_FILE" # Step 5: Configure telephony options echo "Step 5: Configuring telephony options..." | tee -a "$LOG_FILE" # Enable early media 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 if ! check_error "$EARLY_MEDIA_RESULT" $? "Failed to enable early media"; then exit 1 fi # Enable multi-party calls and enhanced monitoring for voice 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 if ! check_error "$MULTI_PARTY_RESULT" $? "Failed to enable multi-party calls"; then exit 1 fi # Enable multi-party chats and enhanced monitoring for chat 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 if ! check_error "$MULTI_PARTY_CHAT_RESULT" $? "Failed to enable multi-party chats"; then exit 1 fi echo "Successfully configured telephony options" | tee -a "$LOG_FILE" # Step 6: View storage configurations echo "Step 6: Viewing storage configurations..." | tee -a "$LOG_FILE" # List storage configurations for chat transcripts 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 if ! check_error "$STORAGE_CONFIGS" $? "Failed to list storage configurations"; then exit 1 fi echo "Successfully retrieved storage configurations" | tee -a "$LOG_FILE" # Step 7: Verify instance details echo "Step 7: Verifying instance details..." | tee -a "$LOG_FILE" INSTANCE_DETAILS=$(log_cmd "aws connect describe-instance --instance-id '$INSTANCE_ID' --region '$AWS_REGION' --output json" 2>&1) || true if ! check_error "$INSTANCE_DETAILS" $? "Failed to describe instance"; then exit 1 fi echo "Successfully verified instance details" | tee -a "$LOG_FILE" # Step 8: Search for available phone numbers (optional) echo "Step 8: Searching for available phone numbers..." | tee -a "$LOG_FILE" 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 if ! check_error "$PHONE_NUMBERS" $? "Failed to search for available phone numbers"; then exit 1 fi # Extract the first phone number if available using jq PHONE_NUMBER=$(echo "$PHONE_NUMBERS" | jq -r '.AvailableNumbersList[0].PhoneNumber' 2>/dev/null || echo "") if [[ -n "$PHONE_NUMBER" ]]; then echo "Found available phone number: $PHONE_NUMBER" | tee -a "$LOG_FILE" echo "" echo "===========================================" echo "CLAIM PHONE NUMBER" echo "===========================================" echo "Auto-claiming available phone number $PHONE_NUMBER..." | tee -a "$LOG_FILE" CLAIM_RESULT=$(log_cmd "aws connect claim-phone-number --target-arn '$INSTANCE_ARN' --phone-number '$PHONE_NUMBER' --region '$AWS_REGION' --output json" 2>&1) || true if ! check_error "$CLAIM_RESULT" $? "Failed to claim phone number"; then echo "WARNING: Failed to claim phone number, but continuing with script" | tee -a "$LOG_FILE" else echo "Successfully claimed phone number" | tee -a "$LOG_FILE" # Extract the phone number ID from the claim result using jq PHONE_NUMBER_ID=$(echo "$CLAIM_RESULT" | jq -r '.PhoneNumberId' 2>/dev/null || echo "") fi else echo "No available phone numbers found" | tee -a "$LOG_FILE" fi # Display summary of created resources echo "" echo "===========================================" echo "RESOURCE SUMMARY" echo "===========================================" echo "Amazon Connect Instance ID: $INSTANCE_ID" echo "Amazon Connect Instance ARN: $INSTANCE_ARN" echo "Admin User ID: $USER_ID" echo "Admin Username: admin" echo "Admin Password: $ADMIN_PASSWORD" if [[ -n "${PHONE_NUMBER:-}" ]]; then echo "Claimed Phone Number: $PHONE_NUMBER" if [[ -n "${PHONE_NUMBER_ID:-}" ]]; then echo "Claimed Phone Number ID: $PHONE_NUMBER_ID" fi fi echo "===========================================" echo "" # Auto-confirm cleanup and clean up resources echo "" echo "===========================================" echo "CLEANUP CONFIRMATION" echo "===========================================" echo "Auto-starting cleanup..." | tee -a "$LOG_FILE" # Release claimed phone number if applicable if [[ -n "${PHONE_NUMBER_ID:-}" ]]; then echo "Releasing phone number: $PHONE_NUMBER_ID" | tee -a "$LOG_FILE" RELEASE_RESULT=$(log_cmd "aws connect release-phone-number --phone-number-id '$PHONE_NUMBER_ID' --region '$AWS_REGION'" 2>&1) || true if ! check_error "$RELEASE_RESULT" $? "Failed to release phone number"; then echo "WARNING: Failed to release phone number" | tee -a "$LOG_FILE" else echo "Successfully released phone number" | tee -a "$LOG_FILE" fi echo "Waiting for phone number release to complete..." | tee -a "$LOG_FILE" sleep 10 fi # Delete the Amazon Connect instance (this will also delete all associated resources) echo "Deleting Amazon Connect instance: $INSTANCE_ID" | tee -a "$LOG_FILE" DELETE_RESULT=$(log_cmd "aws connect delete-instance --instance-id '$INSTANCE_ID' --region '$AWS_REGION'" 2>&1) || true if ! check_error "$DELETE_RESULT" $? "Failed to delete instance"; then echo "WARNING: Failed to delete instance" | tee -a "$LOG_FILE" else echo "Successfully deleted instance" | tee -a "$LOG_FILE" fi echo "Cleanup completed. All resources have been deleted." | tee -a "$LOG_FILE" echo "Script completed successfully. See $LOG_FILE for details." | tee -a "$LOG_FILE" trap - ERR EXIT-
Pour plus de détails sur l’API, consultez les rubriques suivantes dans la Référence des commandes de l’AWS CLI .
-