如何使用 AWS CLI 命令来管理我的 Lightsail 实例?

5 分钟阅读
0

我想使用 AWS 命令行界面(AWS CLI)命令来管理我的 Amazon Lightsail 实例。

简短描述

以下是在您的 Lightsail 实例上使用 AWS CLI 命令的常见场景:

  • 列出 AWS 区域中可用的所有 Lightsail 计划(捆绑包)和蓝图
  • 使用特定的捆绑包和蓝图创建 Lightsail 实例
  • 列出特定区域中的所有 Lightsail 实例
  • 分配静态 IP 地址,然后将其附加到实例并进行验证

注意:

  • 如果在运行 AWS CLI 命令时收到错误,请参阅排查 AWS CLI 错误。此外,确保您使用的是最新版本的 AWS CLI
  • 默认的 AWS CLI 输出为 JSON。或者,您可以将**--output json** 附加到命令中以接收以下示例中所示的输出。有关更多信息,请参阅控制 AWS CLI 的命令输出
  • AWS CLI 命令输出以 Unix Epoch 时间格式显示时间戳。使用以下方法之一将时间戳转换为 UTC。

macOS

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

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

Linux

运行以下命令:

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

Windows

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

解决方法

列出某个区域中可用的 Lightsail 捆绑包和蓝图

运行 get-bundles 命令列出您的区域可供购买的捆绑包。下面是输出示例:

# aws lightsail get-bundles --region eu-west-1 --query 'bundles[].{price:price,cpuCount:cpuCount,ramSizeInGb:ramSizeInGb,diskSizeInGb:diskSizeInGb,bundleId:bundleId,instanceType:instanceType,supportedPlatforms:supportedPlatforms[0]}' --output table
---------------------------------------------------------------------------------------------------------------
|                                                 GetBundles                                                  |
+-----------------+-----------+---------------+---------------+--------+---------------+----------------------+
|    bundleId     | cpuCount  | diskSizeInGb  | instanceType  | price  |  ramSizeInGb  | supportedPlatforms   |
+-----------------+-----------+---------------+---------------+--------+---------------+----------------------+
|  nano_2_0       |  1        |  20           |  nano         |  3.5   |  0.5          |  LINUX_UNIX          |
|  small_2_0      |  1        |  60           |  small        |  10.0  |  2.0          |  LINUX_UNIX          |
|  nano_win_2_0   |  1        |  30           |  nano         |  8.0   |  0.5          |  WINDOWS             |
|  micro_win_2_0  |  1        |  40           |  micro        |  12.0  |  1.0          |  WINDOWS             |
+-----------------+-----------+---------------+---------------+--------+---------------+----------------------+

**注意:**请将 eu-west-1 替换为您的区域。

运行 get-blueprints 命令列出您的区域的所有映像或蓝图。下面是输出示例:

# aws lightsail get-blueprints --region eu-west-1 --query 'blueprints[].{blueprintId:blueprintId,name:name,group:group,productUrl:productUrl,platform:platform}' --output table
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
|                                                                           GetBlueprints                                                                           |
+--------------------------------------+-----------------------+--------------------------------+-------------+-----------------------------------------------------+
|              blueprintId             |         group         |             name               |  platform   |                     productUrl                      |
+--------------------------------------+-----------------------+--------------------------------+-------------+-----------------------------------------------------+
|  windows_server_2019                 |  windows_2019         |  Windows Server 2019           |  WINDOWS    |  https://aws.amazon.com/marketplace/pp/B07QZ4XZ8F   |
|  windows_server_2016                 |  windows_2016         |  Windows Server 2016           |  WINDOWS    |  https://aws.amazon.com/marketplace/pp/B01M7SJEU7   |
|  amazon_linux_2                      |  amazon_linux_2       |  Amazon Linux 2                |  LINUX_UNIX |  https://aws.amazon.com/amazon-linux-2/             |
|  amazon_linux                        |  amazon-linux         |  Amazon Linux                  |  LINUX_UNIX |  https://aws.amazon.com/marketplace/pp/B00CIYTQTC   |
|  ubuntu_18_04                        |  ubuntu_18            |  Ubuntu                        |  LINUX_UNIX |  https://aws.amazon.com/marketplace/pp/B07CQ33QKV   |
|  debian_10                           |  debian_10            |  Debian                        |  LINUX_UNIX |  https://aws.amazon.com/marketplace/pp/B0859NK4HC   |
+--------------------------------------+-----------------------+--------------------------------+-------------+-----------------------------------------------------+

**注意:**请将 eu-west-1 替换为您的区域。

筛选所需平台的蓝图。例如,以下命令为筛选 Linux/Unix 平台操作系统的蓝图:

# aws lightsail get-blueprints --query 'blueprints[?type==`os`&&platform==`LINUX_UNIX`].{name:name,version:version}'--output table
--------------------------------------------
|               GetBlueprints              |
+-----------------+------------------------+
|      name       |        version         |
+-----------------+------------------------+
|  Amazon Linux 2 |  2.0.20200917.0        |
|  Amazon Linux   |  2018.03.0.20200918.0  |
|  Ubuntu         |  20.04 LTS             |
|  Debian         |  10.5                  |
|  openSUSE       |  15.1                  |
|  CentOS         |  7 1901-01             |
+-----------------+------------------------+

