{"id":7560,"date":"2019-10-31T21:10:41","date_gmt":"2019-10-31T21:10:41","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=7560"},"modified":"2021-11-15T11:20:46","modified_gmt":"2021-11-15T11:20:46","slug":"terraform-environment-variables-exaplained","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/terraform-environment-variables-exaplained\/","title":{"rendered":"Terraform Environment Variables Exaplained!"},"content":{"rendered":"\n<p>Terraform has many environment variables which can be used to customize various aspects of its behavior of terraform. However all are optional, i.e None of these environment variables are required. But these can be used to change some of Terraform&#8217;s default behaviors of Terraform, or to increase output verbosity for debugging.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">TF_LOG<\/h4>\n\n\n\n<p class=\"has-text-align-left\">Terraform has detailed logs which can be enabled by setting the TF_LOG environment variable to any value. This will cause detailed logs to appear on stderr. If set to any value, enables detailed logs to appear on stderr which is useful for debugging. TRACE is the most verbose and it is the default. For example<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>export TF_LOG=TRACE<\/li><li>export TF_LOG=DEBUG<\/li><li>export TF_LOG=INFO<\/li><li>export TF_LOG=WARN<\/li><li>export TF_LOG=ERROR<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">TF_LOG_PATH<\/h4>\n\n\n\n<p>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&#8217;re currently running terraform from:<br> export TF_LOG_PATH=.\/terraform.log<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">TF_INPUT<\/h4>\n\n\n\n<p>If set to &#8220;false&#8221; or &#8220;0&#8221;, 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&#8217;t had their values specified. For example:<br> export TF_INPUT=0<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">TF_VAR_name<\/h4>\n\n\n\n<p>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:<br> export TF_VAR_region=us-west-1<br> export TF_VAR_ami=ami-049d8641<br> export TF_VAR_alist='[1,2,3]&#8217;<br> export TF_VAR_amap='{ foo = &#8220;bar&#8221;, baz = &#8220;qux&#8221; }&#8217;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">TF_CLI_ARGS and TF_CLI_ARGS_name<\/h4>\n\n\n\n<p>The value of TF_CLI_ARGS will specify additional arguments to the command-line. 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. The flag TF_CLI_ARGS affects all Terraform commands.<\/p>\n\n\n\n<p>For example, the following command: TF_CLI_ARGS=&#8221;-input=false&#8221; terraform apply -force is the equivalent to manually typing: terraform apply -input=false -force.<\/p>\n\n\n\n<p>export TF_CLI_ARGS=&#8221;-var-file=..\/..\/global.tfvars -var-file=..\/..\/dev.tfvars&#8221;<br>\nterraform apply<\/p>\n\n\n\n<p>To get around this issue without changing all commands or to disable colors when an external program runs terraform use<br>\nTF_CLI_ARGS = &#8220;-no-color&#8221;<\/p>\n\n\n\n<p>If you specify a named command in the form of TF_CLI_ARGS_name then it will only affect that command. <br> As an example, to specify that only plans never refresh, you can set TF_CLI_ARGS_plan=&#8221;-refresh=false&#8221;.<\/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\">\n$ <span class=\"hljs-keyword\">export<\/span> TF_CLI_ARGS_init=<span class=\"hljs-string\">'-backend=s3 -backend-config=\"...\"'<\/span>\n$ <span class=\"hljs-keyword\">export<\/span> TF_CLI_ARGS_init=<span class=\"hljs-string\">'-backend-config=\"bucket=((aws_bucket))\" -backend-config=\"key=((aws_bucket_key))\" -backend-config=\"region=((aws_region))\"'<\/span>\n$ TF_CLI_ARGS_init=-backend-config=bucket=example\n$ terraform init<\/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\n<h4 class=\"wp-block-heading\">TF_DATA_DIR<\/h4>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>TF_DATA_DIR changes the location where Terraform keeps its per-working-directory data, such as the current remote backend configuration.<\/p>\n\n\n\n<p>Where can i use this? It may be useful to do so if e.g. the working directory is not writable.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">TF_IN_AUTOMATION<\/h4>\n\n\n\n<p>When you run terraform sub commands, it display many output along with suggesting specific commands to run next. When you run terraform sub commands through CI or automation, you dont need these suggestions thus it is best to avoid to be printed to CI servers logs.  <\/p>\n\n\n\n<p>If TF_IN_AUTOMATION is set to any non-empty value, Terraform adjusts its output to avoid suggesting specific commands to run next. This is a purely cosmetic change to Terraform&#8217;s human-readable output, and the exact output differences can change between minor Terraform versions.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">TF_CLI_CONFIG_FILE<\/h4>\n\n\n\n<p>The CLI configuration file configures per-user settings for CLI behaviors, which apply across all Terraform working directories. These files are CLI Configuration File (.terraformrc or terraform.rc)<\/p>\n\n\n\n<p>The configuration is placed in a single file whose location depends on the host operating system:<br> <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>On Windows &#8211;<ul><li>The file must be named named terraform.rc<\/li><li>Placed in the relevant user&#8217;s %APPDATA% directory<\/li><\/ul><\/li><li>On all other systems &#8211; <ul><li>The file must be named .terraformrc<\/li><li>placed directly in the home directory of the relevant user.<\/li><\/ul><\/li><\/ul>\n\n\n\n<p>The location of the Terraform CLI configuration file can also be specified using the TF_CLI_CONFIG_FILE<br> $ export TF_CLI_CONFIG_FILE=&#8221;$HOME\/.terraformrc-custom&#8221;<\/p>\n\n\n\n<p>CLI configuration file has settings such Terraform&#8217;s CLI needs credentials before it can access Terraform Cloud.<\/p>\n\n\n\n<p>Example of .terraformrc<\/p>\n\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\">\nplugin_cache_dir   = <span class=\"hljs-string\">\"$HOME\/.terraform.d\/plugin-cache\"<\/span>\ndisable_checkpoint = <span class=\"hljs-literal\">true<\/span>\ndisable_checkpoint_signature = <span class=\"hljs-literal\">true<\/span>\nccredentials <span class=\"hljs-string\">\"app.terraform.io\"<\/span> {\n  token = <span class=\"hljs-string\">\"xxxxxx.atlasv1.zzzzzzzzzzzzz\"<\/span>\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><strong>disable_checkpoint<\/strong> \u2014 when set to true, disables upgrade and security bulletin checks that require reaching out to HashiCorp-provided network services.<\/p>\n\n\n\n<p><strong>disable_checkpoint_signature <\/strong>\u2014 when set to true, allows the upgrade and security bulletin checks described above but disables the use of an anonymous id used to de-duplicate warning messages.<\/p>\n\n\n\n<p><strong>plugin_cache_dir <\/strong>\u2014 enables plugin caching and specifies, as a string, the location of the plugin cache directory.<\/p>\n\n\n\n<p><strong>credentials<\/strong> \u2014 provides credentials for use with Terraform Cloud. Terraform uses this when performing remote operations or state access with the remote backend and when accessing Terraform Cloud&#8217;s private module registry.<\/p>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_23654\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_60636\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_23654\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Terraform has many environment variables which can be used to customize various aspects of its behavior of terraform. However all are optional, i.e None of these environment variables are required. But these can be used to change some of Terraform&#8217;s default behaviors of Terraform, or to increase output verbosity for debugging. TF_LOG Terraform has detailed&#8230;<\/p>\n","protected":false},"author":1,"featured_media":7789,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5129],"tags":[5449,4795,5130,317],"class_list":["post-7560","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-terraform","tag-devopsschool","tag-environments","tag-terraform","tag-variables"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7560","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=7560"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7560\/revisions"}],"predecessor-version":[{"id":25333,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7560\/revisions\/25333"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/7789"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=7560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=7560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=7560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}