用 aws cli 新建 lightsail 实例

因网络原因,无法使用 AWS 管理台新建 lightsail 实例,所以使用 aws cli 创建 lightsail 实例

用 aws cli 新建 lightsail 实例

获取蓝图与样例的 ID

1
2
aws lightsail get-blueprints
aws lightsail get-bundles

使用组合命令创建实例

 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}"

获取 apiURL

1
cat /var/log/cloud-init-output.log

删除实例

1
aws lightsail delete-instance --region "${region}" --instance-name "${instanceName}"

其他命令

安装 outline

1
sudo bash -c "$(wget -qO- https://raw.githubusercontent.com/Jigsaw-Code/outline-server/master/src/server_manager/install_scripts/install_server.sh)"

根据 outline 提示开放端口

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17

# 填入开放端口

managementPort=""
accessKeyPort=""

aws lightsail open-instance-public-ports \
 --instance-name ubuntu \
 --port-info fromPort="${managementPort}",toPort="${managementPort}",protocol=TCP \
&& \
aws lightsail open-instance-public-ports \
 --instance-name ubuntu \
 --port-info fromPort="${accessKeyPort}",toPort="${accessKeyPort}",protocol=TCP \
&& \
aws lightsail open-instance-public-ports \
 --instance-name ubuntu \
 --port-info fromPort="${accessKeyPort}",toPort="${accessKeyPort}",protocol=UDP

切换区域

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
aws configure set region ap-northeast-1
# 韩国
region="ap-northeast-2"
availabilityZone="ap-northeast-2a"
# 新加坡
region="ap-southeast-1"
availabilityZone="ap-southeast-1a"
# 东京
region="ap-northeast-1"
availabilityZone="ap-northeast-1a"
# 雅加达
region="ap-southeast-3"
availabilityZone="ap-southeast-3a"
版权声明:本文为原创,依据 CC BY-NC-SA 4.0 许可证进行授权,转载请附上出处链接及本声明。
最后更新于 2025年11月19日 13:24 +0800