如何使用 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 命令时请记住以下事项:

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": [
        {
            "id": "c86e552e-c21a-4cdf-aa68-05fb20574e8b",
            "resourceName": "StaticIpForTestLightsailInstance1",
            "resourceType": "StaticIp",
            "createdAt": 1602182597.168,
            "location": {
                "availabilityZone": "all",
                "regionName": "eu-west-1"
            },
            "isTerminal": true,
            "operationDetails": "TestLightsailInstance1",
            "operationType": "DetachStaticIp",
            "status": "Succeeded",
            "statusChangedAt": 1602182597.168
        },
        {
            "id": "4b9dcaa7-be3a-4dfd-8ac0-32f0238c0833",
            "resourceName": "TestLightsailInstance1",
            "resourceType": "Instance",
            "createdAt": 1602182597.17,
            "location": {
                "availabilityZone": "eu-west-1a",
                "regionName": "eu-west-1"
            },
            "isTerminal": true,
            "operationDetails": "StaticIpForTestLightsailInstance1",
            "operationType": "DetachStaticIp",
            "status": "Succeeded",
            "statusChangedAt": 1602182597.17
        }
    ]
}

将静态 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": [
        {
            "id": "192c4917-c332-49c8-88ab-60484a42c98f",
            "resourceName": "StaticIpForTestLightsailInstance1",
            "resourceType": "StaticIp",
            "createdAt": 1602182686.46,
            "location": {
                "availabilityZone": "all",
                "regionName": "eu-west-1"
            },
            "isTerminal": true,
            "operationDetails": "RestoredTestLightsailInstance1-New",
            "operationType": "AttachStaticIp",
            "status": "Succeeded",
            "statusChangedAt": 1602182686.46
        },
        {
            "id": "fb93c012-e3a2-4908-8746-01a4ae018440",
            "resourceName": "RestoredTestLightsailInstance1-New",
            "resourceType": "Instance",
            "createdAt": 1602182686.463,
            "location": {
                "availabilityZone": "eu-west-1a",
                "regionName": "eu-west-1"
            },
            "isTerminal": true,
            "operationDetails": "StaticIpForTestLightsailInstance1",
            "operationType": "AttachStaticIp",
            "status": "Succeeded",
            "statusChangedAt": 1602182686.463
        }
    ]
}

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     |
+----------------------------------+------------------+----------------+------------+-----------------+------------------------------------------+-------------------+