View a markdown version of this page

Bash 스크립트와 AWS CLI 함께를 사용하는 WorkSpaces 예제 - AWS SDK 코드 예제

Doc AWS SDK 예제 GitHub 리포지토리에서 더 많은 SDK 예제를 사용할 수 있습니다. AWS

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

Bash 스크립트와 AWS CLI 함께를 사용하는 WorkSpaces 예제

다음 코드 예제에서는 WorkSpaces에서 Bash 스크립트 AWS Command Line Interface 와 함께를 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.

시나리오는 동일한 서비스 내에서 또는 다른 AWS 서비스와 결합된 상태에서 여러 함수를 직접적으로 호출하여 특정 태스크를 수행하는 방법을 보여주는 코드 예제입니다.

각 예시에는 전체 소스 코드에 대한 링크가 포함되어 있으며, 여기에서 컨텍스트에 맞춰 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있습니다.

시나리오

다음 코드 예제에서는 다음과 같은 작업을 수행하는 방법을 보여줍니다.

  • 리전에서 WorkSpaces 가용성 확인

  • 사용 가능한 WorkSpaces 번들 나열

  • WorkSpace 만들기

  • WorkSpace 상태 확인

  • 리소스 정리

AWS CLI Bash 스크립트 사용
참고

GitHub에 더 많은 내용이 있습니다. 샘플 개발자 튜토리얼 리포지토리에서 전체 예제를 찾아보고 설정 및 실행 방법을 배워보세요.

