#!/usr/bin/env bash # # launch.sh - deploy the Windows GPU cloud-gaming host. # # Validates the launch kit, creates the S3 artifact bucket, deploys the # CloudFormation stack, waits for the instance to register with Systems Manager, # then pushes the Windows bootstrap script over SSM. No inbound RDP, ever. # # Quick start: # export AWS_PROFILE=my-profile KEY_NAME=my-keypair # ./launch.sh --instance-type g6.xlarge --volume-gib 600 # set -euo pipefail # ----------------------------------------------------------------- defaults STACK_NAME="${STACK_NAME:-steam-gpu}" REGION="${AWS_REGION:-${AWS_DEFAULT_REGION:-us-east-1}}" INSTANCE_TYPE="${INSTANCE_TYPE:-g6.xlarge}" GAME_VOLUME_GIB="${GAME_VOLUME_GIB:-600}" ADMIN_CIDR="${ADMIN_CIDR:-}" KEY_NAME="${KEY_NAME:-}" ARTIFACT_BUCKET="${ARTIFACT_BUCKET:-}" BOOTSTRAP="${BOOTSTRAP:-1}" DRY_RUN="${DRY_RUN:-0}" TEMPLATE="cloudformation/steam-gpu-host.yaml" BOOTSTRAP_PS1="scripts/bootstrap.ps1" DIAGNOSE_PS1="scripts/diagnose.ps1" SSM_DOC="ssm/SteamGpu-Diagnose.json" # ------------------------------------------------------------------- output if [[ -t 1 ]]; then C_B=$'\033[1;34m'; C_Y=$'\033[1;33m'; C_R=$'\033[1;31m'; C_G=$'\033[1;32m'; C_0=$'\033[0m' else C_B=''; C_Y=''; C_R=''; C_G=''; C_0='' fi log() { printf '%s[launch]%s %s\n' "$C_B" "$C_0" "$*"; } ok() { printf '%s[ ok ]%s %s\n' "$C_G" "$C_0" "$*"; } warn() { printf '%s[warn]%s %s\n' "$C_Y" "$C_0" "$*" >&2; } die() { printf '%s[fail]%s %s\n' "$C_R" "$C_0" "$*" >&2; exit 1; } usage() { sed -n '2,20p' "$0" | sed 's/^# \{0,1\}//' cat <<'EOF' Options: --stack-name NAME CloudFormation stack name (default: steam-gpu) --region REGION AWS region (default: us-east-1) --instance-type TYPE g4dn.xlarge|g5.xlarge|g6.xlarge|... (default: g6.xlarge) --key-name NAME EC2 key pair (decrypts the local Administrator password) --admin-cidr CIDR CIDR allowed to reach DCV, e.g. 1.2.3.4/32 (default: your current public IP) --volume-gib N Steam library EBS size in GiB (default: 600) --artifact-bucket NAME Bucket for bootstrap scripts (default: created) --no-bootstrap Create infrastructure only; skip the SSM bootstrap --dry-run Validate and print the plan; change nothing -h, --help This text Every option is also settable as the uppercase environment variable of the same name: STACK_NAME AWS_REGION INSTANCE_TYPE KEY_NAME ADMIN_CIDR GAME_VOLUME_GIB ARTIFACT_BUCKET DRY_RUN EOF exit 0 } # --------------------------------------------------------------------- args while [[ $# -gt 0 ]]; do case "$1" in --stack-name) STACK_NAME="$2"; shift 2 ;; --region) REGION="$2"; shift 2 ;; --instance-type) INSTANCE_TYPE="$2"; shift 2 ;; --key-name) KEY_NAME="$2"; shift 2 ;; --admin-cidr) ADMIN_CIDR="$2"; shift 2 ;; --volume-gib) GAME_VOLUME_GIB="$2"; shift 2 ;; --artifact-bucket) ARTIFACT_BUCKET="$2"; shift 2 ;; --no-bootstrap) BOOTSTRAP=0; shift ;; --dry-run) DRY_RUN=1; shift ;; -h|--help) usage ;; *) die "unknown option: $1 (try --help)" ;; esac done # ------------------------------------------------------------ preconditions command -v aws >/dev/null 2>&1 || die "aws CLI not found" command -v python3 >/dev/null 2>&1 || die "python3 not found (needed by the validator)" [[ -f "$TEMPLATE" ]] || die "missing $TEMPLATE - run this from the launch kit root" aws sts get-caller-identity >/dev/null 2>&1 \ || die "no usable AWS credentials. Set AWS_PROFILE or AWS_ACCESS_KEY_ID/SECRET." ACCOUNT="$(aws sts get-caller-identity --query Account --output text)" CALLER="$(aws sts get-caller-identity --query Arn --output text)" log "account ${ACCOUNT} region ${REGION} stack ${STACK_NAME}" log "caller ${CALLER}" if [[ -z "$ADMIN_CIDR" ]]; then MYIP="$(curl -fsS https://checkip.amazonaws.com | tr -d '\n')" \ || die "could not determine your public IP; pass --admin-cidr explicitly" ADMIN_CIDR="${MYIP}/32" warn "defaulted --admin-cidr to ${ADMIN_CIDR}. Your IP changes; re-run or update the rule later." fi [[ "$ADMIN_CIDR" =~ /32$ ]] || die "--admin-cidr must be a single host, e.g. 203.0.113.7/32" # ---------------------------------------------------------------- step 1: validate log "step 1/5 - validating the launch kit (no AWS calls)" PY=python3 $PY -c 'import yaml' 2>/dev/null || { warn "pyyaml missing; attempting a local install" $PY -m pip install --quiet --user pyyaml || die "install pyyaml to validate: python3 -m pip install pyyaml" } if $PY scripts/validate.py --root . --pretty; then ok "validation passed" else die "validation failed - fix the checks above before deploying" fi # ---------------------------------------------------------------- step 2: bucket if [[ -z "$ARTIFACT_BUCKET" ]]; then ARTIFACT_BUCKET="steam-gpu-launchkit-${ACCOUNT}-${REGION}" log "step 2/5 - no --artifact-bucket given; will use ${ARTIFACT_BUCKET}" else log "step 2/5 - using artifact bucket ${ARTIFACT_BUCKET}" fi # ---------------------------------------------------------------- step 3: deploy DEPLOY_CMD=( aws cloudformation deploy --stack-name "$STACK_NAME" --template-file "$TEMPLATE" --capabilities CAPABILITY_NAMED_IAM --no-fail-on-empty-changeset --region "$REGION" --parameter-overrides "InstanceType=${INSTANCE_TYPE}" "KeyName=${KEY_NAME}" "AdminCidr=${ADMIN_CIDR}" "ArtifactBucket=${ARTIFACT_BUCKET}" "GameVolumeSizeGiB=${GAME_VOLUME_GIB}" ) log "step 3/5 - deploying the stack" printf ' %s\n' "${DEPLOY_CMD[*]}" if [[ "$DRY_RUN" == "1" ]]; then warn "dry run: stopping before any AWS change." ok "plan printed. Re-run without --dry-run to apply." exit 0 fi [[ -n "$KEY_NAME" ]] || die "KEY_NAME/--key-name is required for a real deploy" # Create the artifact bucket first: the instance role's inline policy references # it, and the bootstrap step needs to read the scripts from it. if ! aws s3api head-bucket --bucket "$ARTIFACT_BUCKET" --region "$REGION" >/dev/null 2>&1; then log "creating artifact bucket ${ARTIFACT_BUCKET}" if [[ "$REGION" == "us-east-1" ]]; then aws s3api create-bucket --bucket "$ARTIFACT_BUCKET" --region "$REGION" >/dev/null else aws s3api create-bucket --bucket "$ARTIFACT_BUCKET" --region "$REGION" \ --create-bucket-configuration "LocationConstraint=${REGION}" >/dev/null fi aws s3api put-public-access-block --bucket "$ARTIFACT_BUCKET" \ --public-access-block-configuration \ "BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true" >/dev/null aws s3api put-bucket-encryption --bucket "$ARTIFACT_BUCKET" \ --server-side-encryption-configuration \ '{"Rules":[{"ApplyServerSideEncryptionByDefault":{"SSEAlgorithm":"AES256"}}]}' >/dev/null ok "bucket ready (public access blocked, SSE-S3 on)" fi aws s3 cp "$BOOTSTRAP_PS1" "s3://${ARTIFACT_BUCKET}/bootstrap.ps1" --region "$REGION" >/dev/null aws s3 cp "$DIAGNOSE_PS1" "s3://${ARTIFACT_BUCKET}/diagnose.ps1" --region "$REGION" >/dev/null ok "bootstrap.ps1 and diagnose.ps1 uploaded" "${DEPLOY_CMD[@]}" ok "stack ${STACK_NAME} deployed" read -r INSTANCE_ID DCV_ENDPOINT ROLE_ARN <<<"$(aws cloudformation describe-stacks \ --stack-name "$STACK_NAME" --region "$REGION" \ --query 'Stacks[0].Outputs' --output text \ | awk '/InstanceId/{id=$2} /PublicDcvEndpoint/{ep=$2} /InstanceRoleArn/{rn=$2} END{print id, ep, rn}')" log "instance ${INSTANCE_ID}" log "dcv endpoint ${DCV_ENDPOINT}" log "role arn ${ROLE_ARN}" # ---------------------------------------------------------------- step 4: SSM log "step 4/5 - waiting for the instance to register with Systems Manager" DEADLINE=$(( SECONDS + 600 )) while (( SECONDS < DEADLINE )); do PING="$(aws ssm describe-instance-information \ --region "$REGION" \ --filters "Key=InstanceIds,Values=${INSTANCE_ID}" \ --query 'InstanceInformationList[0].PingStatus' --output text 2>/dev/null || echo None)" if [[ "$PING" == "Online" ]]; then ok "instance is Online in Systems Manager" break fi printf ' ... %s (waiting)\n' "$PING" sleep 20 done [[ "$PING" == "Online" ]] || die "instance never registered with SSM; check the instance role and outbound 443" # Register the diagnostic document so it is available from console, CLI or a schedule. aws ssm create-document \ --name SteamGpu-Diagnose \ --document-type Command \ --document-format JSON \ --content "file://${SSM_DOC}" \ --region "$REGION" \ --tags Key=Project,Value=steam-gpu >/dev/null 2>&1 \ && ok "SSM document SteamGpu-Diagnose registered" \ || warn "SSM document already exists or could not be created; continuing" # ---------------------------------------------------------------- step 5: bootstrap if [[ "$BOOTSTRAP" == "1" ]]; then log "step 5/5 - running the Windows bootstrap over SSM (10-20 minutes; the box reboots)" CMD_ID="$(aws ssm send-command \ --region "$REGION" \ --instance-ids "$INSTANCE_ID" \ --document-name "AWS-RunPowerShellScript" \ --comment "steam-gpu bootstrap via launch.sh" \ --timeout-seconds 3600 \ --parameters "commands=[\"Set-ExecutionPolicy Bypass -Scope Process -Force; Read-S3Object -BucketName '${ARTIFACT_BUCKET}' -Key bootstrap.ps1 -File C:\\\\bootstrap.ps1; & C:\\\\bootstrap.ps1\"]" \ --query 'Command.CommandId' --output text)" log "command id ${CMD_ID}" log "follow along with:" log " aws ssm list-command-invocations --command-id ${CMD_ID} --region ${REGION} --details \\" log " --query 'CommandInvocations[0].CommandPlugins[0].Output' --output text" else log "step 5/5 - skipped (--no-bootstrap)" warn "upload the DCV MSI and driver yourself, then run scripts/bootstrap.ps1 from the instance" fi printf '\n' ok "launch complete" cat < Go Offline. 5. Run the health report: aws ssm send-command --document-name SteamGpu-Diagnose \\ --instance-ids ${INSTANCE_ID} --region ${REGION} RDP without exposing 3389 aws ssm start-session --target ${INSTANCE_ID} --region ${REGION} \\ --document-name AWS-StartPortForwardingSession \\ --parameters '{"portNumber":["3389"],"localPortNumber":["13389"]}' Cost ${INSTANCE_TYPE} runs until you stop it. Stop ${INSTANCE_ID} between sessions. EOF