{"id":12691,"date":"2020-12-11T13:07:24","date_gmt":"2020-12-11T12:07:24","guid":{"rendered":"https:\/\/www.codemotion.com\/magazine\/?p=12691"},"modified":"2022-01-05T20:06:43","modified_gmt":"2022-01-05T19:06:43","slug":"terraform-aws-files-variables","status":"publish","type":"post","link":"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/","title":{"rendered":"Terraform AWS: Files and Variables"},"content":{"rendered":"\t\t\t\t<div class=\"wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-9bd791ad      \"\n\t\t\t\t\tdata-scroll= \"1\"\n\t\t\t\t\tdata-offset= \"30\"\n\t\t\t\t\tstyle=\"\"\n\t\t\t\t>\n\t\t\t\t<div class=\"uagb-toc__wrap\">\n\t\t\t\t\t\t<div class=\"uagb-toc__title\">\n\t\t\t\t\t\t\tTable Of Contents\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"uagb-toc__list-wrap \">\n\t\t\t\t\t\t<ol class=\"uagb-toc__list\"><li class=\"uagb-toc__list\"><a href=\"#prerequisites\" class=\"uagb-toc-link__trigger\">Prerequisites<\/a><li class=\"uagb-toc__list\"><a href=\"#a-dynamic-terraform-aws-project-in-continuous-growth\" class=\"uagb-toc-link__trigger\">A dynamic Terraform AWS project in continuous growth<\/a><li class=\"uagb-toc__list\"><a href=\"#input-variables-in-terraform\" class=\"uagb-toc-link__trigger\">Input Variables in Terraform<\/a><li class=\"uagb-toc__list\"><a href=\"#how-to-give-value-to-our-variables-variable\" class=\"uagb-toc-link__trigger\">How to give value to our variable\u2019s Variable<\/a><li class=\"uagb-toc__list\"><a href=\"#data-resources-how-to-retrieve-information-outside-terraform\" class=\"uagb-toc-link__trigger\">Data resources: how to retrieve information outside Terraform<\/a><li class=\"uagb-toc__list\"><a href=\"#outputs\" class=\"uagb-toc-link__trigger\">Outputs<\/a><\/ol>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\n\n\n<p>In the <a href=\"https:\/\/www.codemotion.com\/magazine\/dev-hub\/cloud-manager\/terraform-aws\/\" class=\"ek-link\">previous article<\/a> of our series about <strong>Terraform <span id=\"urn:enhancement-9819f50f\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/amazon_web_services\">AWS<\/span><\/strong>, we looked at an initial <span id=\"urn:enhancement-c36f0f62\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/project\">project<\/span> that allowed us to create an <span id=\"urn:enhancement-faa95d7c\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/amazon_simple_queue_service\">SQS<\/span>-type <span id=\"urn:enhancement-285199eb\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/resource\">resource<\/span>, understanding how to configure Terraform so that it can communicate with <span id=\"urn:enhancement-e895c725\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/amazon_web_services\">AWS<\/span> thanks to<strong> programmatic access <span id=\"urn:enhancement-87942e08\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">users<\/span><\/strong>.<\/p>\n\n\n\n<p>We also explored how to use the basic terraform <span id=\"urn:enhancement-86fe3b06\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/command_computing\">commands<\/span> to generate our <span id=\"urn:enhancement-d9d16daf\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/resource\">resources<\/span> and terminate them when we&#8217;re done, such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>init<\/code><\/li><li><code>plan<\/code><\/li><li><code>apply<\/code><\/li><li><code>destroy<\/code><\/li><\/ul>\n\n\n\n<p id=\"h-in-this-article-we-will-see-in-more-detail-how-to-structure-a-terraform-project-in-more-depth-dividing-the-logic-into-multiple-files-in-order-to-make-everything-more-well-ordered-and-maintainable\">In this article, we will see in more detail <strong>how to <span id=\"urn:enhancement-d714d71c\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/structure\">structure<\/span> a Terraform <span id=\"urn:enhancement-8ca4f64b\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/project\">project<\/span><\/strong> in more depth, dividing the logic into multiple <span id=\"urn:enhancement-dd68e391\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computer_file\">files<\/span>, in order to make everything more well-ordered and maintainable.<\/p>\n\n\n\n<p>By the end of the article, you will also understand <strong>how to use <span id=\"urn:enhancement-69342761\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span><\/strong>, and what <span id=\"urn:enhancement-b455467e\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/programming_tool\">tools<\/span> are available to you to make your <span id=\"urn:enhancement-4383b3b1\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/project\">project<\/span> more dynamic. In this way, you can create a single <span id=\"urn:enhancement-ac2df040\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/web_template_system\">template<\/span> and use it in multiple environments, for example.<\/p>\n\n\n\n<p>You will also understand the <span id=\"urn:enhancement-c4e64898\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/interdependence\">interdependence<\/span> between <span id=\"urn:enhancement-64b442c5\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/resource\">resources<\/span> and how to retrieve information that is present in your cloud <span id=\"urn:enhancement-f7e3b979\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">account<\/span> in order to build increasingly complex <span id=\"urn:enhancement-3e04f4b8\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/resource\">resources<\/span>.<\/p>\n\n\n\n<p>A <a href=\"https:\/\/github.com\/diegoitaliait\/terraform-aws-showcase\/tree\/develop\/base_2\" target=\"_blank\" aria-label=\"GitHub project (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\">GitHub project<\/a>, with a ready-to-use example that I will refer to, accompanies this article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-prerequisites\">Prerequisites<\/h2>\n\n\n\n<p>In order to start, you will need to know these prerequisites:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong><span id=\"urn:local-annotation-626744\" class=\"textannotation disambiguated\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/unix\">UNIX<\/span>-based environment<\/strong> (I used <span id=\"urn:local-annotation-551005\" class=\"textannotation disambiguated\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/ubuntu_operating_system\">Ubuntu<\/span> or WLS in <span id=\"urn:local-annotation-122363\" class=\"textannotation disambiguated\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/windows_10\">Windows 10<\/span>)<\/li><li><strong>Terraform <span id=\"urn:local-annotation-189995\" class=\"textannotation disambiguated\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/command-line_interface\">CLI<\/span><\/strong> (if you haven&#8217;t installed it yet, see the <a class=\"ek-link\" href=\"https:\/\/www.codemotion.com\/magazine\/dev-hub\/cloud-manager\/terraform-aws\/\">previous article<\/a>)<\/li><li><strong>AWS<\/strong>: basic knowledge of terminology, and security and resource cost issues<\/li><li>At least one <strong>AWS account<\/strong> (I used the<a aria-label=\" free tier for this lab (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\" href=\"https:\/\/aws.amazon.com\/free\" target=\"_blank\"> free tier for this lab<\/a>) which will allow us to create our resources without initial investments<\/li><li>Basic knowledge of any <a class=\"ek-link ek-link\" href=\"https:\/\/www.codemotion.com\/magazine\/dev-hub\/backend-dev\/programming-languages-how-to-choose-the-best-one\/\">programming language<\/a><\/li><\/ol>\n\n\n\n<p>Before starting, I want to return to the importance of <span id=\"urn:enhancement-d33bf282\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/knowledge\">knowledge<\/span> of the <strong><span id=\"urn:enhancement-10a608f7\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/cloud_computing\">cloud<\/span><\/strong>, and the great responsibilities this brings, especially from an economic point of view.<\/p>\n\n\n\n<p>In fact, we are going to create real <span id=\"urn:enhancement-df9d9517\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/resource\">resources<\/span> and it is necessary to be very careful with the <span id=\"urn:enhancement-7896c7af\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/test_assessment\">tests<\/span> that are carried out to avoid finding yourself paying for <span id=\"urn:enhancement-c05e1acd\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/resource\">resources<\/span> that have been created only once as a test, but which, from the point of view of <span id=\"urn:enhancement-def47010\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/amazon_web_services\">AWS<\/span>, are in effect <span id=\"urn:enhancement-bfb462b7\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/resource\">resources<\/span> with associated costs due from the <span id=\"urn:enhancement-98e1a8d9\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/user_computing\">account<\/span><\/p>\n\n\n\n<p>I speak from personal <span id=\"urn:enhancement-862fb49\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/experience\">experience<\/span>, so I strongly advise you to <a href=\"https:\/\/docs.aws.amazon.com\/AmazonCloudWatch\/latest\/monitoring\/monitor_estimated_charges_with_cloudwatch.html\" target=\"_blank\" aria-label=\"insert alerts (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\">insert alerts<\/a> (more than one) that will notify you immediately if you are starting to pay unexpected amounts of money.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-a-dynamic-terraform-aws-project-in-continuous-growth\">A dynamic Terraform AWS project in continuous growth<\/h2>\n\n\n\n<p>While in the first article we treated an example of a static, simple and immediate <span id=\"urn:enhancement-fae17586\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/project\">project<\/span> to get to the Terraform juice, now we want to get into the specifics, simulating a possible work <span id=\"urn:enhancement-101e2ed8\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/project\">project<\/span>.<\/p>\n\n\n\n<p>We will focus more on the <span id=\"urn:enhancement-9610eb26\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/organization\">organisation<\/span> of the <span id=\"urn:enhancement-5b2e35f5\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/project\">project<\/span>, dividing it into several <span id=\"urn:enhancement-226f2dd1\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computer_file\">files<\/span> that will simplify <span id=\"urn:enhancement-27ddba0d\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/management\">management<\/span>.<\/p>\n\n\n\n<p>In the simulation, we are managing a request from the customer who wants an <strong><span id=\"urn:enhancement-58c64c60\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/amazon_elastic_compute_cloud\">EC2<\/span> VM<\/strong> that can be created in different environments (<em>DEV<\/em>, <em>STAGE<\/em>, <em>PROD<\/em>), in order to support various development phases.<\/p>\n\n\n\n<p>To be able to do this, therefore, we must begin to deal in depth with Terraform\u2019s management of <span id=\"urn:enhancement-20e8da88\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span>. In this way, we can create a single template valid for all environments that will behave differently depending on the value of our <span id=\"urn:enhancement-13f8e5d8\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-input-variables-in-terraform\">Input Variables in Terraform<\/h2>\n\n\n\n<p>If we want to have an orderly and dynamic <span id=\"urn:enhancement-c7d0b6a1\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/project\">project<\/span>, the <span id=\"urn:enhancement-e32c2ffe\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/tool\">tool<\/span> that immediately comes to our aid is the use of <span id=\"urn:enhancement-eb625571\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span>, which allows the creation of a unique <span id=\"urn:enhancement-6511f0b1\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/web_template_system\">template<\/span> that will behave differently depending on the values \u200b\u200bwe are going to use.<\/p>\n\n\n\n<p>Not only that &#8211; another advantage of <span id=\"urn:enhancement-12eb6308\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span> is the ability to give a <span id=\"urn:enhancement-29253988\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/business\">business<\/span> meaning to each value, in order to simplify the problem determination in case of errors, or the expansion of the <span id=\"urn:enhancement-a39bd2b9\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/project\">project<\/span> itself.<\/p>\n\n\n\n<p>For more in-depth information, check the documentation pages dedicated to <a href=\"https:\/\/www.terraform.io\/docs\/configuration\/variables.html\" target=\"_blank\" aria-label=\"input variables (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\">input variables<\/a> and <a href=\"https:\/\/www.terraform.io\/docs\/configuration\/expressions.html\" target=\"_blank\" aria-label=\"expressions (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link\">expressions<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-variables-structure-and-types\">Variables: structure and types<\/h3>\n\n\n\n<p><span id=\"urn:enhancement-6e67f050\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">Variables<\/span> in Terraform come in three macro types:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>primitive:<ul><li>string<\/li><li>number<\/li><li>boolean<\/li><\/ul><\/li><li>collections<ul><li>maps<\/li><li>list<\/li><li>set<\/li><li>tuple<\/li><li>objects<\/li><\/ul><\/li><li>generic<ul><li>any (when you don&#8217;t know or don&#8217;t want to force a type). If no type is entered, terraform automatically considers a variable as type ANY<\/li><\/ul><\/li><\/ul>\n\n\n\n<p>In this article we will deal only with the <span id=\"urn:enhancement-d23d1be0\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/primitive_data_type\">primitive types<\/span> for simplicity. In the next article of this series, we will deal with the other types in more detail.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-variables-concrete-example\">Variables: concrete example<\/h3>\n\n\n\n<p>This is an example of the <span id=\"urn:enhancement-fa150728\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span> that we are going to use.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"548\" height=\"485\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image8.png\" alt=\"terraform aws variables\" class=\"wp-image-12704\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image8.png 548w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image8-300x266.png 300w\" sizes=\"auto, (max-width: 548px) 100vw, 548px\" \/><\/figure><\/div>\n\n\n\n<p>The <span id=\"urn:enhancement-9b007663\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span> can be defined anywhere in Terraform, as long as it occurs inside a <span id=\"urn:enhancement-540139a0\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computer_file\">file<\/span> with the TF extension. In the previous example we used the <em>main.tf<\/em>.<\/p>\n\n\n\n<p>In this case, to simulate a more complex <span id=\"urn:enhancement-b55f8fd7\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/project\">project<\/span> the use of a dedicated <span id=\"urn:enhancement-85b2fffc\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computer_file\">file<\/span> called <strong>vars.tf<\/strong> is highly recommended. This must be in the same folder that contains the main.tf.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"255\" height=\"155\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image1-3.png\" alt=\"terraform aws structure of variables\" class=\"wp-image-12697\"\/><\/figure><\/div>\n\n\n\n<p>The <span id=\"urn:enhancement-ed1aeeb2\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/structure\">structure<\/span> of a <span id=\"urn:enhancement-d8375c63\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variable<\/span> is:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code><strong><em>variable<\/em><\/strong> \"&lt;<strong>variable name<\/strong>&gt;\" =<\/code><br>Here, the name of the variable is defined (in fact, if we wanted to, we could stop here too &#8211; terraform assigns ANY as the default type, e.g. <strong>plain<\/strong>).&nbsp; Alternatively, we can choose to be more detailed and invest our time in being precise in the definition and construction. From personal experience, when the project begins to grow without any such precautions, when a problem arises it becomes difficult to get to the point in a short time.<br><br>The remaining information is:<ul><li><em>description <\/em>(optional): the description of our variable;<\/li><li><em>type <\/em>(optional): defines the type of the variable (see the previous list for the various types offered, or check the <a aria-label=\"documentation (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"ek-link ek-link\" href=\"https:\/\/www.terraform.io\/docs\/configuration\/variables.html#type-constraints\" target=\"_blank\"><span id=\"urn:local-annotation-225001\" class=\"textannotation disambiguated\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/documentation\">documentation<\/span><\/a> for more details);<\/li><li><em>default <\/em>(optional): defines the default value that we want the variable to have if no value is entered. Obviously the default value follows the type of the variable, so a variable of type list has as default <code>[]<\/code>, while a variable of type bool will have a default value of <code>true<\/code>. It is very important to keep this in mind, as sometimes there can be correlation errors between variables.<\/li><\/ul><\/li><\/ul>\n\n\n\n<p>There are at least two ways to use the <span id=\"urn:enhancement-b2ff4427\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span> we have just created:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>through interpolation:<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"618\" height=\"42\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image10.png\" alt=\"\" class=\"wp-image-12706\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image10.png 618w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image10-300x20.png 300w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image10-600x42.png 600w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><\/figure><\/div><\/li><li>plain-direct:<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"234\" height=\"68\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image12.png\" alt=\"\" class=\"wp-image-12709\"><\/figure><\/div>Note that in previous versions of Terraform 0.11, the use of <code>${var.env}<\/code> was always mandatory.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-locals\">Locals<\/h3>\n\n\n\n<p>Within a real <span id=\"urn:enhancement-64db397f\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/project\">project<\/span>, we cannot limit ourselves to having only AS-IS <span id=\"urn:enhancement-7357a767\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span>; we also need to be able, for example, to concatenate information, or make it dynamic, in the case of a machine name that depends on the <span id=\"urn:enhancement-1ba40210\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/environment_variable\">environment<\/span>. For <span id=\"urn:enhancement-7ef6285e\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/instance_computer_science\">instance<\/span>, we can have names such as <code><strong>test<\/strong>_micro_vm<\/code> or <code><strong>dev<\/strong>_micro_vm<\/code>.<\/p>\n\n\n\n<p>In order to achieve this <span id=\"urn:enhancement-4b6544bc\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/goal\">goal<\/span>, Terraform provides us with the <strong>locals<\/strong> <span id=\"urn:enhancement-59c8ee97\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/tool\">tool<\/span>:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"445\" height=\"85\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image9.png\" alt=\"terraform aws locals\" class=\"wp-image-12705\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image9.png 445w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image9-300x57.png 300w\" sizes=\"auto, (max-width: 445px) 100vw, 445px\" \/><\/figure><\/div>\n\n\n\n<p>This <span id=\"urn:enhancement-9b8a8ac\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/tool\">tool<\/span> allows the creation of a new special <span id=\"urn:enhancement-4110024a\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variable<\/span>, starting from the manipulation of previous <span id=\"urn:enhancement-365e82f3\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span>.<\/p>\n\n\n\n<p>To be able to use the new local <span id=\"urn:enhancement-1a81585\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variable<\/span>, it will be enough to recall them in the following way:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"406\" height=\"48\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image7.png\" alt=\"\" class=\"wp-image-12703\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image7.png 406w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image7-300x35.png 300w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image7-400x48.png 400w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/figure><\/div>\n\n\n\n<p>Be careful! If I use a <span id=\"urn:enhancement-5f799ff2\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variable<\/span> I will call it using <strong>var.xyz<\/strong>, while if it is local it is called with <strong>local.abc<\/strong>.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to give value to our variable\u2019s Variable<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Precedence<\/h3>\n\n\n\n<p>Before talking about the various <span id=\"urn:enhancement-230a1a91\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/method_computer_programming\">methods<\/span> we have to value our <span id=\"urn:enhancement-4b5dd930\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span> and therefore eventually give them a fundamental <span id=\"urn:enhancement-991a5b31\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/business\">business<\/span> value for our <span id=\"urn:enhancement-62125ef0\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/project\">project<\/span> to take shape, it is important to discuss the precedence of <span id=\"urn:enhancement-7c3355dc\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span>.<\/p>\n\n\n\n<p>Terraform uses <strong>hierarchical logic<\/strong> to decide which value to consider in the event of a conflict.<\/p>\n\n\n\n<p>The order is as follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Environment variables<\/li><li>The <code>terraform.tfvars<\/code> file, if present<\/li><li>The <code>terraform.tfvars.json<\/code> file, if present<\/li><li>Any <code>*.auto.tfvars<\/code> or <code>*.auto.tfvars.json<\/code> files, processed in lexical order of their filenames<\/li><li>Any <code>-var<\/code> and <code>-var-file<\/code> options on the command line, in the order they are provided<\/li><\/ol>\n\n\n\n<p>In this guide we will not talk about <code>auto.tfvars<\/code>, as in my opinion these are difficult to manage, but it is useful to know that this <span id=\"urn:enhancement-37c97051\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/method_computer_programming\">method<\/span> also exists.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Environment variables<\/h3>\n\n\n\n<p>This is the first <span id=\"urn:enhancement-9b496711\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/entry_point\">entry point<\/span> when it comes to giving a value to <span id=\"urn:enhancement-3c2355eb\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span>. By <span id=\"urn:enhancement-ae6809df\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span>, I mean here not only the <span id=\"urn:enhancement-d6a8e1b\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span> we have defined within our var <span id=\"urn:enhancement-a84eee82\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computer_file\">file<\/span>, but also the configuration <span id=\"urn:enhancement-2ba6243e\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span> that terraform makes available to configure, for example, <span id=\"urn:enhancement-ade57559\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/management\">management<\/span> of the <span id=\"urn:enhancement-d302ee9f\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/data_logger\">logs<\/span> or the location of the <span id=\"urn:enhancement-8838c4e3\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/project\">project<\/span> <span id=\"urn:enhancement-5f411dbd\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computer_file\">files<\/span>.<\/p>\n\n\n\n<p>In this article we will only talk about how to manage the value of our <span id=\"urn:enhancement-cbbe0daf\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span>; for everything else, refer to <a aria-label=\"this link (opens in a new tab)\" href=\"https:\/\/www.terraform.io\/docs\/commands\/environment-variables.html\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"ek-link\">this link<\/a> which gives a better explanation of all the various options.<\/p>\n\n\n\n<p>To be able to value our newly created personal <span id=\"urn:enhancement-a7ff5cb7\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span>, Terraform uses a very simple convention:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">TF_VAR_<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">name<\/span> <span class=\"hljs-attr\">variable<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>So for example, if we had created an &#8220;env&#8221; <span id=\"urn:enhancement-f20e92b4\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variable<\/span>, in order to value it we must use:<\/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\"><span class=\"hljs-keyword\">export<\/span> TF_VAR_env = PROD<\/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>This <span id=\"urn:enhancement-24eef4b2\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/method_computer_programming\">method<\/span> of valuing <span id=\"urn:enhancement-6154a128\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span> is very useful when you want to:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>avoid inserting a sensitive value in the code, such as the aws secret key. In this case terraform knows the name of the variable and when it starts the project, the first thing it does is query the environment variables to load the sensitive value:<br><code>export AWS_ACCESS_KEY_ID = DASDSAXCKSCNK<\/code><\/li><li>For automation: in this case, taking advantage of the fact that terraform gives absolute priority to environment variables, I can overwrite the value of the environment where I want to deploy terraform, for example, and force a certain behaviour,<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-terraform-tfvars\">terraform.tfvars<\/h3>\n\n\n\n<p>This <span id=\"urn:enhancement-75f15d\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computer_file\">file<\/span> is used to define the default values \u200b\u200bof a <span id=\"urn:enhancement-78e86435\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variable<\/span>.<\/p>\n\n\n\n<p>In fact, even if it is very convenient to be able to use various <span id=\"urn:enhancement-3ba8d4ab\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computer_file\">files<\/span> to contain the variants of our <span id=\"urn:enhancement-de77d7f8\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span>, it is true that there will always be <span id=\"urn:enhancement-ad24ac53\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span> that are <span id=\"urn:enhancement-2a158d8e\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/constant_computer_programming\">constant<\/span> even if we change the <span id=\"urn:enhancement-8829116f\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/environment_variable\">environment<\/span>. Having to map these every time to every single <span id=\"urn:enhancement-b9037247\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computer_file\">file<\/span> leads us not only to a waste of time, but also to possible errors.<\/p>\n\n\n\n<p>For this there is <strong>terraform.tfvars<\/strong> This is the first <span id=\"urn:enhancement-2d2d59c8\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computer_file\">file<\/span> loaded by Terraform to enhance our <span id=\"urn:enhancement-63aa1917\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span> and can consequently be used as a <span id=\"urn:enhancement-dbbeed9a\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/software_repository\">repository<\/span> for all the global or default <span id=\"urn:enhancement-a8fc3cc4\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variables<\/span> that we want to share within the <span id=\"urn:enhancement-a3dcc7c7\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/project\">project<\/span>.<\/p>\n\n\n\n<p>To be able to use it, just create a <span id=\"urn:enhancement-c0685ef7\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computer_file\">file<\/span> called <em>terraform.tfvars<\/em> in the same folder as the <em>main.tf<\/em>. The syntax inside is very simple, as shown in the example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">my_default_variable = <span class=\"hljs-string\">\"default\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><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\" id=\"h-var-files\">Var files<\/h3>\n\n\n\n<p>With all that achieved, we are finally going to see how to organise the values \u200b\u200baccording to the environments and start to approach our imaginary customer&#8217;s request.<\/p>\n\n\n\n<p>This is where the concept of vars <span id=\"urn:enhancement-d82b5c1c\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computer_file\">files<\/span> comes in handy.&nbsp; These are <span id=\"urn:enhancement-73e9967c\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computer_file\">files<\/span> with the <strong>TFVARS<\/strong> extension, and unlike <span id=\"urn:enhancement-ac1a9aaf\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/variable_computer_science\">variable<\/span> <span id=\"urn:enhancement-4bf0b42f\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computer_file\">files<\/span>, can be anywhere in our <span id=\"urn:enhancement-553daae0\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/project\">project<\/span>.<\/p>\n\n\n\n<p>Here is an example snippet of a vars file <em>base_2\\vars_files\\variables_dev.tfvars<\/em>:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"273\" height=\"128\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image13.png\" alt=\"\" class=\"wp-image-12710\"\/><\/figure><\/div>\n\n\n\n<p>As you can see, these recall the name of our variables, and we feed them with the values \u200b\u200bwe want.<\/p>\n\n\n\n<p>To be able to recall these variables, just use the command:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">terraform <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">command<\/span>&gt;<\/span> -var-file=vars_files\\variables_dev.tfvars<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Thanks to this, we can use any vars file that respects our variables. In this case, you contain the info for other environments such as <em>STAGE<\/em>, for example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-variables-from-command-line\">Variables from command line<\/h3>\n\n\n\n<p>Another way in which we can value our variables is via the command line. In this case, we simply use the command:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">terraform <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">command<\/span>&gt;<\/span> -var=\"<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">variable<\/span> <span class=\"hljs-attr\">name<\/span>&gt;<\/span>=<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">value<\/span>&gt;<\/span>\"<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">terraform &lt;command: apply\/plan&gt; -<span class=\"hljs-keyword\">var<\/span>=<span class=\"hljs-string\">\"env=dev\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><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>This type of use is definitely not recommended as it is difficult to maintain. I strongly suggest using&nbsp; the other methods previously discussed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Variables with user input<\/h3>\n\n\n\n<p>If you want to have user input, it can be done simply &#8211; just don&#8217;t value the variable.<\/p>\n\n\n\n<p>In this case, Terraform automatically realises that the variable has not yet been evaluated, and will ask you to enter its value on the screen, as each variable that is defined within the Terraform must have a value.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"557\" height=\"62\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image2-3.png\" alt=\"terraform aws\" class=\"wp-image-12698\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image2-3.png 557w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image2-3-300x33.png 300w\" sizes=\"auto, (max-width: 557px) 100vw, 557px\" \/><\/figure><\/div>\n\n\n\n<p>In this example, I purposely created a variable that is not valued anywhere and terraform immediately asked me for its value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Data resources: how to retrieve information outside Terraform<\/h2>\n\n\n\n<p>While developing your project, at some point (very soon) you will find yourself in a situation where you need information that is located outside your terraform project.<\/p>\n\n\n\n<p>For example in our sample project, in order to build my EC2, I need to know the AMI ID to use for my vm. This ID could change over time or depending on the region where I am, therefore it is not possible to have it static in the code. I need to ask AWS to form it for me.<\/p>\n\n\n\n<p>This is where the data source component comes into play &#8211; it allows us to upload information outside of our project and then be able to use it at will.<\/p>\n\n\n\n<p>Terraform provides an infinite range of datasources for nearly every resource it creates. As a result, it is possible not only to create the resources, but also to query them and loading the necessary data and be able to reuse them at a later time.<\/p>\n\n\n\n<p>Here is an example (from <a aria-label=\"here (opens in a new tab)\" href=\"https:\/\/registry.terraform.io\/providers\/hashicorp\/aws\/latest\/docs\/data-sources\/vpc\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"ek-link\">here<\/a>):<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"820\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image11-1024x820.png\" alt=\"terraform aws data sources\" class=\"wp-image-12708\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image11-1024x820.png 1024w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image11-300x240.png 300w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image11-768x615.png 768w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image11.png 1061w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>As can be seen within the Terraform documentation for each resource (in this case VPC) there is both a subsection <strong>Resources<\/strong> (where their use to create the resource is explained), but also a subsection <strong><span id=\"urn:local-annotation-904818\" class=\"textannotation disambiguated\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/data\">Data<\/span> Sources.<\/strong><\/p>\n\n\n\n<p>Here you will find:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>a brief explanation of the component<\/li><li>examples&nbsp;<\/li><li>arguments (necessary and optional) for their use<\/li><li>attributes returned: i.e., the values \u200b\u200bthat you can obtain to be able to use them later<\/li><\/ul>\n\n\n\n<p>In our example project we have:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"511\" height=\"803\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image3-3.png\" alt=\"terraform aws\" class=\"wp-image-12699\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image3-3.png 511w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image3-3-191x300.png 191w\" sizes=\"auto, (max-width: 511px) 100vw, 511px\" \/><\/figure><\/div>\n\n\n\n<p>Where in order to define a <span id=\"urn:local-annotation-852556\" class=\"textannotation disambiguated\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/data_source\">data source<\/span>, it is necessary to use the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">data \"<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">datasource<\/span> <span class=\"hljs-attr\">id<\/span>&gt;<\/span>\" \"<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">local<\/span> <span class=\"hljs-attr\">name<\/span> \/ <span class=\"hljs-attr\">id<\/span>&gt;<\/span>\" {\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">attribute<\/span>&gt;<\/span>=<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">value<\/span>&gt;<\/span>\n    ...        \n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In order to use the attributes&nbsp;that are made&nbsp;available, just use the following notation:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">given.<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">datasource<\/span> <span class=\"hljs-attr\">id<\/span>&gt;<\/span>.<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">local<\/span> <span class=\"hljs-attr\">name<\/span>&gt;<\/span>.<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">attribute<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For instance:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">data<\/span><span class=\"hljs-selector-class\">.aws_ami<\/span><span class=\"hljs-selector-class\">.amazon_linux_ami<\/span><span class=\"hljs-selector-class\">.id<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you execute a plan \/ apply command you will see that terraform will load the requested information with the data source, so that it is available immediately afterwards:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"698\" height=\"48\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image6.png\" alt=\"terraform aws\" class=\"wp-image-12702\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image6.png 698w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image6-300x21.png 300w\" sizes=\"auto, (max-width: 698px) 100vw, 698px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h.5d7rt9l2b4y2\">Outputs<\/h2>\n\n\n\n<p>This is the last feature we will talk about. For now we will focus on the simplest use of outputs, which is to print the result of a process on the screen, or the content of the variables we used. Further on, we can see how output is used within modules to make a value available to whoever invokes it.<\/p>\n\n\n\n<p>In our example project we can see how we defined the outputs:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"361\" height=\"236\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image4-3.png\" alt=\"terraform aws output\" class=\"wp-image-12700\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image4-3.png 361w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image4-3-300x196.png 300w\" sizes=\"auto, (max-width: 361px) 100vw, 361px\" \/><\/figure><\/div>\n\n\n\n<p>with the following result:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"656\" height=\"137\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image5-2.png\" alt=\"terraform aws output\" class=\"wp-image-12701\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image5-2.png 656w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/image5-2-300x63.png 300w\" sizes=\"auto, (max-width: 656px) 100vw, 656px\" \/><\/figure><\/div>\n\n\n\n<p>The outputs are very useful in both the development phase (as <span id=\"urn:local-annotation-541486\" class=\"textannotation disambiguated\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/debugging\">debugging<\/span> a run with Terraform is not always easy), and in terms of having useful information on screen in order to understand the progress of resource creation, such as when you have automatic pipelines and want to see in the logs, a report of what they have created.<\/p>\n\n\n","protected":false},"excerpt":{"rendered":"<p>In the previous article of our series about Terraform AWS, we looked at an initial project that allowed us to create an SQS-type resource, understanding how to configure Terraform so that it can communicate with AWS thanks to programmatic access users. We also explored how to use the basic terraform commands to generate our resources&#8230; <a class=\"more-link\" href=\"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/\">Read more<\/a><\/p>\n","protected":false},"author":103,"featured_media":12715,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_editorskit_title_hidden":false,"_editorskit_reading_time":6,"_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","_uag_custom_page_level_css":"","_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[5244],"tags":[71,7224],"collections":[],"class_list":{"0":"post-12691","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-cloud","8":"tag-aws","9":"tag-terraform","10":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v26.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Terraform AWS: Files and Variables - Codemotion Magazine<\/title>\n<meta name=\"description\" content=\"In this tutorial, we&#039;ll learn how to properly structure a project based on AWS Terraform into multiple files, and how to use variables.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Terraform AWS: Files and Variables\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, we&#039;ll learn how to properly structure a project based on AWS Terraform into multiple files, and how to use variables.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/\" \/>\n<meta property=\"og:site_name\" content=\"Codemotion Magazine\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Codemotion.Italy\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-11T12:07:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-01-05T19:06:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Diego Lagos\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodemotionIT\" \/>\n<meta name=\"twitter:site\" content=\"@CodemotionIT\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Diego Lagos\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/\"},\"author\":{\"name\":\"Diego Lagos\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/d2fd5340943c72ee9429c7ff78e089b9\"},\"headline\":\"Terraform AWS: Files and Variables\",\"datePublished\":\"2020-12-11T12:07:24+00:00\",\"dateModified\":\"2022-01-05T19:06:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/\"},\"wordCount\":2367,\"publisher\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash.jpg\",\"keywords\":[\"AWS\",\"Terraform\"],\"articleSection\":[\"Cloud\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/\",\"name\":\"Terraform AWS: Files and Variables - Codemotion Magazine\",\"isPartOf\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash.jpg\",\"datePublished\":\"2020-12-11T12:07:24+00:00\",\"dateModified\":\"2022-01-05T19:06:43+00:00\",\"description\":\"In this tutorial, we'll learn how to properly structure a project based on AWS Terraform into multiple files, and how to use variables.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/#primaryimage\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash.jpg\",\"contentUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash.jpg\",\"width\":1200,\"height\":675,\"caption\":\"cloud developer\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.codemotion.com\/magazine\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"DevOps\",\"item\":\"https:\/\/www.codemotion.com\/magazine\/devops\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Cloud\",\"item\":\"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Terraform AWS: Files and Variables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#website\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/\",\"name\":\"Codemotion Magazine\",\"description\":\"We code the future. Together\",\"publisher\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.codemotion.com\/magazine\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\",\"name\":\"Codemotion\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png\",\"contentUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png\",\"width\":225,\"height\":225,\"caption\":\"Codemotion\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/Codemotion.Italy\/\",\"https:\/\/x.com\/CodemotionIT\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/d2fd5340943c72ee9429c7ff78e089b9\",\"name\":\"Diego Lagos\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3a2794ba6956efb870864dfbddcf44bb33b240312e0a5ebd95f8b27b5c9aeac6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3a2794ba6956efb870864dfbddcf44bb33b240312e0a5ebd95f8b27b5c9aeac6?s=96&d=mm&r=g\",\"caption\":\"Diego Lagos\"},\"url\":\"https:\/\/www.codemotion.com\/magazine\/author\/diego-lagos\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Terraform AWS: Files and Variables - Codemotion Magazine","description":"In this tutorial, we'll learn how to properly structure a project based on AWS Terraform into multiple files, and how to use variables.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/","og_locale":"en_US","og_type":"article","og_title":"Terraform AWS: Files and Variables","og_description":"In this tutorial, we'll learn how to properly structure a project based on AWS Terraform into multiple files, and how to use variables.","og_url":"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/","og_site_name":"Codemotion Magazine","article_publisher":"https:\/\/www.facebook.com\/Codemotion.Italy\/","article_published_time":"2020-12-11T12:07:24+00:00","article_modified_time":"2022-01-05T19:06:43+00:00","og_image":[{"width":1200,"height":675,"url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash.jpg","type":"image\/jpeg"}],"author":"Diego Lagos","twitter_card":"summary_large_image","twitter_creator":"@CodemotionIT","twitter_site":"@CodemotionIT","twitter_misc":{"Written by":"Diego Lagos","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/#article","isPartOf":{"@id":"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/"},"author":{"name":"Diego Lagos","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/d2fd5340943c72ee9429c7ff78e089b9"},"headline":"Terraform AWS: Files and Variables","datePublished":"2020-12-11T12:07:24+00:00","dateModified":"2022-01-05T19:06:43+00:00","mainEntityOfPage":{"@id":"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/"},"wordCount":2367,"publisher":{"@id":"https:\/\/www.codemotion.com\/magazine\/#organization"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash.jpg","keywords":["AWS","Terraform"],"articleSection":["Cloud"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/","url":"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/","name":"Terraform AWS: Files and Variables - Codemotion Magazine","isPartOf":{"@id":"https:\/\/www.codemotion.com\/magazine\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/#primaryimage"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash.jpg","datePublished":"2020-12-11T12:07:24+00:00","dateModified":"2022-01-05T19:06:43+00:00","description":"In this tutorial, we'll learn how to properly structure a project based on AWS Terraform into multiple files, and how to use variables.","breadcrumb":{"@id":"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/#primaryimage","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash.jpg","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash.jpg","width":1200,"height":675,"caption":"cloud developer"},{"@type":"BreadcrumbList","@id":"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/terraform-aws-files-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.codemotion.com\/magazine\/"},{"@type":"ListItem","position":2,"name":"DevOps","item":"https:\/\/www.codemotion.com\/magazine\/devops\/"},{"@type":"ListItem","position":3,"name":"Cloud","item":"https:\/\/www.codemotion.com\/magazine\/devops\/cloud\/"},{"@type":"ListItem","position":4,"name":"Terraform AWS: Files and Variables"}]},{"@type":"WebSite","@id":"https:\/\/www.codemotion.com\/magazine\/#website","url":"https:\/\/www.codemotion.com\/magazine\/","name":"Codemotion Magazine","description":"We code the future. Together","publisher":{"@id":"https:\/\/www.codemotion.com\/magazine\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.codemotion.com\/magazine\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.codemotion.com\/magazine\/#organization","name":"Codemotion","url":"https:\/\/www.codemotion.com\/magazine\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png","width":225,"height":225,"caption":"Codemotion"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Codemotion.Italy\/","https:\/\/x.com\/CodemotionIT"]},{"@type":"Person","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/d2fd5340943c72ee9429c7ff78e089b9","name":"Diego Lagos","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/3a2794ba6956efb870864dfbddcf44bb33b240312e0a5ebd95f8b27b5c9aeac6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3a2794ba6956efb870864dfbddcf44bb33b240312e0a5ebd95f8b27b5c9aeac6?s=96&d=mm&r=g","caption":"Diego Lagos"},"url":"https:\/\/www.codemotion.com\/magazine\/author\/diego-lagos\/"}]}},"featured_image_src":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash-600x400.jpg","featured_image_src_square":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash-600x600.jpg","author_info":{"display_name":"Diego Lagos","author_link":"https:\/\/www.codemotion.com\/magazine\/author\/diego-lagos\/"},"uagb_featured_image_src":{"full":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash.jpg",1200,675,false],"thumbnail":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash-150x150.jpg",150,150,true],"medium":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash-300x169.jpg",300,169,true],"medium_large":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash-768x432.jpg",768,432,true],"large":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash-1024x576.jpg",1024,576,true],"1536x1536":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash.jpg",1200,675,false],"2048x2048":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash.jpg",1200,675,false],"small-home-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash.jpg",100,56,false],"sidebar-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash-180x128.jpg",180,128,true],"genesis-singular-images":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash-896x504.jpg",896,504,true],"archive-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash-400x225.jpg",400,225,true],"gb-block-post-grid-landscape":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash-600x400.jpg",600,400,true],"gb-block-post-grid-square":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/12\/sigmund-oFKyMk4_5gA-unsplash-600x600.jpg",600,600,true]},"uagb_author_info":{"display_name":"Diego Lagos","author_link":"https:\/\/www.codemotion.com\/magazine\/author\/diego-lagos\/"},"uagb_comment_info":0,"uagb_excerpt":"In the previous article of our series about Terraform AWS, we looked at an initial project that allowed us to create an SQS-type resource, understanding how to configure Terraform so that it can communicate with AWS thanks to programmatic access users. We also explored how to use the basic terraform commands to generate our resources&#8230;&hellip;","lang":"en","_links":{"self":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/12691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/users\/103"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/comments?post=12691"}],"version-history":[{"count":8,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/12691\/revisions"}],"predecessor-version":[{"id":12725,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/12691\/revisions\/12725"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/media\/12715"}],"wp:attachment":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/media?parent=12691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/categories?post=12691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/tags?post=12691"},{"taxonomy":"collections","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/collections?post=12691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}