返回 CodeWhale
provision.sh
1 #!/usr/bin/env bash
2 # EXPERIMENTAL — AWS Lightsail smoke-lab provisioning for the CodeWhale
3 # remote workbench (issue #1990). Creates ONE Ubuntu 24.04 Lightsail
4 # instance with SSH-only firewall. Prints every step and the monthly price
5 # from the Lightsail API, then requires an explicit "yes" before creating
6 # anything that costs money.
7 #
8 # Usage:
9 # AWS_REGION=us-east-1 bash scripts/aws-lightsail/provision.sh
10 #
11 # Tunables (env):
12 # INSTANCE_NAME default codewhale-smoke
13 # BUNDLE_ID default medium_3_0 (2 vCPU / 4 GB — docs/REMOTE_VM_US.md default)
14 # BLUEPRINT_ID default ubuntu_24_04
15 # SSH_PUBKEY default ~/.ssh/id_ed25519.pub (imported as key pair)
16 # RESTRICT_SSH_TO_MY_IP default true (firewall cidr = caller IP /32)
17 set -euo pipefail
18
19 INSTANCE_NAME="${INSTANCE_NAME:-codewhale-smoke}"
20 BUNDLE_ID="${BUNDLE_ID:-medium_3_0}"
21 BLUEPRINT_ID="${BLUEPRINT_ID:-ubuntu_24_04}"
22 SSH_PUBKEY="${SSH_PUBKEY:-$HOME/.ssh/id_ed25519.pub}"
23 KEY_PAIR_NAME="${KEY_PAIR_NAME:-${INSTANCE_NAME}-key}"
24 RESTRICT_SSH_TO_MY_IP="${RESTRICT_SSH_TO_MY_IP:-true}"
25
26 command -v aws >/dev/null || { echo "aws CLI is required" >&2; exit 1; }
27 aws sts get-caller-identity >/dev/null || { echo "aws is not authenticated; run 'aws configure' or 'aws sso login'" >&2; exit 1; }
28
29 REGION="${AWS_REGION:-$(aws configure get region || true)}"
30 [[ -n "${REGION}" ]] || { echo "Set AWS_REGION (e.g. us-east-1)" >&2; exit 1; }
31
32 echo "== Preflight =="
33 aws lightsail get-blueprints --region "$REGION" \
34 --query "blueprints[?blueprintId=='${BLUEPRINT_ID}'].[blueprintId,name]" --output text \
35 | grep -q . || { echo "Blueprint ${BLUEPRINT_ID} not found in ${REGION}" >&2; exit 1; }
36
37 PRICE=$(aws lightsail get-bundles --region "$REGION" \
38 --query "bundles[?bundleId=='${BUNDLE_ID}'].price | [0]" --output text)
39 SPECS=$(aws lightsail get-bundles --region "$REGION" \
40 --query "bundles[?bundleId=='${BUNDLE_ID}'].[cpuCount,ramSizeInGb,diskSizeInGb] | [0]" --output text)
41 [[ "$PRICE" != "None" && -n "$PRICE" ]] || { echo "Bundle ${BUNDLE_ID} not found in ${REGION}" >&2; exit 1; }
42
43 [[ -f "$SSH_PUBKEY" ]] || { echo "SSH public key not found: $SSH_PUBKEY" >&2; exit 1; }
44
45 echo "Region: $REGION"
46 echo "Instance: $INSTANCE_NAME"
47 echo "Blueprint: $BLUEPRINT_ID"
48 echo "Bundle: $BUNDLE_ID (vCPU/RAM-GB/Disk-GB: $SPECS)"
49 echo "Monthly price: \$$PRICE USD (billed hourly until deleted)"
50 echo "SSH key: $SSH_PUBKEY -> key pair '$KEY_PAIR_NAME'"
51 echo
52 read -r -p "Create this instance and start billing? Type 'yes' to proceed: " CONFIRM
53 [[ "$CONFIRM" == "yes" ]] || { echo "Aborted; nothing created."; exit 1; }
54
55 echo "== Import SSH key pair =="
56 if ! aws lightsail get-key-pair --region "$REGION" --key-pair-name "$KEY_PAIR_NAME" >/dev/null 2>&1; then
57 aws lightsail import-key-pair --region "$REGION" \
58 --key-pair-name "$KEY_PAIR_NAME" \
59 --public-key-base64 "$(base64 < "$SSH_PUBKEY")" >/dev/null
60 echo "imported $KEY_PAIR_NAME"
61 else
62 echo "key pair $KEY_PAIR_NAME already exists; reusing"
63 fi
64
65 echo "== Create instance =="
66 AZ=$(aws lightsail get-regions --include-availability-zones --region "$REGION" \
67 --query "regions[?name=='${REGION}'].availabilityZones[0].zoneName | [0]" --output text)
68 aws lightsail create-instances --region "$REGION" \
69 --instance-names "$INSTANCE_NAME" \
70 --availability-zone "$AZ" \
71 --blueprint-id "$BLUEPRINT_ID" \
72 --bundle-id "$BUNDLE_ID" \
73 --key-pair-name "$KEY_PAIR_NAME" >/dev/null
74 echo "created $INSTANCE_NAME in $AZ; waiting for running state..."
75
76 for _ in $(seq 1 60); do
77 STATE=$(aws lightsail get-instance-state --region "$REGION" --instance-name "$INSTANCE_NAME" \
78 --query 'state.name' --output text 2>/dev/null || echo pending)
79 [[ "$STATE" == "running" ]] && break
80 sleep 5
81 done
82 [[ "${STATE:-}" == "running" ]] || { echo "instance did not reach running state" >&2; exit 1; }
83
84 echo "== Firewall: SSH only =="
85 CIDR="0.0.0.0/0"
86 if [[ "$RESTRICT_SSH_TO_MY_IP" == "true" ]]; then
87 MYIP=$(curl -fsS https://checkip.amazonaws.com | tr -d '\n')
88 CIDR="${MYIP}/32"
89 fi
90 aws lightsail put-instance-public-ports --region "$REGION" \
91 --instance-name "$INSTANCE_NAME" \
92 --port-infos "fromPort=22,toPort=22,protocol=tcp,cidrs=${CIDR}" >/dev/null
93 echo "open ports replaced with: 22/tcp from ${CIDR} (everything else closed)"
94
95 IP=$(aws lightsail get-instance --region "$REGION" --instance-name "$INSTANCE_NAME" \
96 --query 'instance.publicIpAddress' --output text)
97 echo
98 echo "== Done =="
99 echo "Instance: $INSTANCE_NAME ($REGION, $STATE)"
100 echo "Public IP: $IP"
101 echo "SSH: ssh -i ${SSH_PUBKEY%.pub} ubuntu@${IP}"
102 echo
103 echo "Teardown when finished (stops billing):"
104 echo " AWS_REGION=$REGION bash scripts/aws-lightsail/teardown.sh"
105
105 lines BASH