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.
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 Variable
Description
terraform.workspace
Returns the current workspace name.
path.module
Returns the current module path.
path.root
Returns the root module path.
path.cwd
Returns the current working directory.
count.index
Current index in a count loop.
each.key, each.value
Current 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.
self
Refers to the current resource instance.
depends_on
Explicitly sets dependencies between resources.
env.<ENV_VAR>
Retrieves environment variables from the shell.
Example Usage:
output "workspace" {
value = terraform.workspace
}
Code language:JavaScript(javascript)
output "current_path" {
value = path.module
}
Code language:JavaScript(javascript)
By understanding and utilizing these Terraform environment variables and predefined expressions, users can optimize their infrastructure as code (IaC) workflows efficiently.
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow , and SEO strategies at Wizbrand.
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 - Learn the fundamentals and advanced concepts of DevOps practices and tools.
SRE Certification - Gain expertise in Site Reliability Engineering and ensure reliability at scale.
MLOps Certification - Dive into Machine Learning Operations and streamline ML workflows.
AiOps Certification - Discover AI-driven operations management for next-gen IT environments.
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.