如何使用 AWS CLI 命令管理 Lightsail 实例上的静态 IP 地址?

上次更新日期:2021 年 1 月 11 日

我想从 Amazon Lightsail 实例中分离我的静态 IP 地址,然后将其附加到新的 Lightsail 实例。如何使用 AWS 命令行界面 (AWS CLI) 命令执行此操作?

简短描述

有关 Amazon Lightsail AWS CLI 命令的列表,请参阅 AWS CLI 命令参考和 Amazon Lightsail API 参考。

重要提示:使用 AWS CLI 命令时请记住以下事项:

如果在运行 AWS CLI 命令时收到错误,请确保您使用的是最新版本的 AWS CLI。 默认的 AWS CLI 输出为 JSON。您可以使用默认值,也可以将 --output json 附加到命令以接收输出,如以下示例所示。有关更多信息,请参阅控制 AWS CLI 的命令输出。 有关解决 AWS CLI 错误的一般信息,请参阅运行 AWS CLI 命令时为什么会收到错误? AWS CLI 输出以 Unix Epoch 时间格式显示时间戳。使用以下一种方法将时间戳转换成 UTC:

macOS:

从时间戳中删除小数点和小数点右侧的任何数字,然后运行以下命令:

# date -r 1602175741 -u
Thu Oct  8 16:49:01 UTC 2020

Linux:

运行以下命令:

# date -d @1602175741.603 -u
Thu Oct  8 16:49:01 UTC 2020

Windows:

使用转换器对时间戳进行转换,如 epochconverter.com。

解决方法 从现有 Lightsail 实例中分离静态 IP 地址

运行 detach-static-ip 命令以将静态 IP 地址与实例分离。以下示例将静态 IP 地址从 eu-west-1 区域中的某个实例分离。将 --static-ip-name--region 替换为您的请求中的相应值。

# aws lightsail detach-static-ip --static-ip-name StaticIpForTestLightsailInstance1 --region eu-west-1
{
    "operations": 
}
将静态 IP 地址附加到新的 Lightsail 实例

1.运行 attach-static-ip 命令将静态 IP 地址附加到备份中的新实例。将 --static-ip-name--instance-name--region 替换为您的请求中的相应值。

# aws lightsail attach-static-ip --static-ip-name StaticIpForTestLightsailInstance1 --instance-name RestoredTestLightsailInstance1-New  --region eu-west-1
{
    "operations": 
}

2.运行 get-instances 命令以验证静态 IP 地址是否已分配给您的实例。

# aws lightsail get-instances --region eu-west-1 --query 'instances[].{name:name,createdAt:createdAt,blueprintId:blueprintId,bundleid:bundleId,blueprintName:blueprintName,publicIpAddress:publicIpAddress,InstanceID:supportCode}' --output table
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
|                                                                            GetInstances                                                                            |
+----------------------------------+------------------+----------------+------------+-----------------+------------------------------------------+-------------------+
|            InstanceID            |   blueprintId    | blueprintName  | bundleid   |    createdAt    |                  name                    |  publicIpAddress  |
+----------------------------------+------------------+----------------+------------+-----------------+------------------------------------------+-------------------+
|  11178xxxxxxx/i-09f6xxxx|  wordpress       |  WordPress     |  large_2_0 |  1602182374.625 |  RestoredTestLightsailInstance1-New      |  52.210.xx.xx     |
+----------------------------------+------------------+----------------+------------+-----------------+------------------------------------------+-------------------+