In this post, I’ll document how to use AWS CLI to delete a few Lightsail resources. I have never done this before, so this would be a good exercise for me. To make it less boring, I’m writing this post while I’m deleting these resources.

First step, I need to look for the instance name [1].

❯ aws lightsail get-instances --query 'instances[].name'
[
    "WordPress-1"
]

The above output is the name of the instance I’d like to delete. Wit its name, I can delete the instance using the following command line [2]:

❯ aws lightsail delete-instance --instance-name 'WordPress-1'
{
    "operations": [
        {
            "id": "8b661373-xxxx-xxxx-8463-4d28e619a276",
            "resourceName": "StaticIp-1",
            "resourceType": "StaticIp",
            "createdAt": 1696511633.86,
            "location": {
                "availabilityZone": "all",
                "regionName": "ap-southeast-2"
            },
    ...
}

After a short while later, using the command below, I could verify that my Lightsail instance ‘WordPress-1’ no longer exists.

❯ aws lightsail get-instances --query 'instances[].name'
[]

This could have been the last step, but I just remember that I also have a static IP assigned to the deleted Lightsail instance. The static IP is no longer in use and should be deleted too!

I’m going to start with finding the name of the static IP [3].

❯ aws lightsail get-static-ips | jq '.staticIps[] | {name:.name,isAttached:.isAttached}'
{
  "name": "StaticIp-1",
  "isAttached": false
}

Having found the name of the static IP, StaticIp-1, I can proceed with the deletion (alternatively known as ‘release’) [4]:

❯ aws lightsail release-static-ip --static-ip-name StaticIp-1
{
    "operations": [
        {
            "id": "83ec4f5a-xxxx-xxxx-xxxx-e1a0f6bd4451",
            "resourceName": "StaticIp-1",
            "resourceType": "StaticIp",
            "createdAt": 1696512709.88,
            "location": {
                "availabilityZone": "all",
                "regionName": "ap-southeast-2"
            },
            "isTerminal": true,
            "operationDetails": "",
            "operationType": "ReleaseStaticIp",
            "status": "Succeeded",
            "statusChangedAt": 1696512709.88
        }
    ]
}

Let’s verify, shall we?

❯ aws lightsail get-static-ips
{
    "staticIps": []
}

Great, the static IP is now gone. But then, I just remember that I also want to delete the DNS zone which was created for testing purpose.

So what should I start first? Of course, find the name of the domain name. This can be done using the following command with get-domains [5]:

❯ aws lightsail get-domains

An error occurred (InvalidInputException) when calling the GetDomains operation: Domain-related APIs are only available in the us-east-1 Region. Please set your Region configuration to us-east-1 to create, view, or edit domain resources.

Ahh… the get-dmains sub-command requires the region set to us-east-1.

❯ aws lightsail get-domains --region us-east-1 | jq '.domains[].name'
"sudbomain.example.com"

That looks better. With the domain name found, to delete it use the sub-command delete-domain [6]:

❯ aws lightsail delete-domain --domain-name "sudbomain.example.com" --region us-east-1
{
    "operation": {
        "id": "740f0872-xxxx-xxxx-a35f-bb680fb6c91f",
        "resourceName": "sudbomain.example.com",
        "resourceType": "Domain",
        "createdAt": 1696514510.275,
        "location": {
            "availabilityZone": "all",
            "regionName": "global"
        },
        "isTerminal": true,
        "operationDetails": "",
        "operationType": "DeleteDomain",
        "status": "Succeeded",
        "statusChangedAt": 1696514510.275
    }
}

Well, that’s it. I have no more resources that I could get rid.

References: