{"id":2037,"date":"2024-05-31T08:12:00","date_gmt":"2024-05-31T06:12:00","guid":{"rendered":"https:\/\/robermb.com\/blog\/?p=2037"},"modified":"2024-06-01T18:26:10","modified_gmt":"2024-06-01T16:26:10","slug":"terraform-variable-definition-precedence","status":"publish","type":"post","link":"https:\/\/robermb.com\/blog\/geeks\/terraform-variable-definition-precedence\/","title":{"rendered":"Terraform Variable Definition Precedence"},"content":{"rendered":"\n<p>In Terraform, the order of precedence for variable assignment determines which value is used when multiple sources are available. Here&#8217;s the order of precedence from lowest to highest:<\/p>\n\n\n\n<p>1. <strong>Default Values in Variable Definitions<\/strong>&nbsp;(<code>default<\/code>):<br>If no other value is provided, the default value defined in the variable block is used.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>variable \"region\" {\n  type        = string\n  description = \"The region where resources will be deployed\"\n  default     = \"us-west-1\"\n}<\/code><\/pre>\n\n\n\n<p>2. <strong>Environment Variables<\/strong>&nbsp;(<code>TF_VAR_<\/code>):<br>Environment variables that follow the format&nbsp;<code>TF_VAR_name<\/code>. For example, for the variable&nbsp;<code>region<\/code>, you can set it in your execution environment as&nbsp;<code>TF_VAR_region<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export TF_VAR_region=\"us-east-1\"<\/code><\/pre>\n\n\n\n<p>3. <strong><code>terraform.tfvars<\/code>&nbsp;or&nbsp;<code>*.auto.tfvars<\/code>&nbsp;Files<\/strong>:<br>Terraform automatically looks for files named&nbsp;<code>terraform.tfvars<\/code>&nbsp;or files with the&nbsp;<code>.auto.tfvars<\/code>&nbsp;extension in the current directory and applies the values defined in them.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># terraform.tfvars\nregion = \"us-east-1\"<\/code><\/pre>\n\n\n\n<p>4. <strong>Specific Variable Files<\/strong>:<br>You can specify a custom variable file using the&nbsp;<code>-var-file<\/code>&nbsp;flag on the command line.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform apply -var-file=\"custom.tfvars\"<\/code><\/pre>\n\n\n\n<p>5. <strong>Command-Line Flags<\/strong>&nbsp;(<code>-var<\/code>):<br>You can pass values directly on the command line using the&nbsp;<code>-var<\/code>&nbsp;flag. This method has the highest precedence and will override any other defined values.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform apply -var=\"region=us-east-2\"<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Practical Example<\/h2>\n\n\n\n<p>Suppose you have a Terraform configuration with a variable named&nbsp;<code>region<\/code>&nbsp;defined as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># variables.tf\nvariable \"region\" {\n  type        = string\n  description = \"The region where resources will be deployed\"\n  default     = \"us-west-1\"\n}<\/code><\/pre>\n\n\n\n<p>And you have the following files and configurations:<\/p>\n\n\n\n<p>1. <strong><code>terraform.tfvars<\/code>&nbsp;File<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>region = \"us-east-1\"<\/code><\/pre>\n\n\n\n<p>2. <strong>Custom Variable File&nbsp;<code>custom.tfvars<\/code><\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>region = \"us-west-2\"<\/code><\/pre>\n\n\n\n<p>3. <strong>Environment Variable<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export TF_VAR_region=\"eu-central-1\"<\/code><\/pre>\n\n\n\n<p>4. <strong>Command-Line Flag<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform apply -var=\"region=us-east-2\"<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Precedence Order in Action<\/h2>\n\n\n\n<p>1. If you run&nbsp;<code><strong>terraform apply<\/strong><\/code>&nbsp;without any other options:<br>The value from the&nbsp;<code>terraform.tfvars<\/code>&nbsp;file (<code><strong>us-east-1<\/strong><\/code>) will be used.<\/p>\n\n\n\n<p>2. If you run&nbsp;<code><strong>terraform apply -var-file=\"custom.tfvars\"<\/strong><\/code>:<br>The value from the&nbsp;<code>custom.tfvars<\/code>&nbsp;file (<code><strong>us-west-2<\/strong><\/code>) will be used.<\/p>\n\n\n\n<p>3. If you have the <strong>environment<\/strong> <strong>variable<\/strong>&nbsp;<code><strong>TF_VAR_region<\/strong><\/code>&nbsp;set:<br>The value from the environment variable (<code><strong>eu-central-1<\/strong><\/code>) will be used.<\/p>\n\n\n\n<p>4. If you use the <strong>command-line flag&nbsp;<code>-var<\/code><\/strong>:<br>The value passed on the command line (<code><strong>us-east-2<\/strong><\/code>) will be used, overriding any other value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>The order of precedence for variable assignment in Terraform is crucial to understanding which value will be used when multiple sources are available. <br><br>The <strong>precedence<\/strong> <strong>order<\/strong> <strong>from<\/strong> <strong>lowest<\/strong> <strong>to highest<\/strong> is:<\/p>\n\n\n\n<ol>\n<li>Default values in variable definitions.<\/li>\n\n\n\n<li>Environment variables (<code>TF_VAR_<\/code>).<\/li>\n\n\n\n<li><code>terraform.tfvars<\/code>&nbsp;or&nbsp;<code>*.auto.tfvars<\/code>&nbsp;files.<\/li>\n\n\n\n<li>Specific variable files (<code>-var-file<\/code>).<\/li>\n\n\n\n<li>Command-line flags (<code>-var<\/code>).<\/li>\n<\/ol>\n\n\n\n<p>This order allows you to control and override variable values flexibly according to your needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Terraform, the order of precedence for variable assignment determines which value is used when multiple sources are available. Here&#8217;s &hellip; <a href=\"https:\/\/robermb.com\/blog\/geeks\/terraform-variable-definition-precedence\/\" class=\"more-link\">More <span class=\"screen-reader-text\">Terraform Variable Definition Precedence<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2031,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[103,2],"tags":[112,126,131],"_links":{"self":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/2037"}],"collection":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/comments?post=2037"}],"version-history":[{"count":2,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/2037\/revisions"}],"predecessor-version":[{"id":2039,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/2037\/revisions\/2039"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/media\/2031"}],"wp:attachment":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/media?parent=2037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/categories?post=2037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/tags?post=2037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}