You should be using tfvars vs variables in Terraform

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi this is Kevin from let me take you in today's video I'm gonna be going to be going over uh why you should be using the tfvrs file versus just variables in terraform now this isn't to say that the variables file isn't useful because it is it's just that as you start to grow and you start to use terraforming specific Parts your environment as you start to decouple your code to be more usable across different environments you don't want to have to then change out a lot of different values based off of the environment that you're running the code in so with that inside your variables file you can typically have like things like the instant size but let's say in your Dev environment it might be like if you're using AWS like a t2.micro but in your production environment you might need a two two dollar large so you don't want to have to keep changing those values up so in this video I'm going to go over something really simple it's just changing a um address prefix for a subnet so typically you could put it in here you know then let it run but then if you need to put it in another environment or change it and you have to come in here change this but then you know the other parts of the code and stuff might not you know match up with what what was being referenced so what we're going to do and that's why you kind of want to keep a lot of stuff variabilized so that it's not too static to the environment you're running in so instead of running this here what I'm going to do is I'm going to go into this file I'm going to create a new file called variables.tf and inside of here we're going to call it address prefix and to do that and this is just the way you can initially do it using a variable so it's going to be called address underscore prefix we can name it whatever you want and then you know you always want to put a description so description so subnet prefix for the v-net and then inside that we're just going to make this is going to be a type list and then default is going to be 10.0.1.0 24. all right so we can save that so now we can go back to our code get rid of this static value and we're just going to do a VAR so that's variable Dot address prefix and save that now if we run this we're going to go ahead and you know get what's going to be added and changed so we do a terraform plan and uh default value for a variable all right that's what do I put wrong here this needs to be tight um strain save that and then now when this runs this is going to actually be able to see that we have the variable it's being um referenced it should show that we have a couple of things to be added so as you can see four to be added so now if we look up in here it shouldn't show so 10.0.1.0 so as you can see in our variables we have that in here but it's not being referenced in here so but you know let's say you need to then you want to use the same code in another environment well that other environment you may not want a 10.0.1.0 in there so what you would have to do would be to come in change this save it run it come in change this so you know so on and so forth but what you can do is we'll create another folder and we'll call this let's say this is Dev it's going to be dev.tf bars and that's terraform variables so what we'll do is we'll call we'll put a address prefix value in here and we'll call this 10.0.1.0 24. now but what we can do is what I'm going to do is I'm going to put in 10.2.0 here I'm going to run another terraform plan and what's going to happen is you're going to see that it's not going to use the tfvrs file it's actually going to still use the variables file and I'll show you the reason why here in a second so when you're passing values through to your um so if we scroll up we're going to still see the 10.2.0 if you want to so and the reason being is unless it's unless it says like terraform.tfrs or Dev dot Auto you need to have the auto in there if you have anything that's not specified as actually terraform.tfrs in the file name so it could be you know whatever auto.tfrs or whatever auto.tfr.json so because you can essentially have another file that can be prod.tfrs and then another one that's you know stage Gene Dot tfrs and in those could be different environment variables in terraform is not going to know which one to automatically use so what we're going to do is let's go ahead and get rid of these [Music] um so let's get rid of the staging we're not using that one all right now if we go back in here we can rename this and we'll do a Dev dot Auto Dot tfrs and now if we do a plan we should get the 10.0.1.0 as the subnet prefix here that shows up and and so with that once that comes in here look and see what other okay so we scroll up now you see we have the 10.0.1.0 24. so now this is going to be utilized and not the main.tf or the variables that TFT as the default value so and I mean if you don't have a value in there um one thing it will do is so like let's say you don't have a tfrs but you do have a variables file uh if you don't have a value in there it's going to make you put one in in the command line when you uh run your terraform apply so that's just a simple kind of overview of why the tfrs file can be more useful versus just using variables because then like I said you can kind of utilize your code in different environments without having to not necessarily it's not necessarily hard-coded in the variables file but it's more work for you if you need to utilize this same module in another environment because now you have to go through and change out all the um values that's in the variables file to match the environment that you're running it in so again that said if you have any questions in regards to the actual types of variables and you know um things that you can kind of apply in your TFR file drop me a comment down below I'll be sure you know um you know help where I can as far as setting up things like that and how they how they should be ran but again you know check out the site letmetechu.com for some other tips and tricks using terraform and also some of the other videos on the page here so go ahead and like And subscribe and thanks for tuning in hope to see you next time
Info
Channel: LetMeTechYou
Views: 4,856
Rating: undefined out of 5
Keywords: terraform, hashicorp, devops
Id: BHWM4D2GJvI
Channel Id: undefined
Length: 9min 12sec (552 seconds)
Published: Wed Oct 12 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.