使用特定的捆绑包和蓝图创建 Lightsail 实例

  1. 运行 get-blueprint 命令。以下示例获取了 WordPress 蓝图。
    # aws lightsail get-blueprints --region eu-west-1 --query 'blueprints[].{blueprintId:blueprintId,name:name,group:group,productUrl:productUrl,platform:platform}' --output table |grep -i wordpress
    |  wordpress                           |  wordpress            |  WordPress                     |  LINUX_UNIX |  https://aws.amazon.com/marketplace/pp/B00NN8Y43U   |
    |  wordpress_multisite                 |  wordpress_multisite  |  WordPress Multisite           |  LINUX_UNIX |  https://aws.amazon.com/marketplace/pp/B00NN8XE6S   |
    **注意:**请将 eu-west-1 替换为您的区域,将 WordPress 信息替换为您的蓝图信息。
  2. 运行 create-instances 命令来创建 Amazon Lightsail 实例。以下示例为使用 WordPress 蓝图和 micro_2_o 捆绑包在 eu-west-1 区域创建 Amazon Lightsail 实例。
    # aws lightsail create-instances --region eu-west-1 --instance-names MyNewInstance --availability-zone eu-west-1a --blueprint-id wordpress --bundle-id micro_2_0
    {
      "operations": [
        {
          "id": "003757e7-0880-4162-9912-211d4756081e",
          "resourceName": "MyNewInstance",
          "resourceType": "Instance",
          "createdAt": 1602175452.334,
          "location": {
            "availabilityZone": "eu-west-1a",
            "regionName": "eu-west-1"
          },
          "isTerminal": false,
          "operationType": "CreateInstance",
          "status": "Started",
          "statusChangedAt": 1602175452.334
        }
      ]
    }
    注意:instance-namesblueprint-id、bundle-idregion 替换为您的值。要使用单个 API 调用创建多个实例,请使用--instance-names 指定参数,例如 LightsailInstance1LightsailInstance2。以下示例为使用 WordPress 蓝图和 micro_2_0 捆绑包在 eu-west-1 区域创建两个 Lightsail 实例。
    # aws lightsail create-instances --region eu-west-1 --instance-names {"TestLightsailInstance1","TestLightsailInstance2"} --availability-zone eu-west-1a --blueprint-id wordpress --bundle-id micro_2_0
    {
      "operations": [
        {
          "id": "8a15b195-9a20-4d79-ad1d-6e85557ae94d",
          "resourceName": "TestLightsailInstance1",
          "resourceType": "Instance",
          "createdAt": 1602175741.602,
          "location": {
            "availabilityZone": "eu-west-1a",
            "regionName": "eu-west-1"
          },
          "isTerminal": false,
          "operationType": "CreateInstance",
          "status": "Started",
          "statusChangedAt": 1602175741.602
        },
        {
          "id": "a006513b-0e25-4802-ab33-671a40bf5a92",
          "resourceName": "TestLightsailInstance2",
          "resourceType": "Instance",
          "createdAt": 1602175741.603,
          "location": {
            "availabilityZone": "eu-west-1a",
            "regionName": "eu-west-1"
          },
          "isTerminal": false,
          "operationType": "CreateInstance",
          "status": "Started",
          "statusChangedAt": 1602175741.603
        }
      ]
    }

列出一个区域中的所有 Lightsail 实例

运行 get-instances 命令显示特定区域中的 Lightsail 实例。以下示例所示为 eu-west-1 区域中的 Lightsail 实例。

# aws lightsail get-instances --region eu-west-1 --query 'instances[].{name:name,createdAt:createdAt,blueprintId:blueprintId,blueprintName:blueprintName,publicIpAddress:publicIpAddress}' --output table
---------------------------------------------------------------------------------------------------------------------
|                                                   GetInstances                                                    |
+-----------------+----------------+-----------------+------------------------------------------+-------------------+
|   blueprintId   | blueprintName  |    createdAt    |                  name                    |  publicIpAddress  |
+-----------------+----------------+-----------------+------------------------------------------+-------------------+
|  wordpress_4_9_8|  WordPress     |  1545700733.77  | WordPress-512MB-Ireland-1  |  63.33.xx.xx |
|  wordpress      |  WordPress     |  1602175741.603 |  TestLightsailInstance2    |  63.33.xx.xx |
|  wordpress      |  WordPress     |  1602175741.602 |  TestLightsailInstance1    |  54.7.xx.xx  |
|  wordpress      |  WordPress     |  1588098501.555 |  WordPress-1               |  3.250.xx.xx |
+-----------------+----------------+-----------------+------------------------------------------+-------------------+

**注意:**请将 region 替换为您的区域。

分配静态 IP 并将其附加到创建的 Lightsail 实例

