4. Terraform | How to use Variables and Output in terraform | terraform file with variables, output

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is the terraform file which we have created in our previous lecture that is the VPC underscore ec2.tf over here we are providing some of the information like region that is AP South 1 and Ami this is the Ami keeper this is the one instance type and subnets and vpcc IDR and cadr block for the subnet and availability zone for the subnet and many more if you see these values these values are mostly hard coded how we can avoid providing hard-coded values in our terraform scripts hey guys welcome to Galaxy Technologies my name is Ravi Shankar in this video we are going to see how to use variables and outputs in terraform initially we will discuss about variables then we are going to see how we can use the output in the terraform let's go back so this is the terraform script which is already available in my local system so to use variables we need to create a file called variable.tf that is the requirement for that let me open the es code and let me open our folder file open folder and it is under wellaxi terraform right so terraform okay select this folder and currently we have only VPC underscore ec2 which I have committed into our repository and also dot kit ignore okay anyway so we are going to talk about this one so my requirement is I don't want to give the hard coded values over here that is where I can create a new file that is variables.ef okay so this is the file name I can mention and I need to specify the variable name and what is the value so I'm going to name it as a variable okay and variable name variables require one value that is label so here I can able to give that region so I'm going to just name it as a region over here I am going to mention it as a default value of region that is AP South one okay so this is the region and uh if I go over here so this is the file instead of over here I can just give the where dot region okay where dot region nothing but it looks for the variable file and in the variable file it goes for the region so after that whatever you have provided same thing you need to Define over here let's take that if if I give it as a location same thing I need to provide it over here where dot location anyway it is auto propagating because of our plugins okay so you can use anything over here next thing is I want to change the Ami ID also as a variable then I can go over here so variable and I just name it as a Ami ID or OS name for just better understanding I am giving the naming conventions like this so default equal to I can give the value of this one okay and over there I can just copy this variable and I can specify the variable name over here that is where dot OS name okay similar way instance type also I don't want to provide it as a hard coded so I will just give the new variable that is variable and instance type okay it can be anything but I am giving instance type and the default value equal to T2 small and next thing even CID are also I can change before that I will change the where dot instance type and similar way key name also I would like to change so where Dot key I can give and I can go over here and specify the okay it can be in any sequence but I am going to name it as a where key right we have given as a key so default equal to RTP minus 0 3 okay this is the key right so that is how you can specify so where dot key and where dot key okay key value has been specified so let me remove the Extra Spaces so yeah so these are the variables and also I would like to change the cadr block as well so this is the one I am going to take it variable bpc cidr default value equal to the cidr which we would like to use it even I can give the variable name as a subnet cider okay subnet one dot cadr because later we are going to use the subnet to default value can be something like this okay 10.1.24 I can give okay so these are the variables I am going to use and I will just replace this one with the cider and don't give underscore maybe it will be giving some confusion so give hyphen terraform default modules also have a similar naming convention so let me replace it for better understanding so go here instance type is equal to where dot instance type and the next thing is this is the one right where Dot vpcc ADR and similar way over here where dot subnet1.cadia minus cidr okay even availability zone for this one also I am going to change it so I will name it as a variable subnet aged and default equal to this one and let me go over here where dot subnet h8 okay so we have given almost all the values which are required and we have just changed it to the hard coded value to our variables file so let me save this file and what we have done is we just kept extra variable Dot efl and all variables we have specified over here later point of time if I want to change something for this file then I just need to update the variable.tfl so that it will change maybe our key name our instance type rcidr whatever you wish to change you can just change the variable.tf file that is the advantage of this one so let me open the new terminal and uh let's try to execute it so terraform init so we are initializing it then terraform validate it is also successful and it is a continuation of our previous video so you just follow the previous ones to understand better and if you have any challenges please let me know under this code has been updated in our GitHub repository and URLs has been shared in the description of this video now we'll plan for it so terraform plan it is going to create seven resources as part of this requirement now terraform upload and before that one I will just show you my AWS console there is no running instances at this moment so all are terminated even I forget to show you the VPC so this is the VPC and so for only one and it is going to create the second one okay now let's go back and give yes to execute this one so yes so now it is actually going to create our resources and you can see the changes over here so it is creating a new VPC that is 10.10 series and once it is created you can see the new ec2 instance as well let's wait until it get created all right it has been completed and let me go and check it out for the new system yes this is the new system and it is the series of 10.10.1.61 which means that in our subnet it has been created so this is how you can use the variables so that it will be very handy where to change and what to change next thing is we want to get this information as a output file nothing but we have created a VPC and also ec2 instance how it will be if I retrieve the IP address of my ec2 instance and how it will be if I get my VP VPC ID so those kind of things you can retrieve with the help of output okay so we'll add one more file called output over here so let's create a new file that is output dot TF to get the output of our public and private IP addresses of our instance we need to define the output file output file expects a label so I will name it as a public IP of demo server okay something like this I will give and you need to Define even description you can use so description so this is the public IP so how do I know all these things it is just following the documentation or else you can ask our friend that is chart GPT as well and to get the value so you can give the value and here you need to Define how to get the public IP of our instance for that let's go and ask our chart GPT because we just want to incorporate the artificial intelligence in our learning process that is the overall intention so I have just opened my chart GPT and will look for how to get public and private eyepiece of ec2 using terraform output output file so I'm just searching for this one and it gives the syntax what we needed okay that is the advantage of asking chart GPT so this is the output file it is saying that okay value is equal to AWS instance example dot public IP and private IP okay so however we got the syntax which we want to get it so let me take this one and I will just give it over here anyway value I have given and AWS instance this example we need to get the resource type of our ec2 instance so to make you understand let me go back to your VPC with ec2 file and here you can see resource this is the resource type and resource name so this is the one we need to give demo server okay just to take this one and give it as a demo server dot public IP we are retrieving the demo server the dot public IP even we can get the private IP also similar way that is private and even and I can change it as a private IP okay this is how you can Define and let me save this file and let's try to execute our script once again now it is not going to create our instance but whatever it is created that instance output it should able to retrieve so let me try to apply terraform apply you can see here private iPad is retrieving let's give s it should also retrieve the public API let's see okay there is some mistake in the public IP so we got it only private IP because we didn't allocate any public IP you can go and check it out over here so this is the ec2 instance and if I go and refresh it there is no public IP now we'll try to allocate a public IP so we'll ask our chart GPT again how to allocate a public IP so I'm just searching for how to allocate your public IP to an ec2 instance while launching in terraform file so let's see so there is option associate public IP address so that is what we need to use under AWS instance which helps us to allocate a public IP let me take this one let me take this one and go back to our script and go to ec2 and over here let me give this one let me give it as a associate public IP let's save this one and again apply for this yes to apply so now we could see it is going to allocate a public IP to our ec2 instance and it will display in the outputs by the way it is destroying the existing ec2 instance and creating the new one alright you can see the public and private IP addresses because we have allocated the public IP all right that's all for this video before that I will commit these files to our remote repository so git add Dot so again git status now it is trying to add this dot terraform file as well which we don't want to add as part of this one okay anyway I will just ignore this one and commit the code into our repository this repository URL I am going to share it in the description of this video thank you and see you in the next video where we are going to create the kubernetes cluster by using the terraform see you in that interesting video
Info
Channel: Valaxy Technologies
Views: 6,144
Rating: undefined out of 5
Keywords: aws, devops, valaxy technologies, security, automation, serverless, linux, valaxy, valaxy devops, devops online training, devops realtime training, valaxy devops training, valaxy devops videos, terraform, terraform for aws, terraform variables, terraform output, terraform variables and outputs, terraform file to create ec2 instance with variables and outputs, terraform beginners training, terraform online training, terraform to setup aws ec2 instance, writing terraform manifest file
Id: szSKKSiCU2A
Channel Id: undefined
Length: 13min 38sec (818 seconds)
Published: Mon Mar 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.