instruction: How do you manage multiple environments with Terraform?
output: Terraform CLI environment variables reference | Terraform | HashiCorp Developer HashiConf The full conference agenda&#x27;s now LIVE. Buy your pass and plan your schedule. Register Dismiss alert HashiCorp Cloud Platform Get started in minutes with our cloud products All HCP Products Infrastructure Lifecycle Management Terraform Manage infrastructure as code Packer Build machine images Nomad Orchestrate workloads Waypoint Standardize application patterns Vagrant Build developer environments Security Lifecycle Management Vault Centrally manage secrets Boundary Secure remote access HCP Vault Radar Scan for embedded secrets Consul Secure network services Learn Certifications Get HashiCorp certified Tutorials Learn HashiCorp products Validated Patterns Field-tested patterns for using HashiCorp products Well-Architected Framework Adopt HashiCorp best practices Terraform Install Tutorials Documentation Documentation Intro to Terraform Configuration Language Terraform CLI HCP Terraform Terraform Enterprise Terraform Migrate CDK for Terraform Provider Use Plugin Development Registry Publishing Integration Program Registry (opens in new tab) Try Cloud (opens in new tab) Search Command or control key K key Sign in Sign up Theme Terraform Home Terraform CLI Terraform CLI Basic CLI Features Initializing Working Directories Provisioning Infrastructure Authenticating Writing and Modifying Code Inspecting Infrastructure Import Infrastructure Manually Update State Managing Workspaces Managing Plugins CLI Configuration Overview CLI Configuration Environment Variables Using HCP Terraform Testing Terraform Automating Terraform Alphabetical List of Commands Terraform Internals Resources Tutorial Library Certifications Community Forum (opens in new tab) Support (opens in new tab) GitHub (opens in new tab) Terraform Registry (opens in new tab) Developer Terraform Terraform CLI CLI Configuration Environment Variables v1.12.x (latest) Terraform v1.13.x (rc) v1.11.x v1.10.x v1.9.x v1.8.x v1.7.x v1.6.x v1.5.x v1.4.x v1.3.x v1.2.x v1.1.x Terraform CLI environment variables reference This topic contains reference information about the environment variables you can use with the Terraform CLI. Introduction Terraform refers to a number of environment variables to customize various aspects of its behavior. None of these environment variables are required when using Terraform, but you can use them to change some of Terraform&#x27;s default behaviors or to increase output verbosity for debugging. TF_LOG Enables detailed logs to appear on stderr which is useful for debugging. For example: export TF_LOG = trace To disable, either unset it, or set it to off . For example: export TF_LOG = off For more on debugging Terraform, check out the section on Debugging . TF_LOG_PATH This specifies where the log should persist its output to. Note that even when TF_LOG_PATH is set, TF_LOG must be set in order for any logging to be enabled. For example, to always write the log to the directory you&#x27;re currently running terraform from: export TF_LOG_PATH = ./terraform.log For more on debugging Terraform, check out the section on Debugging . TF_INPUT If set to &quot;false&quot; or &quot;0&quot;, causes terraform commands to behave as if the -input=false flag was specified. This is used when you want to disable prompts for variables that haven&#x27;t had their values specified. For example: export TF_INPUT = 0 TF_VAR_name Environment variables can be used to set variables. The environment variables must be in the format TF_VAR_name and this will be checked last for a value. For example: export TF_VAR_region = us-west-1 export TF_VAR_ami = ami-049d8641 export TF_VAR_alist = &#x27;[1,2,3]&#x27; export TF_VAR_amap = &#x27;{ foo = &quot;bar&quot;, baz = &quot;qux&quot; }&#x27; For more on how to use TF_VAR_name in context, check out the section on Variable Configuration . TF_CLI_ARGS and TF_CLI_ARGS_name The value of TF_CLI_ARGS will specify additional arguments to the command-line. This allows easier automation in CI environments as well as modifying default behavior of Terraform on your own system. These arguments are inserted directly after the subcommand (such as plan ) and before any flags specified directly on the command-line. This behavior ensures that flags on the command-line take precedence over environment variables. For example, the following command: TF_CLI_ARGS=&quot;-input=false&quot; terraform apply -force is the equivalent to manually typing: terraform apply -input=false -force . The flag TF_CLI_ARGS affects all Terraform commands. If you specify a named command in the form of TF_CLI_ARGS_name then it will only affect that command. As an example, to specify that only plans never refresh, you can set TF_CLI_ARGS_plan=&quot;-refresh=false&quot; . The value of the flag is parsed as if you typed it directly to the shell. Double and single quotes are allowed to capture strings and arguments will be separated by spaces otherwise. TF_DATA_DIR TF_DATA_DIR changes the location where Terraform keeps its per-working-directory data, such as the current backend configuration. By default this data is written into a .terraform subdirectory of the current directory, but the path given in TF_DATA_DIR will be used instead if non-empty. In most cases it should not be necessary to set this variable, but it may be useful to do so if e.g. the working directory is not writable. The data directory is used to retain data that must persist from one command to the next, so it&#x27;s important to have this variable set consistently throughout all of the Terraform workflow commands (starting with terraform init ) or else Terraform may be unable to find providers, modules, and other artifacts. TF_WORKSPACE For multi-environment deployment, in order to select a workspace, instead of doing terraform workspace select your_workspace , it is possible to use this environment variable. Using TF_WORKSPACE allow and override workspace selection. For example: export TF_WORKSPACE = your_workspace Using this environment variable is recommended only for non-interactive usage, since in a local shell environment it can be easy to forget the variable is set and apply changes to the wrong state. For more information regarding workspaces, check out the section on Using Workspaces . TF_IN_AUTOMATION If TF_IN_AUTOMATION is set to any non-empty value, Terraform adjusts its output to avoid suggesting specific commands to run next. This can make the output more consistent and less confusing in workflows where users don&#x27;t directly execute Terraform commands, like in CI systems or other wrapping applications. This is a purely cosmetic change to Terraform&#x27;s human-readable output, and the exact output differences can change between minor Terraform versions. For more details, see Running Terraform in Automation . TF_REGISTRY_DISCOVERY_RETRY Set TF_REGISTRY_DISCOVERY_RETRY to configure the max number of request retries the remote registry client will attempt for client connection errors or 500-range responses that are safe to retry. TF_REGISTRY_CLIENT_TIMEOUT The default client timeout for requests to the remote registry is 10s. TF_REGISTRY_CLIENT_TIMEOUT can be configured and increased during exceptional circumstances. export TF_REGISTRY_CLIENT_TIMEOUT = 15 TF_STATE_PERSIST_INTERVAL The interval in seconds that Terraform attempts to persist state to a remote backend during an apply operation. The default minimum interval for all remote backends is 20 seconds. Backends may override the default minimum value. If the value of TF_STATE_PERSIST_INTERVAL is lower than the default interval specified by a remote backend, the default interval will be used. export TF_STATE_PERSIST_INTERVAL = 100 TF_CLI_CONFIG_FILE The location of the Terraform CLI configuration file . export TF_CLI_CONFIG_FILE = &quot;$HOME/.terraformrc-custom&quot; Note that TERRAFORM_CONFIG is a deprecated alias for the TF_CLI_CONFIG_FILE variable. We recommend using TF_CLI_CONFIG_FILE instead of the deprecated TERRAFORM_CONFIG variable. TF_PLUGIN_CACHE_DIR The TF_PLUGIN_CACHE_DIR environment variable is an alternative way to set the plugin_cache_dir setting in the CLI configuration . You can also use TF_PLUGIN_CACHE_MAY_BREAK_DEPENDENCY_LOCK_FILE to activate the transitional compatibility setting plugin_cache_may_break_dependency_lock_file . HCP Terraform CLI Integration The CLI integration with HCP Terraform lets you use HCP Terraform and Terraform Enterprise on the command line. The integration requires including a cloud block in your Terraform configuration. You can define its arguments directly in your configuration file or supply them through environment variables, which can be useful for non-interactive workflows like Continuous Integration (CI). Refer to HCP Terraform Settings for a full list of cloud block environment variables. Edit this page on GitHub On this page: Terraform CLI environment variables reference Introduction TF_LOG TF_LOG_PATH TF_INPUT TF_VAR_name TF_CLI_ARGS and TF_CLI_ARGS_name TF_DATA_DIR TF_WORKSPACE TF_IN_AUTOMATION TF_REGISTRY_DISCOVERY_RETRY TF_REGISTRY_CLIENT_TIMEOUT TF_STATE_PERSIST_INTERVAL TF_CLI_CONFIG_FILE TF_PLUGIN_CACHE_DIR HCP Terraform CLI Integration Theme Certifications System Status Cookie Manager Terms of Use Security Privacy Trademark Policy Trade Controls Accessibility Give Feedback (opens in new tab)