Cleanup of Terraform Apply partial fails

This is unfortunately a big weakness of Terraform. If it fails part way through, it will fail to record in the state file the changes it made, which will leave things in an inconsistent state. If you then re-run apply, you’ll either get an error, because some resource already exists with that name (but Terraform doesn’t know about it because it failed to save it in state) or for resources that don’t enforce unique identities, you’ll get duplicate copies.

There are no good solutions at the moment. Your only options are ugly workarounds such as:

  1. Manually find everything that got deployed, delete it, and re-run terraform apply.
  2. Manually find everything that got deployed, and for each such resource, run terraform import.

See also https://github.com/hashicorp/terraform/issues/20718.

1 Like