How to enable debug logging when using Terraform?

Follow
Table of Contents

Task

If you encounter an issue with the Rancher2 Terraform Provider it may be helpful to capture debug output from Terraform, so that you can provide this to Rancher Support. This article details how to enable debug output and set the log location for Terraform commands such as terraform apply.

Pre-requisites

Resolution

You can set the Terraform log level and location via the TF_LOG and TF_LOG_PATH environment variables.

Set log level with TF_LOG

The environment variableTF_LOG defines the log level. Valid log levels are (in order of decreasing verbosity): TRACE, DEBUG, INFO, WARN or ERROR.

Set the log level in your environment with the appropriate command (substituting your preferred log level):

Bash: export TF_LOG="DEBUG"

PowerShell: $env:TF_LOG="DEBUG"

Redirect Terraform logs with TF_LOG

The environment variable TF_LOG_PATH specifies the file in which Terraform will write logs. If TF_LOG_PATH is not set, output is sent to standard output and error in the terminal. If the environment variable is set, Terraform will append logs from each run to the specified file.

Set the Terraform log location in your environment with the appropriate command (substituting the path to your preferred file):

Bash: export TF_LOG_PATH="tmp/terraform.log"

PowerShell: $env:TF_LOG_PATH="C:\tmp\terraform.log"

To set them permanently, you can add these environment variables to your .profile, .bashrc, PowerShell profile (if it exists, the path is stored in $profile environment variable) file, or the appropriate profile for your chosen shell.

N.B. As this will append the log with the Terraform output every time you run a Terraform command, you may wish to configure log rotation for the chosen log file if this is enabled permanently. Alternatively, disable logging to file once you have finished troubleshooting.

Further reading

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.