TF_LOG and TF_LOG_PATH), how to interpret common log entries, and best practices for secure log handling and centralized ingestion.
Key environment variables
Terraform logging is configured with environment variables:TF_LOG— sets the logging verbosity level Terraform will emit.TF_LOG_PATH— when set, Terraform writes logs to the specified file instead of streaming verbose logs to the console.
If you run Terraform on an Azure VM or build agent, install the Azure Monitor Agent to collect the
TF_LOG file and send it to a Log Analytics workspace. This enables centralized querying, filtering, and analysis of Terraform logs. See: https://learn.microsoft.com/azure/azure-monitor/agents/azure-monitor-agentTerraform logs at
DEBUG or TRACE levels can contain sensitive data such as secrets or API tokens. Treat files created via TF_LOG_PATH as sensitive: restrict access, redact before sharing, and rotate credentials if exposed.TF_LOG levels
Terraform supports these logging levels (from least to most verbose). Setting a level will include that level and all higher-severity messages.
Recommendation: use
DEBUG for typical troubleshooting. Reserve TRACE for deep investigations, and avoid those levels for routine CI runs unless necessary.
Example Terraform configuration
The following compact example is used throughout this guide. It provisions an Azure resource group and a storage account. Alocal-exec provisioner demonstrates how Terrafrom can execute local commands (note: provisioners and writing secrets to disk have security implications).
azurerm provider version and attribute availability can vary. To construct a real connection string, you may need to use provider attributes, data sources, or by retrieving account keys. Avoid writing secrets to disk where possible.
Running Terraform with logging enabled
Set the environment variables in your terminal or CI agent before running Terraform commands. Example:- When
TF_LOGis set andTF_LOG_PATHis unset, verbose logs stream to the console. - When
TF_LOG_PATHis set, detailed logs are written to the file and Terraform’s console output remains the normal, succinct Terraform summary (the plan/apply text remains readable). - For CI systems, prefer writing logs to a file and ingesting that file into your centralized logging.
Inspecting and filtering the log file
Search and filter the log file to focus on provider API calls or errors. For example, to see provider HTTP GET requests:Sample log snippets
A provider plugin error and subsequent plugin exit may appear like this (cleaned and representative):terraform plan run (console output remains user-friendly even when TF_LOG_PATH is set):
Reducing verbosity
IfTRACE or DEBUG is too noisy, reduce the level:
- Use
INFOorWARNfor routine runs. - Use
DEBUGto investigate provider-specific issues. - Use
TRACEonly when troubleshooting deep internals or when requested by provider maintainers.
Best practices for secure logging and centralized analysis
- Treat
TF_LOG_PATHoutput as sensitive data. Limit permissions and encryption-at-rest where possible. - Redact or scrub logs before sharing externally or attaching to tickets.
- Rotate credentials if they may have been exposed in logs.
- In production pipelines, send logs to a centralized logging system (e.g., Azure Log Analytics, Splunk, Elastic) and apply retention, access controls, and alerting.
- For Azure-specific ingestion, use the Azure Monitor Agent to forward log files to a Log Analytics workspace for query and correlation.
Quick reference
Summary
- Control Terraform logging with
TF_LOGand persist logs withTF_LOG_PATH. DEBUGandTRACEreveal provider and API interactions—very useful for troubleshooting, but they can expose secrets.- Centralize
TF_LOGoutputs with logging agents (Azure Monitor Agent → Log Analytics) for better analysis and retention. - Inspect provider HTTP calls in logs to understand what Terraform requested and how the cloud provider responded.
Links and references
- Terraform CLI environment variables: https://developer.hashicorp.com/terraform/cli/config/environment-variables#tf_log
- Azure Monitor Agent: https://learn.microsoft.com/azure/azure-monitor/agents/azure-monitor-agent
- Log Analytics workspace: https://learn.microsoft.com/azure/azure-monitor/logs/log-analytics-workspace
- azurerm provider docs: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs