Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

Terraform Troubleshooting - Enable Logging

·495 words·3 mins· 100 views · 5 likes ·
Terraform troubleshooting

If you run into a problem with Terraform, it can be helpful to capture Terraform’s debug output, so you can analyze it or use it to open an issue case. In this post, I want to show you how to enable terraform logging and set the log location on Windows and Linux operating systems.

Introduction>

Introduction #

Terraform offers verbose logging, which gives way more information making it easier to debug through the issue. To enable the different logging levels, you need to configure two environment variables, TF_LOG and TF_LOG_PATH. The TF_LOG environment variable sets the verbosity level of the log. Five logging levels can be used for debugging purposes:

  • TRACE – Has the most elaborate verbosity and shows every step taken by Terraform.
  • DEBUG - Describes what is happening internally in a more concise way compared to TRACE.
  • INFO - Shows general high-level messages about the execution process.
  • WARN – Logs warnings may indicate misconfiguration or errors but are not critical to execution.
  • ERROR – Displays errors that prevent Terraform from continuing.

The next thing you need to set is the Log File Path, and for that, you should use the environment variable TF_LOG_PATH. This variable will create the specified file and append logs generated by Terraform. If TF_LOG_PATH is not set, the output is sent to standard output and error in the terminal.

Important: Remember to Use .gitignore file to ignore .log files.

Enable Terraform logging>

Enable Terraform logging #

If you want to enable this logging level to temporarily troubleshoot a problem, you can configure these settings only for your working session. For this, you should run the following commands depending on your operating system.

export TF_LOG="TRACE"
export TF_LOG_PATH=./logs/terraform_TRACE.log
$env:TF_LOG="TRACE"
$env:TF_LOG_PATH="./logs/terraform_TRACE.log"

As of version 0.15, terraform allows you to generate logs from the Terraform provider and the core application separately, using the TF_LOG_CORE or TF_LOG_PROVIDER environment variables. In this case, you should run the following commands depending on your operating system.

export TF_LOG_CORE=ERROR
export TF_LOG_PROVIDER=TRACE
$env:TF_LOG_CORE="ERROR"
$env:TF_LOG_PROVIDER="TRACE"
Set permanent environment variables>

Set permanent environment variables #

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.

echo "export TF_LOG=TRACE" >> .bashrc
echo "export TF_LOG_PATH=./logs/terraform_TRACE.log" >> .bashrc

And then close your terminal, and reopen it. If you work with Windows operating systems, edit the PowerShell profile, invoke your favorite editor on the file, or always use the notepad editor.

notepad $profile

And then add the following entries to the file:

$env:TF_LOG="TRACE"
$env:TF_LOG_PATH="./logs/terraform_TRACE.log"

And then save, close your console, and reopen it.

Disable terraform logging>

Disable terraform logging #

To remove a log stream, unset the environment variable you do not need, and clear the TF_LOG_ environment variable by running the following command.

unset TF_LOG
Remove-Item -Path env:TF_LOG

Thanks for reading my post. I hope you find it helpful.

For more information on debugging Terraform, see this  link.