使用 AWS re:Post 即表示您同意 AWS re:Post 使用條款

如何使用 AWS CLI 命令管理 Lightsail 執行個體?

5 分的閱讀內容
0

我想使用 AWS Command Line Interface (AWS CLI) 命令,來管理我的 Amazon Lightsail 執行個體。

簡短說明

以下是在 Lightsail 執行個體上使用 AWS CLI 命令的常見案例:

  • 列出 AWS 區域中所有可用的 Lightsail 計畫 (套件) 和藍圖
  • 使用特定的套件和藍圖來建立 Lightsail 執行個體
  • 列出特定區域中的所有 Lightsail 執行個體
  • 配置靜態 IP 位址,然後將其連接至執行個體並進行驗證

注意:

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 命令。下列範例會顯示在 eu-west-1 區域中,將名為 StaticIpForTestLightsailInstance1 的靜態 IP 52.210.xx.xx 配置為名為 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 官方已更新 9 個月前