Learn how to use Terraform variables

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back uh today we're going to take a look at terraform variables and specifically how to pass in a variable file what you can do is set multiple variables as values inside a single file and then pass that using the cli with terraform to actually set those now variables are useful when you create your resources because you can then reuse the terraform files across multiple environments and during the course of the series we are actually going to build out our infrastructure to span multiple aws accounts we're going to start off with a single account and then move and rename it to a dev account then create a testing or staging account and ultimately production and at the end when all of this is done we will have a single directory with some terraform files defining our infrastructure and then another directory with three individual variable files with the values for those different environments so they'll all look exactly the same except the values will be different so let's jump into the console so i can show you how to set up these variables inside terraform so what we have here is our cloud9 environment from before and i've just gone and run the terraform plan and apply to make sure that our infrastructure is in the same state as it was the last time we ran it so the first thing we want to do is we want to create a new file to define our variables in and i haven't covered this before but with terraform you can actually have as many tier files in the folder as you want terraform will actually take all of those concatenate them into one large data set and then run the define the dependency graph based on what you have inside those files so you can move things between different files without having terraform be affected by it so for example a good practice is to extract your providers into a specific provider file so let's call create one called providers like that dot tf and what i'm going to do is i'm going to cut this out of that example file i'm going to put it inside the provider file over here and then hit the save button cool so now what i can do is i can go ahead and say terraform plan and there shouldn't be any differences so it'll take a few seconds so let's just wait for that to run and here we go boom no changes awesome that is what we want to see so now let's go ahead and create that variables file and get it and start using it so variables inside terraform what we want to do is if i go to my example file over here we can see that down here in my ec2 instance i have the size hard-coded as well as the name for the instance so let's take a look at this um this instance type first so i'm going to grab this string over here cut it out and then we go to our variables file and create a variable for this called variable and we're going to call it ec2 instance sorry instance type like that and then curly braces like that now you can leave it as is and it defaults to a string type but i personally prefer to always be very explicit with the data types that i'm dealing with so we go ahead and define it as string now what i can do inside this example file over here is i can go var dot ec2 instance type like that now if i don't give it a value and i run the plan section so terraform plan what i'll do is it'll actually ask me for that value so i can go ahead and say t3 dot let's make it actually a t3 dot medium just to show you the difference and it'll now go ahead and calculate the plan based on this value that i provided it so if we scroll up you can see there's one to change and we can see over here that it wants to change the instance type from a t3 micro to t3 medium based on that input so this is useful but what if we want to give it a default that's easy as well so what we do over here is you literally just say default equals that value which means that if i don't specify a value via a parameter flag with a command line it'll assume this is the value that i want to use so let me show you quickly again uh terraform plan and it'll just go ahead and calculate the plan and we'll see that there aren't any changes because the instance is already a t2 micro cool so let's quickly just do that with that name that we want to set as well go here grab this name and we're going to call it variable.ec2 instance name head over to variables all ec2 instance name type is also a string and then default equals that cool let's just run a plan once more just to make sure that we didn't have any typos or any mistakes and we are good to go cool okay so this is all very fine and well but now how do i actually set these values or override them with the cli well you do the following so let's say i want to use do that t3 mic um medium again i would go terraform apply and then i just specify the flag for variable and i set this equal to and this is a string quoted key value pair so i say ec2 instance type like that and i set it equal to t3.medium like that and now we just run it and now what will happen is it'll use that value for that variable that i have set inside my terraform files to actually calculate and if we have a look here we can see that it wants to change it to a from a t3 micro to a t3 medium so that's how you would specify this now you can imagine specifying a long list of variables like this gets very very cumbersome so let's create a variable file for that now what you can do for that is to just create a file with the tf vars extension so let's go ahead and say let's just call it tfvars.uh like that and what i'm going to do in here is i'm actually going to grab these and and set the values as key value pairs so let's go in here and then also over here grab this name set these values and these we set as follow we're going to use the graph sorry i'm going to grab these defaults as well just to change them and set them to medium again like that and variables let's just create this name and i'm going to i'm going to remove this default just to show you that you can both override as well as provide a value not if the specifier on the command line cool so we've got those now if i go terraform plan just as is it should still ask me for those variables because it doesn't know about this variable file so how do i tell it well another parameter flag so we go var file and we just and we say dev dot tf vars like that and now what you'll see is that terraform will actually use those values and apply them so let's have a look once again we can see that the name is the value that we have there in this file and the type is once again being changed from a t2 micro to a t2 media now this is useful because what you can do then is as follows you can have a file let's actually do that quickly testing dot tf vars touch prod dot tf vars and let's say you've got a variable called a variable called number uh of instances like that i give it a value type is equal to i think it's int uh we'll just double check now if it's happy with that uh terraform plan okay it doesn't like int integer integer say that i think it's integer or let me quickly go find out ah sorry my mistake it is number so we just say number like that and now we can go ahead and plan and now what it'll do for example is it'll ask us how many instances do we want um so now when we look at our different files so let's say dev like this and we go uh this value set that over there and for div we only want one now what we want to do is for production um over here we let's say we want a large instance or even let's make it a c5 large because we want sustained throughput on our production service and we want five of these so now if i want to create my infrastructure for production versus dip what i would do is it'll be a case of terraform plan with var file equals to dev dot tf vars like that versus um the same except it'll be the pride one so all we do is that broad dot tf vars and those will actually then pass different values to terraform so this is how you get to that point where you've got infrastructure parity between your different environments because you can pass these files with the different values in them between those ones and that is it for variables for today i hope you enjoyed this video if you did please hit the like and the subscribe button the like button helps others discover this video and the subscribe button will help you be notified about future videos so i plan to be releasing quite a few in the next couple of weeks all focused on terraform and we're going to be building towards that multi-aws account setup that i spoke about earlier i'm also going to be creating a make file that i use to wrap a lot of the terraform commands to make it very very simple to actually just jump between the different environments so once set up there's very little that you have to keep track of when you want to jump between the environments so stick around for that you can see the link to the next video inside the screen in the next few seconds until we see you next time bye [Music] [Applause] so [Music] you
Info
Channel: Cobus Bernard
Views: 24,005
Rating: undefined out of 5
Keywords: terraform architecture, getting started on aws, infrastructure as code, terraform, guide, cloud computing, beginners guide, terraform variables, amazon ec2, ec2, Hashicorp, devops terraform, Infrastructure as code, tutorial, getting started, devops, terraform explained, aws, aws ec2, what is terraform, automation, how terraform works
Id: oArutYYvQ_Y
Channel Id: undefined
Length: 9min 39sec (579 seconds)
Published: Tue Aug 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.