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

Last updated: 2021-01-11

I want to detach my static IP address from my Amazon Lightsail instance and attach it to a new Lightsail instance. How can I do this using AWS Command Line Interface (AWS CLI) commands?

Short description

For a list of Amazon Lightsail AWS CLI commands, see the AWS CLI Command Reference and the Amazon Lightsail API Reference.

Important: Keep in mind the following when using AWS CLI commands:

macOS:

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

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

Linux:

Run the following command:

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

Windows:

Convert the timestamp using a converter, such as epochconverter.com.

Resolution

Detaching the static IP address from an existing Lightsail instance

Run the detach-static-ip command to detach the static IP address from the instance. The following example detaches the static IP address from an instance in the eu-west-1 Region. Replace the --static-ip-name and --region with the appropriate values for your request.

# 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
        }
    ]
}

Attaching the static IP address to a new Lightsail instance

1.    Run the attach-static-ip command to attach a static IP address to a new instance from backup. Replace --static-ip-name, --instance-name, and --region with the appropriate values for your request.

# 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.    Run the get-instances command to verify that the static IP address is assigned to your instance.

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