How can I use AWS CLI commands to manage my Lightsail instance?

8 minute read
0

I want to use AWS Command Line Interface (AWS CLI) commands to manage my Amazon Lightsail instances.

Short description

The following are common scenarios to use AWS CLI commands on your Lightsail instances:

  • To list all the Lightsail plans (bundles) and blueprints available in an AWS Region
  • To use a specific bundle and blueprint to create a Lightsail instance
  • To list all the Lightsail Instances in a particular Region
  • To allocate a static IP address, and then attach it to the instance and verify it

Note:

macOS

Remove the decimal point from the timestamp and any digits to the right of the decimal point. Then, run the following command:

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

Linux

Run the following command:

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

Windows

Use a converter to convert the timestamp, such as epochconverter.com.

Resolution

List the Lightsail bundles and blueprints available in a Region

Run the get-bundles command to list bundles that are available for purchase in your Region. The following is an example output:

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

Note: Replace eu-west-1 with your Region.

Run the get-blueprints command to list all images or blueprints in your Region. The following is an example output:

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

Note: Replace eu-west-1 with your Region.

Filter the blueprints for the platform that you need. For example, the following command filters for blueprints with the Linux/Unix Platform OS:

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

Use a specific bundle and blueprint to create a Lightsail instance

  1. Run the get-blueprint command. The following example gets a WordPress blueprint.
    # 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   |
    Note: Replace eu-west-1 with your Region and the WordPress information with your blueprint information.
  2. Run the create-instances command to create Amazon Lightsail Instances. The following example uses a WordPress blueprint and the micro_2_o bundle to create an Amazon Lightsail instance in the eu-west-1 Region.
    # 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
        }
      ]
    }
    Note: Replace instance-names, blueprint-id, bundle-id, and region with your values. To use a single API call to create multiple instances, specify parameters with --instance-names, such as LightsailInstance1 and LightsailInstance2 . The following example uses a WordPress blueprint and the micro_2_0 bundle to create two Lightsail instances in the eu-west-1 Region.
    # 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
        }
      ]
    }

List all Lightsail instances in a Region

Run the get-instances command to display Lightsail instances in a specific Region. The following example shows the Lightsail instances in the eu-west-1 Region.

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

Note: Replace region with your Region.

Allocate a Static IP and attach it to the Lightsail instance created

Note: The create-instances command creates a Lightsail instance with a public IP address. If you haven't created a static IP address for your instance, then Lightsail assigns a new public IP address whenever you stop or restart the instance.

  1. Run the allocate-static-ip command to allocate a fixed public address. The following example shows the allocation of a static IP named StaticIpForTestLightsailInstance1 in the eu-west-1 Region:
    # 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
        }
      ]
    }
    Note: Replace static-ip-name and region with your values.
  2. Run the get-static-ip command to display the value for allocated static IP addresses. The following example shows the allocation of a static IP 52.210.xx.xx named StaticIpForTestLightsailInstance1 in the eu-west-1 Region.
    # 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
      }
    }
    Note: Replace static-ip-name and region with your values.
  3. Run the attach-static-ip command. The following example shows the allocation of a static IP 52.210.xx.xx named StaticIpForTestLightsailInstance1 to the instance named TestLightsailInstance1 in the eu-west-1 Region.
    # 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
        }
      ]
    }
    Note: Replace static-ip-name, instance-name, and region with your values.
  4. Run the get-instances command to verify that the static IP address correctly associated with your Lightsail instance.
    # 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    |
    +----------------------------------+------------------+----------------+-----------------+------------------------------------------+-------------------+

Related information

How do I use AWS CLI commands to manage my snapshots and create backups for my Lightsail instances?

How can I manage static IP addresses on my Lightsail instances using AWS CLI commands?

Turn on or turn off automatic snapshots for instances or disks in Amazon Lightsail

AWS OFFICIAL
AWS OFFICIALUpdated 3 days ago