1

Using Windows server 2016 instances.

I want the instance to run a local command before it terminates.

I can have a script run when windows is shutting down.

I would like the script to check if the instance itself is in the terminating state and if so run some clean up stuff.

Not sure from the docs, is there a way to check if the local instance is being terminated from the instance itself? https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html#instancedata-data-categories

This returns InstanceStatuses:

aws ec2 describe-instance-status --include-all-instances --instance-ids i-123123123

Which looks like:

{
    "InstanceStatuses": [
        {
            "AvailabilityZone": "us-west-2a",
            "InstanceId": "i-123123123",
            "InstanceState": {
                "Code": 16,
                "Name": "running"
            },
            "InstanceStatus": {
                "Details": [
                    {
                        "Name": "reachability",
                        "Status": "passed"
                    }
                ],
                "Status": "ok"
            },
            "SystemStatus": {
                "Details": [
                    {
                        "Name": "reachability",
                        "Status": "passed"
                    }
                ],
                "Status": "ok"
            }
        }
    ]
}

What does the state looks like when the instance is "terminating"? Not already terminated but pending termination? How can I check if an instance is in the pending termination state?

  • Not according to the docs. – Michael Hampton Nov 9 '18 at 22:50
  • yup there is no terminating state only "shutting down" – red888 Nov 12 '18 at 21:45
1

What you're calling the terminating state AWS refers to as shutting-down.

You can use describe-instances and use --query or jq to parse out the information you want.

Example with --query:

aws ec2 describe-instances --instance-ids i-070ca5c669933b949 --query 'Reservations[].Instances[].State.Code

Will return the instance status code (details below):

[ 48 ]

Example with jq:

aws ec2 describe-instances --instance-ids i-070ca5c669933b949 |jq .Reservations[].Instances[].State.Code

Outputs:

48

You can also get the output in text:

aws ec2 describe-instances --instance-ids i-070ca5c669933b949 |jq .Reservations[].Instances[].State.Name

Will output:

"terminated"

Below is the pertinent Output from the command and the codes that correspond to instance state:

State -> (structure)
    The current state of the instance.

    Code -> (integer)
        The low byte represents the state. The high byte is used for internal purposes and should be ignored.

        0 : pending
        16 : running
        32 : shutting-down
        48 : terminated
        64 : stopping
        80 : stopped
    Name -> (string)
        The current state of the instance.

References

https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-lifecycle.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.