注意:create-instances 命令会创建一个具有公有 IP 地址的 Lightsail 实例。如果您尚未为实例创建静态 IP 地址,则无论何时停止或重启实例,Lightsail 都会分配一个新的公有 IP 地址。

  1. 运行 allocate-static-ip 命令分配一个固定的公有地址。以下示例所示为在 eu-west-1 区域中分配名为 StaticIpForTestLightsailInstance1 的静态 IP 的情况:
    # aws lightsail allocate-static-ip --static-ip-name StaticIpForTestLightsailInstance1 --region eu-west-1
    {
      "operations": [
        {
          "id": "6471cedc-97f5-42d1-b35f-4f77c5f93242",
          "resourceName": "StaticIpForTestLightsailInstance1",
          "resourceType": "StaticIp",
          "createdAt": 1602180341.746,
          "location": {
            "availabilityZone": "all",
            "regionName": "eu-west-1"
          },
          "isTerminal": true,
          "operationType": "AllocateStaticIp",
          "status": "Succeeded",
          "statusChangedAt": 1602180342.011
        }
      ]
    }
    **注意:**请将 static-ip-nameregion 替换为您的值。
  2. 运行 get-static-ip 命令显示分配的静态 IP 地址的值。以下示例所示为在 eu-west-1 区域分配名为 StaticIpForTestLightsailInstance1 的静态 IP 52.210.xx.xx 的情况。
    # aws lightsail get-static-ip --static-ip-name StaticIpForTestLightsailInstance1 --region eu-west-1
    {
      "staticIp": {
        "name": "StaticIpForTestLightsailInstance1",
        "arn": "arn:aws:lightsail:eu-west-1:920893848407:StaticIp/16a97579-c51e-404c-96cc-5946528d12aa",
        "supportCode": "11178xxxxxxx/52.210.xx.xx",
        "createdAt": 1602180341.746,
        "location": {
          "availabilityZone": "all",
          "regionName": "eu-west-1"
        },
        "resourceType": "StaticIp",
        "ipAddress": "52.210.xx.xx",
        "isAttached": false
      }
    }
    **注意:**请将 static-ip-nameregion 替换为您的值。
  3. 运行 attach-static-ip 命令。以下示例所示为将名为 StaticIpForTestLightsailInstance1 的静态 IP 52.210.xx.xx 分配给 eu-west-1 区域中名为 TestLightsailInstance1 的实例的情况。
    # aws lightsail attach-static-ip --static-ip-name StaticIpForTestLightsailInstance1 --instance-name TestLightsailInstance1  --region eu-west-1
    {
      "operations": [
        {
          "id": "d9d2372f-b4b7-4838-99ae-c15f8ffb8332",
          "resourceName": "StaticIpForTestLightsailInstance1",
          "resourceType": "StaticIp",
          "createdAt": 1602180562.026,
          "location": {
            "availabilityZone": "all",
            "regionName": "eu-west-1"
          },
          "isTerminal": true,
          "operationDetails": "TestLightsailInstance1",
          "operationType": "AttachStaticIp",
          "status": "Succeeded",
          "statusChangedAt": 1602180562.026
        },
        {
          "id": "131994d8-bd25-4b47-8d81-851224b168d1",
          "resourceName": "TestLightsailInstance1",
          "resourceType": "Instance",
          "createdAt": 1602180562.026,
          "location": {
            "availabilityZone": "eu-west-1a",
            "regionName": "eu-west-1"
          },
          "isTerminal": true,
          "operationDetails": "StaticIpForTestLightsailInstance1",
          "operationType": "AttachStaticIp",
          "status": "Succeeded",
          "statusChangedAt": 1602180562.026
        }
      ]
    }
    **注意:**请将 static-ip-nameinstance-nameregion 替换为您的值。
  4. 运行 get-instances 命令以验证静态 IP 地址是否与您的 Lightsail 实例正确关联。
    # aws lightsail get-instances --region eu-west-1 --query 'instances[].{name:name,createdAt:createdAt,blueprintId:blueprintId,blueprintName:blueprintName,publicIpAddress:publicIpAddress,InstanceID:supportCode}' --output table
    ---------------------------------------------------------------------------------------------------------------------------------------------------------
    |                                                                     GetInstances                                                                      |
    +----------------------------------+------------------+----------------+-----------------+------------------------------------------+-------------------+
    |            InstanceID            |   blueprintId    | blueprintName  |    createdAt    |                  name                    |  publicIpAddress  |
    +----------------------------------+------------------+----------------+-----------------+------------------------------------------+-------------------+
    |        11178xxxxxxx/i-09f6xxxx   |  wordpress       |  WordPress     |  1602175741.602 |  TestLightsailInstance1                  |   52.210.xx.xx    |
    +----------------------------------+------------------+----------------+-----------------+------------------------------------------+-------------------+

相关信息

如何使用 AWS CLI 命令管理我的快照并为 Lightsail 实例创建备份?

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

在 Amazon Lightsail 中开启或关闭实例或磁盘的自动快照

AWS 官方
AWS 官方已更新 7 个月前