Debugging with TF_LOG and reading errors

Beginner · 5 min

Triage a failing Terraform run: read the error's three parts, tell a code error from a provider API error, and turn on TF_LOG only when you need the HTTP calls.

Before thisReading a plan and the dependency graph

Rahul Wagh · 8 min

Debugging with TF_LOG and reading errors

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Rahul WaghReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Most Terraform errors say what failed, where in your code, and often why. Read the whole message before reaching for logs.
  • Where the error comes from matters: validation and plan errors are about your code, apply errors usually come back from the cloud's API.
  • `TF_LOG` takes TRACE, DEBUG, INFO, WARN or ERROR. `TF_LOG_PATH` writes to a file, and `TF_LOG_CORE` and `TF_LOG_PROVIDER` split Terraform's logs from the provider's.
  • TRACE logs can contain credentials. Never paste one into a ticket without reading it first.

The job is half done when you read the message

A new hire runs terraform apply, sees a wall of red text and pastes the whole screen into the team chat with “it doesn’t work”. Someone replies with the answer in under a minute. The answer was in the second line of the red text, which named the argument and the file.

Terraform’s error messages are better than their reputation, because they are structured. Learn the structure and most failures take a minute.

Anatomy of an error

An error from Terraform has the same three parts every time:

  1. A summary with the kind of failure, such as an unsupported argument, an invalid value or an API error from a provider.
  2. A location, usually a file name and line number with the offending lines printed below, when the problem is in your code.
  3. A detail sentence, which often names the fix.

I cannot show real output here because there is no Terraform on this machine, so I will describe the habit instead of faking a transcript. Read the summary first, then open the file at the line named, then read the detail. Only then search the web.

Which stage failed tells you where to look

The command that printed the error is a strong hint, in the order of the loop from the earlier lesson:

  • init errors are about getting software: a provider that does not exist at that source, a version constraint nothing satisfies, a module source that cannot be downloaded, a backend you cannot reach.
  • validate and early plan errors are about your code: an argument that does not exist, a wrong type, a reference to something undefined, a cycle between resources.
  • Late plan errors often come from a provider reading the real world: a data source finding nothing, or a credential problem.
  • apply errors usually come from the cloud’s API. The provider made a call and was refused, or the thing already exists, or a quota was hit. Terraform’s job there was to carry the message back to you.

A useful rule: if the message quotes a line of your code, fix your code. If it quotes a status code and a message from a vendor, the question is about the cloud, and the fix is in permissions, quotas or the existing state of the account. Do not blame the tool for a 403.

Turning on logs

When the message is not enough, turn on logging. Both the Terraform and OpenTofu docs describe the same set of environment variables:

  • TF_LOG enables logging to stderr. The levels from most to least detail are TRACE, DEBUG, INFO, WARN, ERROR.
  • TF_LOG_PATH appends logs to a file. It does nothing unless TF_LOG is also set.
  • TF_LOG_CORE and TF_LOG_PROVIDER set levels separately for Terraform itself and for provider plugins, which is how you cut noise.
  • TF_LOG=JSON writes JSON logs at TRACE or above. The Terraform docs say the JSON encoding is not a stable interface, so only tooling written for it should read it.

A typical session, written from the docs and not run:

export TF_LOG_PROVIDER=DEBUG
export TF_LOG_PATH=./terraform.log
terraform plan
unset TF_LOG_PROVIDER TF_LOG_PATH

Then read the file, not the terminal. Search it for the HTTP status, the provider’s request ID and the first line that says error. A provider’s debug log shows the real API calls it made, which is exactly what you need for “why did this return a 403”. That is the answer a code error never gives you.

Handle logs like credentials

OpenTofu’s debugging page says that logs at TRACE “may contain sensitive details such as credentials and should be treated with care”. Provider logs carry the requests and responses they sent, and a response can include a token or a secret value. Before attaching a log to an issue, search it for Authorization, token, secret, password and anything that looks like a key, and redact it. Delete log files when you are done.

Two more habits that save time

  • Keep plan and apply separate. With plan -out you see errors that depend on your code and the real world before anything changes. Errors during apply are partial failures, and the state records what did succeed.
  • Fix the first error. Terraform often prints several errors from one root cause. The later ones are echoes, such as ten resources failing because the VPC they reference failed. Re-run after fixing the first.

When it is the tool

Occasionally the error is a crash, a Go stack trace from Terraform or a provider. That is a bug report, not your mistake. The docs ask you to include the detailed log, shared as a gist or similar. Check the provider’s issue tracker first, and pin the provider to the previous version via the lock file while you wait for a fix.

About the video

The embed is the first five minutes of Rahul Wagh’s “Terraform debugging and Validation”, from August 2022, covering how to set TF_LOG and TF_LOG_PATH and a demo of generating debug logs (0:00 to 4:52). The remainder is about validation, which I cut. It is a few years old, and the logging variables it uses have not changed in the docs I read. TechTrapture’s video in deeper goes through each log level. English was judged from title, channel and English auto-captions.

Where this fits

That closes the getting-started group. You can install, write, run, read and debug a configuration. Next the language gets proper attention, beginning with how to stop hard-coding values: input variables and validation.

Check yourself

  1. 01You set `TF_LOG_PATH=out.log` and run plan, but no file appears. What is missing?

    `TF_LOG` itself. The docs say `TF_LOG_PATH` only chooses where logs go and does not enable logging by itself.

  2. 02An apply fails with a 403 from the cloud API. Is that a Terraform bug, a code error or something else?

    Something else: the provider made a call and the API refused it, usually because the credentials lack a permission. Terraform's own logging shows the request and response if you need the detail.

  3. 03Why is it risky to attach a TRACE log to a public bug report?

    The OpenTofu docs warn TRACE output may contain sensitive details such as credentials. It records requests and responses, which can include tokens and secret values.

  4. 04Where do you set a log level to see only what the AWS provider is doing and ignore Terraform core?

    `TF_LOG_PROVIDER=DEBUG`, with `TF_LOG_PATH` if you want a file. `TF_LOG_CORE` is the counterpart for Terraform itself.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark