Input Variables with Terraform and Azure

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we review variables in terraform with azure [Music] we're going to cover variables in terraform with this video if you spent any time with programming you probably know how important variables are for those getting started i'll give a quick overview if you're getting started with terraform and azure check out my other videos on terraform and azure in the playlist uh you'll be up here someplace before that please like subscribe share with a friend and click the bell icon for notifications of new content also check out my course on azure virtual desktop and the other on hybrid identities with azure ad on udemy.com the link is below also consider checking out my new join button on this channel so what is a variable it's something i've used so much i haven't given much thought to a definition it's a placeholder for a value how's that maybe an example will help we deployed a resource group in the last video one argument we had to supply was the location now imagine we're deploying multiple resources and we add the same location to each resource part of the value proposition with terraform is that we can reuse the code once it's written let's say we want to reuse the code in a new region we could update all instances of the location to the new location but that's not efficient instead we could use a variable a variable is a name and a value pair we can establish a location variable in the code then instead of supplying a literal location for the resource block we reference the variable when we reuse the code we simply update the value of the location variable and all resources that reference the variable will get the new value let's go to vs code and see how this works in terraform let's start where we left off in the last video we set the provider and created a resource group for the resource let's make this a little bit more interesting by adding another resource a storage block this time let's hop over to registry.terraform.io and then find azure there it is we'll go to documentation and search for storage account under storage there's azure rm storage account let's click on that let's copy the storage account resource we don't need the resource group we already have that so we'll copy this and let's paste it into our main.tf file we'll add it right under the resource group we have to update a couple things before we move on the local name for the resource group is resource group the code we pasted in is configured with the resource group named example so let's update the two lines there we go now that's referencing the name of the location of the resource group with the local name resource group okay now we have two resources let's create our first variable called location we'll add this between the provider and the resource block type var var if you have the terraform extension you can tab to complete so that adds the variable as well as the quotes and the opening and closing curly brackets let's let's delete that and try something else we go v-a-r-e that gives us an empty variable and if we tab here now we get a little bit more information these are code blocks that are added to vs code you could simply type this all in manually if you didn't have that but i'm going to show you how to take advantage of these code blocks so we have variable that's the beginning of a variable block and we're prompted to enter a name the name is location we can tab until we get to the type we've got string number and bowling click tab again and that brings us to description and if we click tab one more time it automatically selects the string between the double quotes so let's type in a new description the location for the deployment we'll go to the end of the line and hit enter now we're going to add a default value we'll enter default equals double quote west u.s or any region you'd like that's our first variable block next let's go to the location in the resource group here we have the location specified by a string let's remove that string including the double quotes and we'll replace that with var dot location now the location for the deployment is a variable the resource group uses this variable and the storage account uses the resource group location so by changing one variable we can change the location for both the resource group and the storage account we could use the variable for all of the arguments in this example let's create variables for the resource group name and the storage account name we'll create a new variable block rsg name for resource group name that's a string we can give it a description go to the end of the line hit enter and we'll default it to terraform rg let's add another block this one is for the storage account name that 2 is a string for the storage account we won't add a default value a storage account name has to be globally unique adding a default value wouldn't do any good because the value can only be used once next go to resource group we'll change the name to var dot rsg name and the same with the storage account name we'll change that to var dot stg act name now we have three variables for our project using variables is an important part of writing good portable code as you saw we start an input variable block with the word variable then with the name of the variable the name is used to reference the value of the variable in the code next we specify the type of value the variable should be the options are a string a number or a boolean value this is optional but you should use it because it offers some input validation the number one is interpreted differently if it's a integer or a string for example next we can add a description for the variable and the default value both of those are optional we can add validation blocks to verify the accuracy of the value provided for the variable there's also a sensitive option this suppresses the value from the command line output use this if there's any passwords or secrets applied for a variable we can add the variables to the same file as the resource we built but the more common approach is to keep variables in a separate terraform file just for variables remember terraform treats all the dot tf files in the directory as one file so splitting them out benefits us to make the code more readable let's create a new variable file next called variable.tf here we are back in vs code on the same project let's create a new file in the directory called variables.tf now we'll cut the variables we set in the main.tf and paste them into the variables.tf if you'd like you can add comments to a terraform file by starting with a hash symbol everything from that hash symbol to the end of the line will be a comment be sure to save the files notice there's a black circle next to the file names at the top we hit save those black circles go away be sure to save these files as we're making changes we now have a variable file for our project once we have the variables we can modify the default value and deploy with those values if we add a default value that's used unless something else is specified what if we don't want to use the default value we could update that default value in the variable file but that's not how variable files are supposed to work besides the default value is optional what if there's none specified we can specify variables by passing them in when we run the terraform planet or terraform apply command let's see how to specify variables at the command line here we are back in vs code let's run a terraform plan and we'll change the variable when we run the command by the way you can go to view terminal to show the terminal it also shows the shortcut to display and hide the terminal first make sure those files are saved and type terraform init next we'll run terraform plan with the dash var argument var equals and here we add a double quote we'll type in the name of the variable storage account name or sdg act name equals and then we'll provide the name of the storage account and this has to be globally unique to make sure i don't run into any errors i'm going to throw in some random numbers and we'll finish with a double quote keep in mind that not only does a storage account have to be globally unique it's also limited with the characters we can use lowercase letters numbers and a very limited number of special characters let's run this command it'll take a few seconds to finish let's look at the output if we go up to the storage account at the very beginning of the storage account output we have the name and that's the name we specified when we passed in the variable through the command that's how to specify a variable from the command line adding the var argument at the command line works but it would be cumbersome for large deployments instead we'll use a terraform.tf vars file that will override the default values with updated values once we're done with the example coming up we'll have three files the main.tf that has the resources we'll deploy the variables.tf that has the variables used for the deployment in the terraform.tf vars file with the settings for the variables with this configuration we can deploy entirely new environments by simply updating the terraform.tfrs file something to note about the terraform.tfrs file the name has to be terraform.tsvars alternatively give it the file extension.auto.tfrs and you can give it any name you want you can also use a combination of the three a terraforms.tfrs file an auto.tfrs file and the command line argument that's the order of precedence when using multiple files let's create a terraform.tfrs file next back at vs code let's create a new file called terraform.tf vars in the root module or the working directory we'll create new values for all of the three variables we set up but i need to see those variables vs code has a helpful feature that allows us to see multiple files at once click on the two rectangles in the top right we'll set the first window to variables.tf now we have our variables file in one pane and terraform.tfvars in the other next we can type or copy and paste the variable name without the quotes into the tf vars file now after each one we'll add an equal sign and the new value make sure to match the value type these are all strings so there's quotes around them integers and boolean values don't use quotes so i'll set the location to central us the resource group name is terraform stgrg and the storage account name will make sure it's globally unique you can populate with whatever values you want just make sure that that storage account is lowercase letters and numbers only and give it a name that's going to be unique now we can click save make sure all the files are saved and we can close terraform.tf vars in the right pane there it is open up the terminal if it's not already open and we'll run terraform init and then we'll run terraform plan and we'll just run plan at this stage to verify that these variables have been picked up we don't need to apply it yet let's verify the variables applied let's go up to the top where the resource group is and the resource group name matches what we added to the terraform.tf-vars as well as the location if we go to the storage account name that matches as well that is how to create a terraforms.tsvar file let's review one more type of variable before we finish a local value or locals locals are used to assign a name to an expression so it can be used multiple times in the code just like variables updating a local will update any instance in the code where the local is used so why do we have two options variables are used for values that change frequently during deployments the best example for when to use a variable is storage account names that has to be different each deployment because the storage account name has to be globally unique locals on the other hand are for redundant settings in the code items that may need to be changed but not as frequently as variables for example maybe if we had to enter a company name in the code we wouldn't need to change that for each deployment at that same company but if that code was moved to different organizations all instances of that company's name in the code could be updated by changing the value of the local locals help with repeating values in a configuration file but used too much can make the code difficult to read use local sparingly only when it makes sense to limit repeating values that may change let's create a local settings for our main.tf file to set tags next here we are back at vs code tags may or may not be a good use case for locals but it makes for a good example let's open up the main.tf file we'll add the locals block underneath the provider block start by typing in locals we can tab to autocomplete now if we're using a single name value pair we could enter a name and value like this we can also group multiple related locals into one local block so we could next add the tags go to the end and hit enter enter value called tags followed by an equal sign and here is where it changes a little bit now enter in another set of opening and closing squiggly brackets notice how the bracket pair colorizer extension is color coding the bracket so it's easy to see the matching pair now we'll enter two tag values for environment and owner we'll set environment to lab and owner to your name the tags local is now a list of values we don't need the local name that was just added as an example so let's delete that and we'll just leave tags now let's go down to the storage account tags are set for the staging environment this is how to enter in static values for tags we'll use the locals instead remove the brackets after tags and everything between them and add local dot tags let's add tags to the resource group as well under location we'll add a new line add tags equal local dot tags let's save the file and test our work we'll run terraform in it make sure to always run that after making a change to the files then we'll run terraform plan let's verify the storage account and resource group have the tags we expect there's the resource group and we can see the tags environment and owner if we scroll down to storage account there's tags just as we expected that looks good let's run terraform apply and add the resource group and storage account enter yes if it all looks good this will take a couple minutes to finish i'll pause here and come back once it's done that finished and no red that looks good let's go to the portal and verify everything worked here we are in the portal and we have our resource group terraform stgrg our tags have been added to the resource group as well let's check our storage account there it is we have a storage account with the name we added to the terraforms.tf vars file and here again our tags are added that looks good now you can keep this if you want but if you want to get rid of it let's go back to vs code and we'll run terraform destroy this will remove everything with the deployment verify you want it removed i'll pause here and come back once it's done that finished our resource group is gone now that is how to use a variable file a tfvrs file and locals in terraform i hope this helps you better understand how to use variables and locals in terraform please don't forget to like and subscribe and thanks for watching
Info
Channel: Travis Roberts
Views: 11,832
Rating: undefined out of 5
Keywords: Terraform.tfvars, variables, terraform vairalbes, terraform var, Azure, Azure CLI, IaC Terraform, Terraform Registry, VSCode, Terraform for Azure, registry, .tf, HashiCorp, workdlow, Terraform Resouce, .terraform, Terraform Write, Terraform Plan, Terraform Apply, Terraform Destroy, main.tf, terraform tutorial, terraform training, DevOps
Id: UETRaGjpJoQ
Channel Id: undefined
Length: 21min 51sec (1311 seconds)
Published: Sun Dec 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.