🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Terraform Tutorias: CLI environment and predefined/meta variables Complete Reference

Terraform CLI environment variables reference

Environment VariableDescriptionExample Usage
TF_LOGControls logging verbosity. Values: TRACE, DEBUG, INFO, WARN, ERROR, OFF.export TF_LOG=DEBUG
TF_LOG_PATHSpecifies log file path to store logs.export TF_LOG_PATH=./terraform.log
TF_INPUTDetermines if Terraform prompts for user input. Set to false to disable.export TF_INPUT=false
TF_VAR_nameSets Terraform input variables from the environment. Example: TF_VAR_region.export TF_VAR_region=us-west-1
TF_CLI_ARGS and TF_CLI_ARGS_nameDefines default CLI arguments for Terraform commands. Example: TF_CLI_ARGS_plan.export TF_CLI_ARGS=’-input=false’
TF_DATA_DIRChanges Terraform data directory from the default `.terraform`.export TF_DATA_DIR=/custom/data/dir
TF_WORKSPACESpecifies the workspace to use in Terraform.export TF_WORKSPACE=production
TF_IN_AUTOMATIONIndicates if Terraform is running in an automated environment (CI/CD).export TF_IN_AUTOMATION=true
TF_REGISTRY_DISCOVERY_RETRYNumber of retries for Terraform Registry discovery.export TF_REGISTRY_DISCOVERY_RETRY=5
TF_REGISTRY_CLIENT_TIMEOUTSets timeout duration for Terraform Registry client operations.export TF_REGISTRY_CLIENT_TIMEOUT=30
TF_STATE_PERSIST_INTERVALDefines how frequently Terraform writes its state file.export TF_STATE_PERSIST_INTERVAL=10
TF_CLI_CONFIG_FILESpecifies custom path for Terraform CLI configuration file.export TF_CLI_CONFIG_FILE=$HOME/.terraformrc
TF_PLUGIN_CACHE_DIRSpecifies the directory for Terraform plugin cache.export TF_PLUGIN_CACHE_DIR=$HOME/.terraform.d/plugin-cache
TF_IGNOREDefines patterns for files and directories that Terraform should ignore.export TF_IGNORE=’*.bak’

Terraform predefined and meta variables reference

Built-in VariableDescription
terraform.workspaceReturns the current workspace name.
path.moduleReturns the current module path.
path.rootReturns the root module path.
path.cwdReturns the current working directory.
count.indexCurrent index in a count loop.
each.key, each.valueCurrent key-value in a for_each loop.
var.<variable_name>References an input variable.
local.<local_variable>References a local variable.
module.<module_name>.<output>Retrieves an output from a child module.
selfRefers to current resource instance.
depends_onExplicitly sets dependencies between resources.
env.<ENV_VAR>Retrieves environment variables from the shell.

Comprehensive Guide: Terraform CLI Environment Variables & Predefined Variables

1. Terraform CLI Environment Variables Reference

Terraform allows configuring its behavior using various environment variables. These variables control logging, input handling, workspace management, and registry discovery settings. Below is a detailed guide with use cases and examples.

1.1 Logging & Debugging

TF_LOG

  • Description: Controls Terraform logging verbosity.
  • Possible Values: TRACE, DEBUG, INFO, WARN, ERROR, OFF
  • Use Case: Useful for debugging Terraform execution.
  • Example: export TF_LOG=DEBUG terraform apply

TF_LOG_PATH

  • Description: Specifies a log file path to store logs.
  • Use Case: Useful when debugging Terraform automation pipelines.
  • Example: export TF_LOG_PATH=./terraform.log terraform apply

1.2 Input Handling

TF_INPUT

  • Description: Determines if Terraform prompts for user input. Set to false to disable interactive prompts.
  • Use Case: Useful in CI/CD environments.
  • Example: export TF_INPUT=false terraform apply

1.3 Variable Handling

TF_VAR_name

  • Description: Sets Terraform input variables from the environment.
  • Use Case: Allows dynamic configuration of Terraform variables without modifying the .tf file.
  • Example: export TF_VAR_region=us-west-1 terraform apply

TF_CLI_ARGS and TF_CLI_ARGS_name

  • Description: Defines default CLI arguments for Terraform commands.
  • Use Case: Ensures that Terraform always runs with specific flags.
  • Example: export TF_CLI_ARGS='-input=false' terraform apply export TF_CLI_ARGS_plan='-refresh=false' terraform plan

1.4 Terraform State & Data Management

