{"id":8097,"date":"2019-12-21T07:23:10","date_gmt":"2019-12-21T07:23:10","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=8097"},"modified":"2026-11-21T19:01:48","modified_gmt":"2026-11-21T19:01:48","slug":"terraform-variable-using-anyotherfile-tfvars-example","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/terraform-variable-using-anyotherfile-tfvars-example\/","title":{"rendered":"Terraform variable using anyotherfile.tfvars example"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This example shows how to keep Terraform variable values in a custom file such as <code>anyotherfile.tfvars<\/code> and load it with <code>terraform apply -var-file=\"anyotherfile.tfvars\"<\/code>. Unlike <code>terraform.tfvars<\/code>, a file with any other name is <strong>not<\/strong> loaded automatically, so you must pass it on the command line every time you run <code>plan<\/code>, <code>apply<\/code> or <code>destroy<\/code>.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/d455fae34d3253dada427787aa20dee6.js\"><\/script>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_91706\"><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_36460\"  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_91706\"  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>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">The example code in plain text<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The embedded gist above contains two files. Here they are as plain text so you can copy them:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">anyotherfile.tfvars<\/h3>\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\">reponame = <span class=\"hljs-string\">\"terraform-var-anyotherfile-tfvars\"<\/span><\/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<h3 class=\"wp-block-heading\">main.tf (as written in the original example)<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">provider <span class=\"hljs-string\">\"github\"<\/span> {\n  token        = <span class=\"hljs-string\">\"\"<\/span>\n  organization = <span class=\"hljs-string\">\"devopsschool-sample-projects\"<\/span>\n}\n\nvariable reponame {}\n\nresource <span class=\"hljs-string\">\"github_repository\"<\/span> <span class=\"hljs-string\">\"example\"<\/span> {\n  name        = <span class=\"hljs-string\">\"${var.reponame}\"<\/span>\n  description = <span class=\"hljs-string\">\"My awesome codebase\"<\/span>\n  <span class=\"hljs-keyword\">private<\/span>     = <span class=\"hljs-keyword\">false<\/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\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">How the example works<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>variable reponame {}<\/code> declares an input variable with no default value, so Terraform must get a value for it from somewhere before it can plan.<\/li>\n\n\n<li><code>anyotherfile.tfvars<\/code> assigns that value. A <code>.tfvars<\/code> file contains only variable assignments, never resources.<\/li>\n\n\n<li>The <code>github_repository<\/code> resource reads the value through <code>var.reponame<\/code> and creates a public repository with that name.<\/li>\n\n\n<li><code>-var-file=\"anyotherfile.tfvars\"<\/code> tells Terraform to read the file. Without it, Terraform stops with &#8220;No value for required variable&#8221; (or asks for the value interactively).<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Run it step by step<\/h2>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># 1. Give the GitHub provider a token (never hard-code it in .tf files)<\/span>\nexport GITHUB_TOKEN=<span class=\"hljs-string\">\"&lt;your-personal-access-token&gt;\"<\/span>\n\n<span class=\"hljs-comment\"># 2. Download the provider<\/span>\nterraform init\n\n<span class=\"hljs-comment\"># 3. Preview the change using the custom variable file<\/span>\nterraform plan -<span class=\"hljs-keyword\">var<\/span>-file=<span class=\"hljs-string\">\"anyotherfile.tfvars\"<\/span>\n\n<span class=\"hljs-comment\"># 4. Create the repository<\/span>\nterraform apply -<span class=\"hljs-keyword\">var<\/span>-file=<span class=\"hljs-string\">\"anyotherfile.tfvars\"<\/span>\n\n<span class=\"hljs-comment\"># 5. Clean up when you are done (pass the same file again)<\/span>\nterraform destroy -<span class=\"hljs-keyword\">var<\/span>-file=<span class=\"hljs-string\">\"anyotherfile.tfvars\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">The token needs permission to create repositories in the organization (for a classic token, the <code>repo<\/code> scope).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Updated syntax for Terraform 0.13 and later<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The original example uses Terraform 0.11 style. It still explains the idea, but on current Terraform versions write it like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">terraform {\n  required_providers {\n    github = {\n      source = <span class=\"hljs-string\">\"integrations\/github\"<\/span>\n    }\n  }\n}\n\nprovider <span class=\"hljs-string\">\"github\"<\/span> {\n  owner = <span class=\"hljs-string\">\"devopsschool-sample-projects\"<\/span>\n  <span class=\"hljs-comment\"># The token is read from the GITHUB_TOKEN environment variable<\/span>\n}\n\nvariable <span class=\"hljs-string\">\"reponame\"<\/span> {\n  description = <span class=\"hljs-string\">\"Name of the GitHub repository to create\"<\/span>\n  type        = string\n}\n\nresource <span class=\"hljs-string\">\"github_repository\"<\/span> <span class=\"hljs-string\">\"example\"<\/span> {\n  name        = <span class=\"hljs-keyword\">var<\/span>.reponame\n  description = <span class=\"hljs-string\">\"My awesome codebase\"<\/span>\n  visibility  = <span class=\"hljs-string\">\"public\"<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Variable names are quoted and should have a <code>type<\/code> and <code>description<\/code>.<\/li>\n\n\n<li>Use <code>var.reponame<\/code> directly; the <code>\"${...}\"<\/code> wrapper is only needed inside longer strings.<\/li>\n\n\n<li>The GitHub provider now lives at <code>integrations\/github<\/code>; <code>owner<\/code> replaces <code>organization<\/code>, and <code>visibility<\/code> replaces the deprecated <code>private<\/code> argument.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Where Terraform looks for variable values<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When the same variable is set in several places, Terraform uses this order. Later sources override earlier ones:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Environment variables named <code>TF_VAR_&lt;name&gt;<\/code>, for example <code>TF_VAR_reponame<\/code><\/li>\n\n\n<li>The <code>terraform.tfvars<\/code> file, if present<\/li>\n\n\n<li>The <code>terraform.tfvars.json<\/code> file, if present<\/li>\n\n\n<li>Any <code>*.auto.tfvars<\/code> or <code>*.auto.tfvars.json<\/code> files, in alphabetical order<\/li>\n\n\n<li><code>-var<\/code> and <code>-var-file<\/code> options on the command line, in the order you type them<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>default<\/code> in the variable block is used only when none of these provide a value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">One variable file per environment<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The most common real-world use of <code>-var-file<\/code> is keeping one file per environment while the code stays the same:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># dev.tfvars<\/span>\nreponame = <span class=\"hljs-string\">\"myapp-dev\"<\/span>\n\n<span class=\"hljs-comment\"># prod.tfvars<\/span>\nreponame = <span class=\"hljs-string\">\"myapp-prod\"<\/span>\n\nterraform plan -<span class=\"hljs-keyword\">var<\/span>-file=<span class=\"hljs-string\">\"dev.tfvars\"<\/span>\nterraform plan -<span class=\"hljs-keyword\">var<\/span>-file=<span class=\"hljs-string\">\"prod.tfvars\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Use a separate state for each environment (separate backends, directories or workspaces), otherwise applying <code>prod.tfvars<\/code> would modify the resources created with <code>dev.tfvars<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common errors and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Error<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td>No value for required variable<\/td><td>The <code>.tfvars<\/code> file was not passed, or the variable name in the file is misspelled<\/td><td>Add <code>-var-file=...<\/code> and check the name matches the <code>variable<\/code> block<\/td><\/tr><tr><td>Given variables file &#8230; does not exist<\/td><td>The path is relative to the directory where you run Terraform<\/td><td>Run from the right folder or give the full path<\/td><\/tr><tr><td>Value for undeclared variable<\/td><td>The file sets a variable that no <code>variable<\/code> block declares<\/td><td>Declare the variable or remove the line from the file<\/td><\/tr><tr><td>401 Bad credentials<\/td><td>Missing, expired or under-privileged GitHub token<\/td><td>Export a valid <code>GITHUB_TOKEN<\/code> with repository permissions<\/td><\/tr><tr><td>Repository creation failed: name already exists<\/td><td>A repository with that name already exists<\/td><td>Change <code>reponame<\/code> or import the existing repository<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Best practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Never put secrets such as tokens or passwords in <code>.tfvars<\/code> files that are committed to Git. Use environment variables or a secret manager, and mark sensitive variables with <code>sensitive = true<\/code>.<\/li>\n\n\n<li>Add <code>*.tfvars<\/code> files that contain secrets to <code>.gitignore<\/code>; keep non-secret environment files in version control so runs are reproducible.<\/li>\n\n\n<li>Give every variable a <code>type<\/code>, a <code>description<\/code> and, where useful, a <code>validation<\/code> block.<\/li>\n\n\n<li>Pin the provider version you have tested and commit <code>.terraform.lock.hcl<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Is anyotherfile.tfvars loaded automatically?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. Terraform only loads <code>terraform.tfvars<\/code>, <code>terraform.tfvars.json<\/code> and files ending in <code>.auto.tfvars<\/code> or <code>.auto.tfvars.json<\/code> automatically. Any other name must be passed with <code>-var-file<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I pass more than one -var-file?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Repeat the option, for example <code>-var-file=common.tfvars -var-file=prod.tfvars<\/code>. If both set the same variable, the file given last wins.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between -var and -var-file?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>-var<\/code> sets one value directly, such as <code>-var=\"reponame=demo\"<\/code>. <code>-var-file<\/code> loads many values from a file. Both have the same priority; whichever appears later on the command line wins.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can a .tfvars file use expressions or other variables?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. Values in <code>.tfvars<\/code> files must be literal values: strings, numbers, booleans, lists and maps. Put calculations in <code>locals<\/code> inside your configuration instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/terraform-variables-and-configuration-explained-5-mins-reading\/\">Terraform Variables and Configuration explained<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/terraform-environment-variables-exaplained\/\">Terraform Environment Variables Explained<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/hcp-terraform-terraform-cloud-complete-guide-to-variables-scope-precedence\/\">HCP Terraform: Variables, Scope &amp; Precedence<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/infrastructure-as-code\/terraform\/\">All Terraform tutorials<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This example shows how to keep Terraform variable values in a custom file such as anyotherfile.tfvars and load it with terraform apply -var-file=&#8221;anyotherfile.tfvars&#8221;. Unlike terraform.tfvars, a file&#8230; <\/p>\n","protected":false},"author":1,"featured_media":8191,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5129],"tags":[26224],"class_list":["post-8097","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-terraform","tag-hcl"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/8097","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=8097"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/8097\/revisions"}],"predecessor-version":[{"id":79986,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/8097\/revisions\/79986"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/8191"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=8097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=8097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=8097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}