Better Terraform Testing Logging and Error Messages

I am getting the following error messages running Terraform test:

Error: Failed to clean up after tests

│ Due to errors during destroy, test suite “defaults” has left behind an object for module.server.module.ec2.aws_iam_instance_profile.instance, with the following identity

However, looks like Terraform test is failing before then, as the resources are not being created, but I can’t pinpoint that error since there’s no additional output. It’d be helpful showing additional logging from the test, such as the output from a plan/apply, much like how terratest shows the creating and deleting of resources in the log output.

It might also be helpful if terraform test accepted arguments such as -verbose or -vv to increase the level of logging.

1 Like

Hi @kevingunn-wk! Thanks for this feedback.

The terraform test command as currently implemented is performing essentially the same steps that terraform plan and terraform apply would, but is doing them at a different level of abstraction than just running those commands separately and so unfortunately it’s not straightforward for it to just reproduce the output those commands would’ve produced. However, it does seem possible for it to show similar information in principle, because it has access to all the same data that those commands can access.

With that said, with the testing experiment as it currently exists you could potentially run terraform test with the environment variable TF_LOG=debug set and then you’ll see Terraform’s internal debug logging. Those logs are intended to help with developing Terraform itself rather than with debugging Terraform modules, but if you can read through the “noise” then you can hopefully get a sense of what exactly failed.

Another way to go, which is actually probably the more direct answer to your suggestion, is to switch into the directory containing your test configuration and then use the normal Terraform commands in there, since the test configuration is itself a valid Terraform configuration:

cd tests/example
terraform init
terraform apply
# (and then once you're done debugging)
terraform destroy

Unfortunately we had to shift our focus away from the testing experiment for the moment and so I’m still collecting and recording feedback but we aren’t currently actively developing a new iteration. However, I am looking forward to returning to this for a second iteration based on the great feedback a bit later in the year, so thanks for sharing this!

2 Likes