Lesson 3 of 3 Importing, Inspecting and Troubleshooting

Verbose Logging

When an error message is not enough, Terraform's logs show what core and the providers are doing. This lesson covers the logging environment variables and when to use them.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe Terraform's logging environment variables and levels.
  • Explain when verbose logging helps and its risks.

Enabling logs

Terraform writes detailed logs when the TF_LOG environment variable is set. The levels, from most to least verbose, are TRACE, DEBUG, INFO, WARN and ERROR. TF_LOG=JSON produces TRACE-level output encoded as JSON for tools, and any value that is not a level also enables TRACE. Logs go to standard error unless TF_LOG_PATH names a file, to which they are appended.

export TF_LOG=DEBUG
export TF_LOG_PATH=./terraform-debug.log
terraform plan

unset TF_LOG TF_LOG_PATH

TF_LOG_PATH does not enable logging on its own: without a level, Terraform creates the file but writes nothing to it, so TF_LOG must also be set. To focus on one side, TF_LOG_CORE sets the level for Terraform core and TF_LOG_PROVIDER for provider plugins, using the same level names.

VariablePurpose
TF_LOGEnable logging for core and providers at a level, or JSON
TF_LOG_CORESet the level for Terraform core only
TF_LOG_PROVIDERSet the level for provider plugins only
TF_LOG_PATHAppend logs to a file; requires logging to be enabled

The following diagram shows the logging settings and what a provider log can reveal.

Using provider debug logs to find a failing API call. TF_LOG_PROVIDER=DEBUG with TF_LOG_PATH records each AWS request; TF_LOG_PATH alone writes nothing. The log shows the failing call is PutBucketPolicy, denied by a bucket policy that rejects changes from outside a VPC endpoint. The team fixes the policy, unsets the variables and deletes the log.
The log line is abbreviated.
Scenario questions

Turning on logs

Exam skills 7.3.1

Configure Terraform's logging variables correctly.

Log in to try this activity Interactive activities are free with an account.

When to use verbose logging

Normal output is enough for most errors. Verbose logs help when the cause is hidden: a provider returns an unclear error, authentication fails in a way the message does not explain, an operation hangs, a network or proxy issue blocks API calls, or Terraform behaves differently from the plan. Provider logs at DEBUG or TRACE typically show each API request and response, which often reveals the real error.

Logs are also required for bug reports to HashiCorp or provider maintainers, together with the Terraform and provider versions. If Terraform itself crashes, it prints a panic message with a stack trace to the terminal, which should be included in the report.

Logs can contain secrets

TRACE and DEBUG logs can include secrets, tokens and personal data from API requests. Review and redact logs before sharing them, and delete log files when you have finished.

Sort into groups

Do logs help here?

Exam skills 7.3.2

Decide whether verbose logging is the right next step for each problem.

Log in to try this activity Interactive activities are free with an account.
Worked example

An unexplained permission error

An apply fails with "AccessDenied" for an S3 operation, but the engineer's role appears to have the permission. The error does not say which API call failed.

The decisive need is to see the provider's API calls.

Running the apply with TF_LOG_PROVIDER=DEBUG and TF_LOG_PATH=s3-debug.log records each request the AWS provider makes. The log shows that the failing call is PutBucketPolicy on a bucket whose policy denies changes from outside a specific VPC endpoint, which the role's permissions cannot override. The team fixes the bucket policy, unsets the variables and deletes the log file.

Check yourself

Does setting only TF_LOG_PATH enable logging?

Show answer

No. Terraform creates the file but writes nothing to it until TF_LOG, TF_LOG_CORE or TF_LOG_PROVIDER sets a level.

Exam Tips

Remember for the exam

  • TF_LOG levels: TRACE > DEBUG > INFO > WARN > ERROR; JSON gives TRACE as JSON.
  • TF_LOG_PATH needs TF_LOG set.
  • TF_LOG_CORE and TF_LOG_PROVIDER split core and provider logs.
  • Logs can contain secrets.