{"id":48785,"date":"2025-03-18T07:52:33","date_gmt":"2025-03-18T07:52:33","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=48785"},"modified":"2025-03-18T07:58:04","modified_gmt":"2025-03-18T07:58:04","slug":"terraform-tutorias-cli-environment-and-predefined-meta-variables-complete-reference","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/terraform-tutorias-cli-environment-and-predefined-meta-variables-complete-reference\/","title":{"rendered":"Terraform Tutorias: CLI environment and predefined\/meta variables Complete Reference"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Terraform CLI environment variables reference<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Environment Variable<\/strong><\/td><td><strong>Description<\/strong><\/td><td><strong>Example Usage<\/strong><\/td><\/tr><tr><td><strong>TF_LOG<\/strong><\/td><td>Controls logging verbosity. Values: TRACE, DEBUG, INFO, WARN, ERROR, OFF.<\/td><td>export TF_LOG=DEBUG<\/td><\/tr><tr><td><strong>TF_LOG_PATH<\/strong><\/td><td>Specifies log file path to store logs.<\/td><td>export TF_LOG_PATH=.\/terraform.log<\/td><\/tr><tr><td><strong>TF_INPUT<\/strong><\/td><td>Determines if Terraform prompts for user input. Set to false to disable.<\/td><td>export TF_INPUT=false<\/td><\/tr><tr><td><strong>TF_VAR_name<\/strong><\/td><td>Sets Terraform input variables from the environment. Example: TF_VAR_region.<\/td><td>export TF_VAR_region=us-west-1<\/td><\/tr><tr><td><strong>TF_CLI_ARGS and TF_CLI_ARGS_name<\/strong><\/td><td>Defines default CLI arguments for Terraform commands. Example: TF_CLI_ARGS_plan.<\/td><td>export TF_CLI_ARGS=&#8217;-input=false&#8217;<\/td><\/tr><tr><td><strong>TF_DATA_DIR<\/strong><\/td><td>Changes Terraform data directory from the default `.terraform`.<\/td><td>export TF_DATA_DIR=\/custom\/data\/dir<\/td><\/tr><tr><td><strong>TF_WORKSPACE<\/strong><\/td><td>Specifies the workspace to use in Terraform.<\/td><td>export TF_WORKSPACE=production<\/td><\/tr><tr><td><strong>TF_IN_AUTOMATION<\/strong><\/td><td>Indicates if Terraform is running in an automated environment (CI\/CD).<\/td><td>export TF_IN_AUTOMATION=true<\/td><\/tr><tr><td><strong>TF_REGISTRY_DISCOVERY_RETRY<\/strong><\/td><td>Number of retries for Terraform Registry discovery.<\/td><td>export TF_REGISTRY_DISCOVERY_RETRY=5<\/td><\/tr><tr><td><strong>TF_REGISTRY_CLIENT_TIMEOUT<\/strong><\/td><td>Sets timeout duration for Terraform Registry client operations.<\/td><td>export TF_REGISTRY_CLIENT_TIMEOUT=30<\/td><\/tr><tr><td><strong>TF_STATE_PERSIST_INTERVAL<\/strong><\/td><td>Defines how frequently Terraform writes its state file.<\/td><td>export TF_STATE_PERSIST_INTERVAL=10<\/td><\/tr><tr><td><strong>TF_CLI_CONFIG_FILE<\/strong><\/td><td>Specifies custom path for Terraform CLI configuration file.<\/td><td>export TF_CLI_CONFIG_FILE=$HOME\/.terraformrc<\/td><\/tr><tr><td><strong>TF_PLUGIN_CACHE_DIR<\/strong><\/td><td>Specifies the directory for Terraform plugin cache.<\/td><td>export TF_PLUGIN_CACHE_DIR=$HOME\/.terraform.d\/plugin-cache<\/td><\/tr><tr><td><strong>TF_IGNORE<\/strong><\/td><td>Defines patterns for files and directories that Terraform should ignore.<\/td><td>export TF_IGNORE=&#8217;*.bak&#8217;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Terraform predefined and meta variables reference<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Built-in Variable<\/th><th>Description<\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>terraform.workspace<\/code><\/td><td>Returns the <strong>current workspace name<\/strong>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>path.module<\/code><\/td><td>Returns the <strong>current module path<\/strong>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>path.root<\/code><\/td><td>Returns the <strong>root module path<\/strong>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>path.cwd<\/code><\/td><td>Returns the <strong>current working directory<\/strong>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>count.index<\/code><\/td><td>Current <strong>index<\/strong> in a <code>count<\/code> loop.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>each.key<\/code>, <code>each.value<\/code><\/td><td>Current <strong>key-value<\/strong> in a <code>for_each<\/code> loop.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>var.&lt;variable_name&gt;<\/code><\/td><td>References an <strong>input variable<\/strong>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>local.&lt;local_variable&gt;<\/code><\/td><td>References a <strong>local variable<\/strong>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>module.&lt;module_name&gt;.&lt;output&gt;<\/code><\/td><td>Retrieves an <strong>output from a child module<\/strong>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>self<\/code><\/td><td>Refers to <strong>current resource instance<\/strong>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>depends_on<\/code><\/td><td>Explicitly <strong>sets dependencies<\/strong> between resources.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>env.&lt;ENV_VAR&gt;<\/code><\/td><td>Retrieves <strong>environment variables<\/strong> from the shell.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Comprehensive Guide: Terraform CLI Environment Variables &amp; Predefined Variables<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1. Terraform CLI Environment Variables Reference<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.1 Logging &amp; Debugging<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>TF_LOG<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Description:<\/strong> Controls Terraform logging verbosity.<\/li>\n\n\n\n<li><strong>Possible Values:<\/strong> <code>TRACE<\/code>, <code>DEBUG<\/code>, <code>INFO<\/code>, <code>WARN<\/code>, <code>ERROR<\/code>, <code>OFF<\/code><\/li>\n\n\n\n<li><strong>Use Case:<\/strong> Useful for debugging Terraform execution.<\/li>\n\n\n\n<li><strong>Example:<\/strong> <code>export TF_LOG=DEBUG terraform apply<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>TF_LOG_PATH<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Description:<\/strong> Specifies a log file path to store logs.<\/li>\n\n\n\n<li><strong>Use Case:<\/strong> Useful when debugging Terraform automation pipelines.<\/li>\n\n\n\n<li><strong>Example:<\/strong> <code>export TF_LOG_PATH=.\/terraform.log terraform apply<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.2 Input Handling<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>TF_INPUT<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Description:<\/strong> Determines if Terraform prompts for user input. Set to <code>false<\/code> to disable interactive prompts.<\/li>\n\n\n\n<li><strong>Use Case:<\/strong> Useful in CI\/CD environments.<\/li>\n\n\n\n<li><strong>Example:<\/strong> <code>export TF_INPUT=false terraform apply<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.3 Variable Handling<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>TF_VAR_name<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Description:<\/strong> Sets Terraform input variables from the environment.<\/li>\n\n\n\n<li><strong>Use Case:<\/strong> Allows dynamic configuration of Terraform variables without modifying the <code>.tf<\/code> file.<\/li>\n\n\n\n<li><strong>Example:<\/strong> <code>export TF_VAR_region=us-west-1 terraform apply<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>TF_CLI_ARGS and TF_CLI_ARGS_name<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Description:<\/strong> Defines default CLI arguments for Terraform commands.<\/li>\n\n\n\n<li><strong>Use Case:<\/strong> Ensures that Terraform always runs with specific flags.<\/li>\n\n\n\n<li><strong>Example:<\/strong> <code>export TF_CLI_ARGS='-input=false' terraform apply<\/code> <code>export TF_CLI_ARGS_plan='-refresh=false' terraform plan<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.4 Terraform State &amp; Data Management<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>TF_DATA_DIR<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Description:<\/strong> Changes Terraform data directory from the default <code>.terraform<\/code>.<\/li>\n\n\n\n<li><strong>Use Case:<\/strong> Useful when running multiple Terraform executions in parallel.<\/li>\n\n\n\n<li><strong>Example:<\/strong> <code>export TF_DATA_DIR=\/custom\/data\/dir terraform init<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>TF_WORKSPACE<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Description:<\/strong> Specifies the workspace to use in Terraform.<\/li>\n\n\n\n<li><strong>Use Case:<\/strong> Automate Terraform workspace switching.<\/li>\n\n\n\n<li><strong>Example:<\/strong> <code>export TF_WORKSPACE=production terraform apply<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>TF_IN_AUTOMATION<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Description:<\/strong> Indicates if Terraform is running in an automated environment (CI\/CD).<\/li>\n\n\n\n<li><strong>Use Case:<\/strong> Helps Terraform suppress interactive prompts.<\/li>\n\n\n\n<li><strong>Example:<\/strong> <code>export TF_IN_AUTOMATION=true terraform apply<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.5 Terraform Registry &amp; Plugin Management<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>TF_REGISTRY_DISCOVERY_RETRY<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Description:<\/strong> Number of retries for Terraform Registry discovery.<\/li>\n\n\n\n<li><strong>Use Case:<\/strong> Useful when working in unstable network environments.<\/li>\n\n\n\n<li><strong>Example:<\/strong> <code>export TF_REGISTRY_DISCOVERY_RETRY=5<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>TF_REGISTRY_CLIENT_TIMEOUT<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Description:<\/strong> Sets timeout duration for Terraform Registry client operations.<\/li>\n\n\n\n<li><strong>Use Case:<\/strong> Useful in slow networks.<\/li>\n\n\n\n<li><strong>Example:<\/strong> <code>export TF_REGISTRY_CLIENT_TIMEOUT=30<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>TF_PLUGIN_CACHE_DIR<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Description:<\/strong> Specifies the directory for Terraform plugin cache.<\/li>\n\n\n\n<li><strong>Use Case:<\/strong> Helps speed up Terraform runs by avoiding plugin downloads.<\/li>\n\n\n\n<li><strong>Example:<\/strong> <code>export TF_PLUGIN_CACHE_DIR=$HOME\/.terraform.d\/plugin-cache<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.6 Miscellaneous Terraform Configurations<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>TF_STATE_PERSIST_INTERVAL<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Description:<\/strong> Defines how frequently Terraform writes its state file.<\/li>\n\n\n\n<li><strong>Example:<\/strong> <code>export TF_STATE_PERSIST_INTERVAL=10<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>TF_CLI_CONFIG_FILE<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Description:<\/strong> Specifies a custom path for the Terraform CLI configuration file.<\/li>\n\n\n\n<li><strong>Example:<\/strong> <code>export TF_CLI_CONFIG_FILE=$HOME\/.terraformrc<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>TF_IGNORE<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Description:<\/strong> Defines patterns for files and directories that Terraform should ignore.<\/li>\n\n\n\n<li><strong>Example:<\/strong> <code>export TF_IGNORE='*.bak'<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. Terraform Predefined and Meta Variables Reference<\/strong><\/h2>\n\n\n\n<p>Terraform provides a set of <strong>predefined variables<\/strong> that allow access to system metadata.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Built-in Variable<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>terraform.workspace<\/code><\/td><td>Returns the current workspace name.<\/td><\/tr><tr><td><code>path.module<\/code><\/td><td>Returns the current module path.<\/td><\/tr><tr><td><code>path.root<\/code><\/td><td>Returns the root module path.<\/td><\/tr><tr><td><code>path.cwd<\/code><\/td><td>Returns the current working directory.<\/td><\/tr><tr><td><code>count.index<\/code><\/td><td>Current index in a <code>count<\/code> loop.<\/td><\/tr><tr><td><code>each.key<\/code>, <code>each.value<\/code><\/td><td>Current key-value in a <code>for_each<\/code> loop.<\/td><\/tr><tr><td><code>var.&lt;variable_name&gt;<\/code><\/td><td>References an input variable.<\/td><\/tr><tr><td><code>local.&lt;local_variable&gt;<\/code><\/td><td>References a local variable.<\/td><\/tr><tr><td><code>module.&lt;module_name&gt;.&lt;output&gt;<\/code><\/td><td>Retrieves an output from a child module.<\/td><\/tr><tr><td><code>self<\/code><\/td><td>Refers to the current resource instance.<\/td><\/tr><tr><td><code>depends_on<\/code><\/td><td>Explicitly sets dependencies between resources.<\/td><\/tr><tr><td><code>env.&lt;ENV_VAR&gt;<\/code><\/td><td>Retrieves environment variables from the shell.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Example Usage:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">output <span class=\"hljs-string\">\"workspace\"<\/span> {\n  value = terraform.workspace\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">output <span class=\"hljs-string\">\"current_path\"<\/span> {\n  value = path.module\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By understanding and utilizing these <strong>Terraform environment variables and predefined expressions<\/strong>, users can optimize their infrastructure as code (IaC) workflows efficiently.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/rajeshkumarin\/ff85176ac4f47ead9b1f45be03b5295e.js\"><\/script>\n","protected":false},"excerpt":{"rendered":"<p>Terraform CLI environment variables reference Environment Variable Description Example Usage TF_LOG Controls logging verbosity. Values: TRACE, DEBUG, INFO, WARN, ERROR, OFF. export TF_LOG=DEBUG TF_LOG_PATH Specifies log file path to store&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-48785","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/48785","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=48785"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/48785\/revisions"}],"predecessor-version":[{"id":48788,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/48785\/revisions\/48788"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=48785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=48785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=48785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}