How do I use the AWS CLI to manage static IP addresses on my Lightsail instances?

4 minutos de lectura
0

I want to use the AWS CLI to detach my static IP address from my Amazon Lightsail instance and attach it to a new Lightsail instance.

Short description

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

Important:

The AWS CLI output displays timestamps in Unix epoch time. Use one of the following methods to convert the timestamp to UTC:

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:

Use a conversion tool to change the timestamp, such as the epoch and Unix timestamp conversion tool on the EpochConverter website.

Resolution

Detach the static IP address from an existing Lightsail instance

Run the detach-static-ip command to detach the static IP address from the instance:

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

Note: In the preceding example, replace the --static-ip-name and --region with your values. The example detaches the static IP address from an instance in the eu-west-1 AWS Region.

Attach the static IP address to a new Lightsail instance

To attach the static IP address to your Lightsail instance, complete the following steps:

  1. Run the attach-static-ip command:
    # 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
            }
        ]
    Note: In the preceding command, replace --static-ip-name, --instance-name, and --region with your values. The command uses a backup to attach a static IP address to a new instance.
  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     |
    +----------------------------------+------------------+----------------+------------+-----------------+------------------------------------------+-------------------+

Related information

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

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

Activate or deactivate automatic snapshots for Lightsail instances and disks

OFICIAL DE AWS
OFICIAL DE AWSActualizada hace 25 días