13 Terraform SE03 | Terraform environment variables | Example of terraform environment variables

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
everyone welcome to the channel where we are discussing the terraform in detail in our previous episodes we have discussed about setting up the values in your terraform configurations we have discussed how to use the terraform dot tf ware file which is a reserve file we have also discussed about environment specific terraform reserve terraform tfl files and then we have also discussed that discuss about the auto.tf where file which is a reserved suffix for setting up the values in this episode we will discuss about the environment variables with the terraform configuration we will see how to set the environment variables in terraform for your terraform variable configuration and why do you need that first of all so let's say we have a requirement wherein your terraform configuration contains some of the sensitive information as invariable for example the service principle username and password as a variable now if you pass those variables into a terraform dot tf4 files for example here if i pass the client id and password values here in this particular file as in plain text then i'll be compromising some some of the most critical information as this is principle details in a plain text because this file is going to be checked into my source control repository and it will be available to all my developers and that is something i do not want in that case the other option we have is to pass the value of those variables as in command line so we have two option to pass the value of the variables as in command line is one is to pass it through the terraform plan file plan command so in the terraform plan command we can pass the variable values like we were passing the variable file name or file path similarly we can find the individual variable values as well and the other option we have is the pass the value or the set the value as an environment variable and then terraform will automatically detects the value of those variables from the environment variable and then it will provide the rx is the value from the environment variable i'll show you both the ways in this demonstration so let's say i have a variable called the client id and client secret these are the two variables which is which are basically uh which which are going to hold the sensitive information on my terraform configuration i don't want to expose this as in the terraform variable files so to pass the variable values what i can do is i'll just first follow open the terminal on the visual studio code like this and if i run the terraform plan then i do have option as in where dashware and then you can pass the name of your variable for example client id and then i can pass the client id value environment env user details something like that some some value of my client id similarly in the next variable i can pass it as where then client secret [Music] and then i can pass that particular secret value pass at the rate xyz some passwords now if i run the terraform plan as is by providing the value this will take the value and use it in my terraform configuration this is where i'm just returning the value whatever i have this variable holds i'm just printing out just for the demonstration of course in the actual users you are not going to display the values in the plaintext but they just to show you demonstration that we are able to access those values which we have passed from the command line we are able to use it this is the first way to pass those values good right now the other way we have is to set as an environment variable in case if you don't want to pass it from as in command line so how do we do that in the terraform is in the terraform configuration you have a reserve keyword called the tf underscore where underscore and then you can use this prefix to inside on your variable name for example if i have a variable name as username then i can create a environment variable with tf underscore where underscore user underscore name and then i can set the that particular variable as in environment variable by running this particular command on my windows if i'm using a windows so i can use the set dash item and then the path of the item which is the name of your environment variable using the e and v colon and then your environment variable name and the value of that environment variable if you are using linux or mac then you can simply use the export command to set the value of your environment variables as i am right now using the windows so i'll be using this particular command for the demonstration run the user id just to retrieve the value just to validate whether the value is set or not i can simply run this variable with as any environment and as you can see that we are returning the value or we we are receiving the value as user at me which which is what we have set next variable i'll set again as the environment variable again i will try to validate this just to read it in case if you want to validate i'm getting the values now now finally we are ready to run the terraform plan command so again this this time if if you see i'm just running the terraform plan command i'm not providing any values as in dash variable file or dashware or even those those values are not being defined here in the in terraform dot variable file so there is no other way apart from the environment variables files so let's see what values the form takes in this case and what it does what it displays as you can see that terraform is intelligent enough to read the value of those variables as in environment variable what we have set up in our windows platform so from the operating system where this program is running it is taking the environment variable and this is really really helpful or this is really really useful in case if you would like to pass some of these critical values and don't want to save those values in your terraform configurations i hope you have found this useful i'll be saving this demonstration on my github repository in case if you want to refer it where i have all the documentations thanks for watching it see in the next video
Info
Channel: Our Cloud School
Views: 5,878
Rating: undefined out of 5
Keywords: Terraform environment variables
Id: BeWIVXqCrH0
Channel Id: undefined
Length: 8min 0sec (480 seconds)
Published: Thu Sep 01 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.