tinny
tinny

Reputation: 4257

Terminate instances never returns terminated status aws sdk

Im using the aws Java SDK with some Groovy code.

Ive started 2 ec2 instances then ive requested that they be terminated. I then want to wait until these instances terminate before deleting other resources that are dependant on these instances.

Ive got the following code checking for the termination of the instances. (the while loop never exits)

//instanceIds == a list of instance ids as strings. Ive previously requested that these instances be terminated     
//Terminate instances
        TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest()
        terminateInstancesRequest.setInstanceIds(instanceIds)   
        TerminateInstancesResult terminateInstancesResult = ec2.terminateInstances(terminateInstancesRequest)

        //Wait for instances to terminate
        terminateInstancesResult.getTerminatingInstances().each {

            while (it.getCurrentState().name != "terminated") {
                Thread.sleep(5000)
                println it.getCurrentState().name
                println it.getCurrentState().code
            }

        }

I get the following printed to the console

shutting-down
32
shutting-down
32
etc..

E.g. I never get a terminated state yet if I login to the aws console I can see that these instances get terminated... whats going on here? Why don't I ever get a status of terminated returned?

Thanks

Upvotes: 2

Views: 1532

Answers (1)

Michael Rutherfurd
Michael Rutherfurd

Reputation: 14045

You need to refresh the state. Use the instances returned from terminate as input to ec2.describeInstances(). You can then check the state of the described instances to determine when they finish terminating.

Upvotes: 2

Related Questions