Skip to main content

Logging for Debugging in Terraform

Terraform logging becomes very important when you are getting an error while executing terraform commands and you want to debug the issue.

In Terraform, You can enable logging by using environment variables. Terraform logging mainly depends on two environment variables. These two variables are TF_LOG and TF_LOG_PATH.

TF_LOG and TF_LOG_PATH

1. To set any value to TF_LOG enables logs to appear on stderr.

2. You can set TF_LOG to one of the log levels TRACE, DEBUG, INFO, WARN or ERROR to change the verbosity of the logs. TRACE is the most verbose and it is the default log level when TF_LOG is set to something else other than an accepted Log Level.

3. To save the logs to a file, use TF_LOG_PATH variable but remember, TF_LOG must be set in order for any logging to be stored on a file. Logs won't be saved in a file only by setting TF_LOG_PATH.

Setting Environment Variables for Current Session

To set these variables for a current session in PowerShell or Bash, use below syntaxes. After setting up these environment variables, you will start seeing logs on console in next terraform command executions.

PowerShell

> $env:TF_LOG="TRACE"
> $env:TF_LOG_PATH="./terraform.log"

Bash

$ export TF_LOG="TRACE"
$ export TF_LOG_PATH="./terraform.log" 

Note

1. To save these variables permanently, use .bashrc file for Linux systems.
2. Use ".gitignore" file to not commit logs in version control repositories.

References