TF_DATA_DIR

  • Description: Changes Terraform data directory from the default .terraform.
  • Use Case: Useful when running multiple Terraform executions in parallel.
  • Example: export TF_DATA_DIR=/custom/data/dir terraform init

TF_WORKSPACE

  • Description: Specifies the workspace to use in Terraform.
  • Use Case: Automate Terraform workspace switching.
  • Example: export TF_WORKSPACE=production terraform apply

TF_IN_AUTOMATION

  • Description: Indicates if Terraform is running in an automated environment (CI/CD).
  • Use Case: Helps Terraform suppress interactive prompts.
  • Example: export TF_IN_AUTOMATION=true terraform apply

1.5 Terraform Registry & Plugin Management

TF_REGISTRY_DISCOVERY_RETRY

  • Description: Number of retries for Terraform Registry discovery.
  • Use Case: Useful when working in unstable network environments.
  • Example: export TF_REGISTRY_DISCOVERY_RETRY=5

TF_REGISTRY_CLIENT_TIMEOUT

  • Description: Sets timeout duration for Terraform Registry client operations.
  • Use Case: Useful in slow networks.
  • Example: export TF_REGISTRY_CLIENT_TIMEOUT=30

TF_PLUGIN_CACHE_DIR

  • Description: Specifies the directory for Terraform plugin cache.
  • Use Case: Helps speed up Terraform runs by avoiding plugin downloads.
  • Example: export TF_PLUGIN_CACHE_DIR=$HOME/.terraform.d/plugin-cache

1.6 Miscellaneous Terraform Configurations

TF_STATE_PERSIST_INTERVAL

  • Description: Defines how frequently Terraform writes its state file.
  • Example: export TF_STATE_PERSIST_INTERVAL=10

TF_CLI_CONFIG_FILE

  • Description: Specifies a custom path for the Terraform CLI configuration file.
  • Example: export TF_CLI_CONFIG_FILE=$HOME/.terraformrc

TF_IGNORE

  • Description: Defines patterns for files and directories that Terraform should ignore.
  • Example: export TF_IGNORE='*.bak'

2. Terraform Predefined and Meta Variables Reference

Terraform provides a set of predefined variables that allow access to system metadata.

Built-in VariableDescription
terraform.workspaceReturns the current workspace name.
path.moduleReturns the current module path.
path.rootReturns the root module path.
path.cwdReturns the current working directory.
count.indexCurrent index in a count loop.
each.key, each.valueCurrent key-value in a for_each loop.
var.<variable_name>References an input variable.
local.<local_variable>References a local variable.
module.<module_name>.<output>Retrieves an output from a child module.
selfRefers to the current resource instance.
depends_onExplicitly sets dependencies between resources.
env.<ENV_VAR>Retrieves environment variables from the shell.

Example Usage:

output "workspace" {
  value = terraform.workspace
}
output "current_path" {
  value = path.module
}

By understanding and utilizing these Terraform environment variables and predefined expressions, users can optimize their infrastructure as code (IaC) workflows efficiently.

variable "instance_count" {
type = number
default = 3
}
variable "region" {
type = string
default = "us-east-1"
}
locals {
env_name = "production"
base_path = path.root
}
provider "aws" {
region = var.region
}
module "network" {
source = "./network"
}
resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-12345678"
instance_type = "t2.micro"
tags = {
Name = "instance-${count.index}"
Workspace = terraform.workspace
Module_Path = path.module
Root_Path = path.root
CWD_Path = path.cwd
Environment = local.env_name
}
}
resource "aws_s3_bucket" "example" {
for_each = {
bucket1 = "my-app-bucket1"
bucket2 = "my-app-bucket2"
}
bucket = each.value
tags = {
Bucket_Name = each.key
}
}
resource "aws_lambda_function" "example" {
depends_on = [aws_instance.example]
function_name = "MyLambda"
role = "arn:aws:iam::123456789012:role/execution_role"
handler = "index.handler"
runtime = "nodejs14.x"
}
output "workspace_name" {
value = terraform.workspace
}
output "module_path" {
value = path.module
}
output "root_path" {
value = path.root
}
output "working_directory" {
value = path.cwd
}
output "current_region" {
value = var.region
}
output "environment_variable" {
value = env.USER
}
output "network_output" {
value = module.network.vpc_id
}
output "instance_count" {
value = var.instance_count
}
output "local_env_name" {
value = local.env_name
}
output "each_example" {
value = { for k, v in aws_s3_bucket.example : k => v.bucket }
}
output "count_example" {
value = [for i in aws_instance.example : i.id]
}
output "depends_on_example" {
value = aws_lambda_function.example.function_name
}
Subscribe
Notify of
guest


0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x