1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
| # 定义变量
# 实例名称
instanceName="ubuntu"
# 选择需要的镜像ID
blueprintId="ubuntu_22_04"
# 选择需要的实例套餐ID
bundleId="nano_3_0"
# 选择需要的密钥对名称,需要提前创建到 aws 的密钥对管理中
keyPairName="lightsail"
region="ap-northeast-1"
availabilityZone="ap-northeast-1a"
# 删除实例
aws lightsail delete-instance --region "${region}" --instance-name "${instanceName}"
# 创建实例
aws lightsail create-instances \
--instance-names "${instanceName}" \
--region "${region}" \
--availability-zone "${availabilityZone}" \
--blueprint-id "${blueprintId}" \
--bundle-id "${bundleId}" \
--key-pair-name "${keyPairName}" \
--user-data '#!/bin/bash
set -e
export OUTLINE_AUTOINSTALL=1
bash -c "$(wget -qO- https://raw.githubusercontent.com/Jigsaw-Code/outline-server/master/src/server_manager/install_scripts/install_server.sh)"
'
# 等待实例创建并获取IP地址
ipAddress=""
while [ -z "$ipAddress" ]; do
echo "Waiting for instance to have a public IP address..."
sleep 1 # 等待1秒
ipAddress=$(aws lightsail get-instance --instance-name "${instanceName}" | jq -r .instance.publicIpAddress)
done
# 等待实例完全启动,不再处于过渡状态
echo "Waiting for instance to finish initializing..."
instanceState=""
while [ "$instanceState" != "running" ]; do
echo "Instance is still starting up, current state: $instanceState"
sleep 1 # 等待1秒
instanceState=$(aws lightsail get-instance --instance-name "${instanceName}" | jq -r .instance.state.name)
done
# 现在实例已完全启动,执行开放端口操作
echo "Instance is running, opening ports..."
# 填入开放端口
fromPort="0"
toPort="65535"
aws lightsail open-instance-public-ports \
--instance-name ubuntu \
--port-info fromPort="${fromPort}",toPort="${toPort}",protocol=TCP \
&& \
aws lightsail open-instance-public-ports \
--instance-name ubuntu \
--port-info fromPort="${fromPort}",toPort="${toPort}",protocol=UDP
# 等待实例的SSH端口开放
echo "Instance IP address: ${ipAddress}"
while ! nc -zv "${ipAddress}" 22 2>&1 | grep -q succeeded; do
echo "Waiting for SSH port to be available..."
sleep 1 # 等待1秒
done
# 连接实例
echo "Connecting to instance..."
ssh -i ~/.ssh/"${keyPairName}".pem ubuntu@"${ipAddress}"
|