#!/bin/bash # Script to create a WorkSpace in WorkSpaces Personal # This script follows the workflow described in the AWS documentation # https://docs.aws.amazon.com/workspaces/latest/adminguide/create-workspaces-personal.html # Set up logging LOG_FILE="workspaces_creation.log" exec > >(tee -a "$LOG_FILE") 2>&1 echo "$(date): Starting WorkSpaces creation script" echo "==============================================" # Initialize resource tracking array declare -a CREATED_RESOURCES # Function to handle errors handle_error() { echo "ERROR: $1" echo "Resources created before error:" for resource in "${CREATED_RESOURCES[@]}"; do echo " - $resource" done exit 1 } # Function to check if a command succeeded check_command() { # Check for ResourceNotFound.User error specifically if echo "$1" | grep -q "ResourceNotFound.User"; then echo "" echo "ERROR: User not found in the directory." echo "" echo "This error occurs when the specified username doesn't exist in the directory." echo "" echo "To resolve this issue:" echo "1. Ensure the user exists in the directory before creating a WorkSpace." echo "2. For Simple AD and AWS Managed Microsoft AD:" echo " - Connect to a directory-joined instance" echo " - Use Active Directory tools to create the user" echo " - See: https://docs.aws.amazon.com/workspaces/latest/adminguide/manage-users.html" echo "" echo "3. For AD Connector:" echo " - Create the user in your on-premises Active Directory" echo " - Ensure proper synchronization with the AD Connector" echo "" echo "4. Alternatively, you can use the AWS Console to create a WorkSpace," echo " which can create the user automatically in some directory types." echo "" handle_error "User '$USERNAME' not found in directory '$DIRECTORY_ID'" # Check for other errors elif echo "$1" | grep -i "error" > /dev/null; then handle_error "$1" fi } # Step 0: Select AWS region echo "" echo "==============================================" echo "AWS REGION SELECTION" echo "==============================================" echo "Enter the AWS region to use (e.g., us-east-1, us-west-2):" AWS_REGION="${AWS_REGION:-us-east-1}" if [ -z "$AWS_REGION" ]; then handle_error "Region cannot be empty" fi export AWS_DEFAULT_REGION="$AWS_REGION" echo "Using AWS region: $AWS_REGION" # Step 1: Prompt for directory ID echo "" echo "==============================================" echo "DIRECTORY SELECTION" echo "==============================================" echo "Listing available directories..." DIRECTORIES_OUTPUT=$(aws workspaces describe-workspace-directories --output json) check_command "$DIRECTORIES_OUTPUT" echo "$DIRECTORIES_OUTPUT" # Extract directory IDs and display them DIRECTORY_IDS=$(echo "$DIRECTORIES_OUTPUT" | grep -o '"DirectoryId": "[^"]*' | cut -d'"' -f4) if [ -z "$DIRECTORY_IDS" ]; then echo "No directories found. Please create a directory first using AWS Directory Service." echo "For more information, see: https://docs.aws.amazon.com/workspaces/latest/adminguide/register-deregister-directory.html" exit 1 fi echo "" echo "Available directory IDs:" echo "$DIRECTORY_IDS" echo "" echo "Enter the directory ID you want to use:" DIRECTORY_ID=$(echo "$DIRECTORY_IDS" | head -1) # Validate directory ID if ! echo "$DIRECTORY_IDS" | grep -q "$DIRECTORY_ID"; then echo "Directory ID $DIRECTORY_ID not found in the list of available directories." echo "Please check the ID and try again." exit 1 fi echo "Selected directory ID: $DIRECTORY_ID" # Step 2: Check if directory is registered with WorkSpaces echo "" echo "==============================================" echo "CHECKING DIRECTORY REGISTRATION" echo "==============================================" REGISTERED=$(echo "$DIRECTORIES_OUTPUT" | grep -A 5 "\"DirectoryId\": \"$DIRECTORY_ID\"" | grep -c "\"State\": \"REGISTERED\"") if [ "$REGISTERED" -eq 0 ]; then echo "Directory $DIRECTORY_ID is not registered with WorkSpaces. Registering now..." REGISTER_OUTPUT=$(aws workspaces register-workspace-directory --directory-id "$DIRECTORY_ID") check_command "$REGISTER_OUTPUT" echo "Directory registration initiated. This may take a few minutes." # Add to resource tracking CREATED_RESOURCES+=("Directory registration: $DIRECTORY_ID") # Wait for directory to be registered echo "Waiting for directory registration to complete..." sleep 30 # Check registration status REGISTRATION_CHECK=$(aws workspaces describe-workspace-directories --directory-ids "$DIRECTORY_ID") check_command "$REGISTRATION_CHECK" REGISTRATION_STATE=$(echo "$REGISTRATION_CHECK" | grep -o '"State": "[^"]*' | cut -d'"' -f4) if [ "$REGISTRATION_STATE" != "REGISTERED" ]; then echo "Directory registration is still in progress. Current state: $REGISTRATION_STATE" echo "Please check the AWS console for the final status." echo "You may need to wait a few minutes before proceeding." else echo "Directory successfully registered with WorkSpaces." fi else echo "Directory $DIRECTORY_ID is already registered with WorkSpaces." fi # Get directory type to provide appropriate user guidance DIRECTORY_TYPE=$(echo "$DIRECTORIES_OUTPUT" | grep -A 10 "\"DirectoryId\": \"$DIRECTORY_ID\"" | grep -o '"DirectoryType": "[^"]*' | cut -d'"' -f4) echo "Directory type: $DIRECTORY_TYPE" # Display user creation guidance based on directory type echo "" echo "==============================================" echo "USER CREATION GUIDANCE" echo "==============================================" case "$DIRECTORY_TYPE" in "SimpleAD" | "MicrosoftAD") echo "For $DIRECTORY_TYPE, users must be created using Active Directory tools." echo "1. Connect to a directory-joined EC2 instance" echo "2. Use Active Directory Users and Computers to create users" echo "3. For detailed instructions, see: https://docs.aws.amazon.com/workspaces/latest/adminguide/manage-users.html" ;; "ADConnector") echo "For AD Connector, users must exist in your on-premises Active Directory." echo "1. Create the user in your on-premises Active Directory" echo "2. Ensure the user is in an OU that is within the scope of your AD Connector" echo "3. For detailed instructions, see: https://docs.aws.amazon.com/directoryservice/latest/admin-guide/ad_connector_management.html" ;; *) echo "For this directory type, ensure users exist before creating WorkSpaces." echo "For detailed instructions, see: https://docs.aws.amazon.com/workspaces/latest/adminguide/manage-users.html" ;; esac echo "" # Step 3: List available bundles echo "" echo "==============================================" echo "BUNDLE SELECTION" echo "==============================================" echo "Listing available WorkSpace bundles..." # Get bundles with a format that's easier to parse BUNDLES_OUTPUT=$(aws workspaces describe-workspace-bundles --owner AMAZON --output text --query "Bundles[*].[BundleId,Name,ComputeType.Name,RootStorage.Capacity,UserStorage.Capacity]") check_command "$BUNDLES_OUTPUT" # Extract bundle information and display in a numbered list echo "Available bundles:" echo "-----------------" echo "NUM | BUNDLE ID | NAME | COMPUTE TYPE | ROOT STORAGE | USER STORAGE" echo "-----------------------------------------------------------------" # Create arrays to store bundle information declare -a BUNDLE_IDS declare -a BUNDLE_NAMES # Process the output to extract bundle information COUNT=1 while IFS=$'\t' read -r BUNDLE_ID BUNDLE_NAME COMPUTE_TYPE ROOT_STORAGE USER_STORAGE || [[ -n "$BUNDLE_ID" ]]; do # Store in arrays BUNDLE_IDS[$COUNT]="$BUNDLE_ID" BUNDLE_NAMES[$COUNT]="$BUNDLE_NAME" # Display with number echo "$COUNT | $BUNDLE_ID | $BUNDLE_NAME | $COMPUTE_TYPE | $ROOT_STORAGE GB | $USER_STORAGE GB" ((COUNT++)) done <<< "$BUNDLES_OUTPUT" # Prompt for selection echo "" echo "Enter the number of the bundle you want to use (1-$((COUNT-1))):" BUNDLE_SELECTION="1" # Validate selection if ! [[ "$BUNDLE_SELECTION" =~ ^[0-9]+$ ]] || [ "$BUNDLE_SELECTION" -lt 1 ] || [ "$BUNDLE_SELECTION" -ge "$COUNT" ]; then handle_error "Invalid bundle selection. Please enter a number between 1 and $((COUNT-1))." fi # Get the selected bundle ID BUNDLE_ID="${BUNDLE_IDS[$BUNDLE_SELECTION]}" BUNDLE_NAME="${BUNDLE_NAMES[$BUNDLE_SELECTION]}" echo "Selected bundle: $BUNDLE_NAME (ID: $BUNDLE_ID)" # Step 4: Prompt for username echo "" echo "==============================================" echo "USER INFORMATION" echo "==============================================" echo "Enter the username for the WorkSpace:" USERNAME="testuser-$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 8 | head -n 1)" echo "NOTE: The user must already exist in the directory for the WorkSpace creation to succeed." echo "If you're using Simple AD or AWS Managed Microsoft AD, the user must be created using Active Directory tools." echo "If you're using AD Connector, the user must exist in your on-premises Active Directory." echo "" echo "Enter the user's first name:" FIRST_NAME="Test" echo "Enter the user's last name:" LAST_NAME="User" echo "Enter the user's email address:" EMAIL="user-$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 8 | head -n 1)@example.com" # Step 5: Choose running mode echo "" echo "==============================================" echo "RUNNING MODE SELECTION" echo "==============================================" echo "Select running mode:" echo "1. AlwaysOn (billed monthly)" echo "2. AutoStop (billed hourly)" RUNNING_MODE_CHOICE="1" if [ "$RUNNING_MODE_CHOICE" = "1" ]; then RUNNING_MODE="ALWAYS_ON" AUTO_STOP_TIMEOUT="" else RUNNING_MODE="AUTO_STOP" AUTO_STOP_TIMEOUT=60 fi echo "Selected running mode: $RUNNING_MODE" # Step 6: Add tags (optional) echo "" echo "==============================================" echo "TAGS (OPTIONAL)" echo "==============================================" echo "Would you like to add tags to your WorkSpace? (y/n):" ADD_TAGS="n" TAGS_JSON="" if [ "$ADD_TAGS" = "y" ] || [ "$ADD_TAGS" = "Y" ]; then echo "Enter tag key (e.g., Department):" read -r TAG_KEY echo "Enter tag value (e.g., IT):" read -r TAG_VALUE TAGS_JSON="[{\"Key\":\"$TAG_KEY\",\"Value\":\"$TAG_VALUE\"}]" fi # Step 7: Create the WorkSpace echo "" echo "==============================================" echo "CREATING WORKSPACE" echo "==============================================" echo "Creating WorkSpace with the following parameters:" echo "Directory ID: $DIRECTORY_ID" echo "Username: $USERNAME" echo "Bundle ID: $BUNDLE_ID" echo "Running Mode: $RUNNING_MODE" if [ -n "$TAGS_JSON" ]; then echo "Tags: $TAG_KEY=$TAG_VALUE" fi # Create JSON for workspace properties if [ "$RUNNING_MODE" = "AUTO_STOP" ]; then PROPERTIES_JSON="{\"RunningMode\":\"$RUNNING_MODE\",\"RunningModeAutoStopTimeoutInMinutes\":$AUTO_STOP_TIMEOUT}" else PROPERTIES_JSON="{\"RunningMode\":\"$RUNNING_MODE\"}" fi # Create JSON for workspaces parameter WORKSPACE_JSON="{\"DirectoryId\":\"$DIRECTORY_ID\",\"UserName\":\"$USERNAME\",\"BundleId\":\"$BUNDLE_ID\",\"WorkspaceProperties\":$PROPERTIES_JSON" # Add tags if specified if [ -n "$TAGS_JSON" ]; then USER_TAGS=$(echo "$TAGS_JSON" | sed 's/^\[//;s/\]$//') WORKSPACE_JSON="$WORKSPACE_JSON,\"Tags\":[$USER_TAGS,{\"Key\":\"project\",\"Value\":\"doc-smith\"},{\"Key\":\"tutorial\",\"Value\":\"workspaces-personal\"}]" else WORKSPACE_JSON="$WORKSPACE_JSON,\"Tags\":[{\"Key\":\"project\",\"Value\":\"doc-smith\"},{\"Key\":\"tutorial\",\"Value\":\"workspaces-personal\"}]" fi # Close the JSON object WORKSPACE_JSON="$WORKSPACE_JSON}" # Construct the create-workspaces command CREATE_COMMAND="aws workspaces create-workspaces --workspaces '$WORKSPACE_JSON'" echo "Executing: $CREATE_COMMAND" CREATE_OUTPUT=$(eval "$CREATE_COMMAND") check_command "$CREATE_OUTPUT" echo "$CREATE_OUTPUT" # Extract WorkSpace ID WORKSPACE_ID=$(echo "$CREATE_OUTPUT" | grep -o '"WorkspaceId": "[^"]*' | head -1 | cut -d'"' -f4) if [ -z "$WORKSPACE_ID" ]; then handle_error "Failed to extract WorkSpace ID from creation output." fi echo "WorkSpace creation initiated. WorkSpace ID: $WORKSPACE_ID" CREATED_RESOURCES+=("WorkSpace: $WORKSPACE_ID") # Step 8: Check WorkSpace status echo "" echo "==============================================" echo "CHECKING WORKSPACE STATUS" echo "==============================================" echo "Checking status of WorkSpace $WORKSPACE_ID..." # Initial status check STATUS_OUTPUT=$(aws workspaces describe-workspaces --workspace-ids "$WORKSPACE_ID") check_command "$STATUS_OUTPUT" echo "$STATUS_OUTPUT" WORKSPACE_STATE=$(echo "$STATUS_OUTPUT" | grep -o '"State": "[^"]*' | head -1 | cut -d'"' -f4) echo "Current WorkSpace state: $WORKSPACE_STATE" # Wait for WorkSpace to be available (this can take 20+ minutes) echo "" echo "WorkSpace creation is in progress. This can take 20+ minutes." echo "The script will check the status every 60 seconds." echo "Press Ctrl+C to exit the script at any time. The WorkSpace will continue to be created." while [ "$WORKSPACE_STATE" = "PENDING" ]; do echo "$(date): WorkSpace state is still PENDING. Waiting 60 seconds before checking again..." sleep 60 STATUS_OUTPUT=$(aws workspaces describe-workspaces --workspace-ids "$WORKSPACE_ID") check_command "$STATUS_OUTPUT" WORKSPACE_STATE=$(echo "$STATUS_OUTPUT" | grep -o '"State": "[^"]*' | head -1 | cut -d'"' -f4) echo "$(date): Current WorkSpace state: $WORKSPACE_STATE" # If state is ERROR or UNHEALTHY, exit if [ "$WORKSPACE_STATE" = "ERROR" ] || [ "$WORKSPACE_STATE" = "UNHEALTHY" ]; then handle_error "WorkSpace creation failed. Final state: $WORKSPACE_STATE" fi # If state is AVAILABLE, break the loop if [ "$WORKSPACE_STATE" = "AVAILABLE" ]; then break fi done # Step 9: Display WorkSpace information echo "" echo "==============================================" echo "WORKSPACE CREATION COMPLETE" echo "==============================================" echo "WorkSpace has been successfully created!" echo "WorkSpace ID: $WORKSPACE_ID" echo "Directory ID: $DIRECTORY_ID" echo "Username: $USERNAME" echo "Running Mode: $RUNNING_MODE" # Step 10: Remind about invitation emails echo "" echo "==============================================" echo "INVITATION EMAILS" echo "==============================================" echo "IMPORTANT: If you're using AD Connector or a trust relationship, or if the user already exists in Active Directory," echo "invitation emails are not sent automatically. You'll need to manually send an invitation email." echo "For more information, see: https://docs.aws.amazon.com/workspaces/latest/adminguide/manage-workspaces-users.html#send-invitation" # Step 11: Cleanup confirmation echo "" echo "==============================================" echo "CLEANUP CONFIRMATION" echo "==============================================" echo "Resources created:" for resource in "${CREATED_RESOURCES[@]}"; do echo " - $resource" done echo "" echo "Do you want to clean up all created resources? (y/n):" CLEANUP_CHOICE="y" if [ "$CLEANUP_CHOICE" = "y" ] || [ "$CLEANUP_CHOICE" = "Y" ]; then echo "" echo "==============================================" echo "CLEANING UP RESOURCES" echo "==============================================" # Terminate WorkSpace if [ -n "$WORKSPACE_ID" ]; then echo "Terminating WorkSpace $WORKSPACE_ID..." TERMINATE_OUTPUT=$(aws workspaces terminate-workspaces --terminate-workspace-requests WorkspaceId="$WORKSPACE_ID") check_command "$TERMINATE_OUTPUT" echo "$TERMINATE_OUTPUT" echo "WorkSpace termination initiated. This may take a few minutes." fi # Deregister directory (only if we registered it in this script) if [[ " ${CREATED_RESOURCES[*]} " == *"Directory registration: $DIRECTORY_ID"* ]]; then echo "Deregistering directory $DIRECTORY_ID from WorkSpaces..." DEREGISTER_OUTPUT=$(aws workspaces deregister-workspace-directory --directory-id "$DIRECTORY_ID") check_command "$DEREGISTER_OUTPUT" echo "$DEREGISTER_OUTPUT" echo "Directory deregistration initiated. This may take a few minutes." fi echo "Cleanup completed." else echo "Skipping cleanup. Resources will remain in your AWS account." fi echo "" echo "==============================================" echo "SCRIPT COMPLETED" echo "==============================================" echo "Log file: $LOG_FILE" echo "Thank you for using the WorkSpaces creation script!"