0
votes

I have an application that is constantly creating/destroying virtual machine resources in AWS using Terraform. It works most of the time, but on occasion the Terraform destroy command fails to fully cleanup the AWS instance.

In this case, since I'm already using Terraform with success, I'd like to setup a CRON job that automatically discovers all the running AWS instances, determines if any of them failed to terminate properly, and then cleans up all the bad resources.

Rather than use a different tool I'd like to do this using Terraform. Is it possible to list all running VMs in AWS using Terraform if Terraform does not currently have any of them in the state file?

On a related note, I'd also like to keep this application as Cloud Agnostic as possible, so I don't want to rely on the AWS CLI if I can help it.

1
Is there any reason you don't want to fix the original problem that Terraform isn't properly destroying some instances but is removing it from the state file? This sounds like a bug of a level that I haven't seen in Terraform for years as if it fails to delete an instance it should stay in the state file and so rerunning destroy should delete the instance (or at least try again).ydaetskcoR

1 Answers

0
votes

It does sound like you need to investigate the problem a little more from a 'why am I having this problem in the first place'. However, you could utilise a tool like terraforming to ad-hoc pull in EC2 instances and then do some determination as to whether you need to terminate.

A vague answer as you probably should investigate the termination problem first.