HashiCorp Terraform Associate Certification Course (003) - Full Course | Pass Exam

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there myself Rahul and welcome to this terraform Series this series is gonna be a little longer series because I have divided this whole series into multiple chapters and you will see each chapter name onto the screen so take a pause over here and go through the topics so that you know like what you will be getting out of this particular series and also for your help I have put down the timestamp of each chapter into the description section so if you are already familiar with some of the topics then feel free to skip those particular chapter but if you are a beginner and you want to learn terraform then I would highly recommend to start from chapter one and go all the way till all end of the chapter so that you know the terraform and how it works and how you can just use those Concepts into your actual day to day work so without further Ado let's start with our chapter number one and these are the topics which I have planned for today's session starting from what is terraform then what are terraform commands that is terraform init terraform plan terraform apply and the third we are gonna create our first terraform file and the fourth one is how we are going to install terraform on our system after that we have a couple of demo the first demo is setting up virtual machine on actual Google Cloud platform using terraform and the second demo is setting up an ec2 instance on AWS and the same using terraform this is going to be a long session so if you want to skip some of the topics then that is also fine I'll put the timestamp of each topic into the description section so just click on those timestamp to follow those individual sessions and if you are a beginner and you want to learn terraform then I would recommend to follow this complete session because this is going to be a very basic session where we will be starting everything from the scratch what is terraform terraform is used to provision and set up your infrastructure on a cloud platform the next question comes what is provisioning and setting up of your infrastructure suppose you have a Google Cloud platform account or AWS account and that's the only thing you have so you need to set up your servers or the virtual machine so that you can deploy your application so terraform will help you to create those infrastructure using terraform file so you don't have to manually go into Google cloud or AWS console and manually create those virtual machines and install your software packages once you know terraform then you are going to create a multiple terraform file which will help you to set up your complete infrastructure from the zero that and what is terraform file this is the example of a very simple terraform file where you will have two component one is provider and second one is resource and always remember the terraform file always have an extension of dot TF so if you look carefully over here inside the file we have a specified provider as Google so if you specify provider as Google then you are definitely going to interact with a Google Cloud platform and in case if you are going to mention here AWS then you are going to interact with the AWS cloud services the second component which I have mentioned over here is the resource so resource varies from Google Cloud platforms to AWS because if you are provisioning a virtual machine on a Google Cloud platform then we are going to call that Resource as a Google compute instance as you can see in this terraform file this terraform file is a sample terraform file for a Google Cloud platform but in case if you are working on a AWS so instead of Google compute instance you are going to mention ec2 instance this is just a little idea how to create your terraform file and how you are going to interact with the cloud service provider but the terraform file is not self-sufficient enough to interact with a cloud service provider you need to know some terraform commands then what are those commands are the first command is terraform in it which is going to prepare your local environment for setting up your Cloud infrastructure second terraform plan it is going to tell you like how many resources you are going to add update or delete the third command is terraform apply this command is actually going to run your virtual instances on a Google Cloud platform or AWS and the final command is terraform destroy so this command is something which is going to destroy or delete all the instances or the resources which you have created using previous terraform apply command don't worry we are going to cover all these commands in more detail into the next slides starting with our first command that is terraform in it suppose you have a directly terraform demo on your local system or a desktop and inside that directory you have a file that is main.terraform and this is the code snippet where we have specified the provider and the resource don't worry we are gonna look the complete actual terraform file into the letter session but this is just an example where we have specified the provider that is Google and the resource that is Google compute instance now after that once you run the First Command that is terraform init command then it is going to contact the cloud service provider and it is going to download all the required dependency onto your local system so what does it mean so once you run this command then you will see you're the same directory you will get a directory called dot terraform so in that directory it is going to download the required packages which is needed for interacting with your cloud service provider so since this example is for Google Cloud platform so it has downloaded terraform provider Google version 3.61. so here you can see this is the dot terraform file which which is just got created once you issue the terraform init command so if we summarize what is terraform init command is then it is just preparing your local environment by downloading all the required dependencies based on the provider which you specify since we have specified the provider as a Google so it has downloaded all the required dependencies for Google Cloud platform and in case if I have specified over here AWS then it is going to download all the required dependencies which is needed for communicating with the AWS environment so this is how the terraform init works all right the next command which we are going to look is terraform plan so again we are going to take the same example we have created a terraform directory and this is our main dot terraform file where we have specified the provider as Google and the resource as Google compute instance and after that we are going to run the terraform plan command once you run the terraform plan command then you will see following message at the end of your terminal and this message is going to tell how many resources you have added how many resources you have changed or updated and how many resources you have destroyed this command is not still not responsible for creating any resource on a Google Google Cloud platform or AWS platform it is just going to inform you like what is going to happen or what is going to change or what is going to destroy so here our main dot terraform if you look it again then we are trying to add a resource that is Google compute instance that's why we have got a message one to add that means we are going to add one new resource to the Google Cloud platform to summarize the good terraform plant command then it is just going to inform you what is going to add what is going to update and what is going to destroy on a Google Cloud platform or AWS platform it is not going to update or change anything on actual Cloud platform it is just is going to inform you that's why the name is plan moving to the next command the next command is terraform apply and I'm just gonna tell you this is where everything will happen or the actual thing will happen so again we are I'm taking the same example I have created a directory terraform demo inside the terraform demo I have a main. terraform file and again there is a provider there is a resource for Google Cloud platform and I am going to run the command terraform apply and once you run the command if you are running this command for Google since this example we have taken it for Google Cloud platform then it is going to start a instance that is a virtual machine on a Google Cloud platform and suppose if you have taken the same example or we have updated the same main. terraform file for AWS then we are going to get an ec2 instance on AWS platform don't worry we have a separate complete lab session for AWS and Google Cloud platform where where we are going to create the complete terraform file and we are going to spin those virtual machine and easy to instances on those platform now you have got an idea like how the terraform command works and what are the sequence of running those terraform command now in this slide we are gonna see our complete terraform file this is an actual code of our main.terraform file uh this code sample or the this complete main. terraform file I have taken it for Google Cloud platform but this file structure or the components which I am going to explain is going to be the pretty much same for AWS instance also okay let's start with the first component so the first component is provider since we are talking about Google Cloud platforms that's why I have mentioned the provider name over here is Google second if you are going to interact with a Google Cloud platform or AWS then you need to have some kind of authentication so you need to create this uh Json key that is your authentication key on a Google Cloud platform first and then you need to download the Json key onto your local system and you need to pass that local that Json key to this terraform file the third component which we are talking about here is the project ID so if you go to Google Cloud platform then you need to select project over here so here I have already created a project called gcp terraform and this is the project ID for it so we need to copy this project ID and paste it over here inside our main.terraform file so that this terraform knows like in which project we are going to create those resources moving further the fourth component we are talking about here is the reason and the Zone this settings is pretty much dependent on in which part of the world you are so I am currently in the Europe zone so that's why I have selected the Europe first reason but if you are in some other part of the world then select the nearest Zone to you now we are done with our provider section the next section we are going to talk we are talking about Google compute instance so the Google compute instance is a resource that means we are going to provision a virtual machine on a Google Cloud platform that's why we have specified the resource name as a Google compute installs but in case if you are working on Adi list then you need to specify the name as AWS underscores in storms okay the second keyword over here it I have kept it as a default so this default is a user defined name actually and you can keep any name of your choice all right then there is a next component sorry I have pasted I have clicked it but the next component over here is the name that is tag that is also a user defined name so any resource which you are trying to create you can put a suitable name over there all right now moving to the sixth component that is machine type what is machine type so machine type is something like a CPU so what kind of a CPU or what kind of a powerful CPU you need since this is an example or demo that's why I have keep I have kept actually very small CPU that is a micro CPU but in case if you are looking for a production environment then just go for some bigger uh machine type that is E2 small medium but still E2 small and medium are just only for development and demo purpose just look out for documentation and pick some suitable CPU if you are looking for a production environment the next component which you are talking about is uh the operating system since we are starting or we are setting up a virtual machine it needs to have an operating system so what kind of operating system you want to install in it so this is the tag boot disk and initialize problems here we are going to specify our Debian cloud or Debian 9 system so we want to have a Debian operating system installed on our virtual machine the next component which we are talking about is the network interface and if you know if you are working on a cloud environment then you need to have an public IP which so that you can access your virtual machine so you need to define a network interface for that so this is the code snippet which you need to keep inside your main.terraform file and if you don't want to control the IP address then you just Define that Network as default and Google Cloud platform it's going to assign a public IP address for your virtual machine in the previous slide we have seen the complete terraform file for a Google Cloud platform so now in this slide we are going to compare the two terraform file one for Google Cloud platform and on one for AWS so here on the left hand side these are the code snippet from our Google Cloud terraform file so which is not I'm not going to explain because we have seen in the previous slide but on the right hand side we are pretty much interested in AWS so as I told you we need to specify the provider so here we are going to specify the provider as AWS then again we are going to specify the region based on your location in the world second uh this access key and secret key this part is little different here in Google we need to download that Json file which combine combines of your authentication but here we need to specify the access key along with the secret key so that you will get from a AWS console don't worry we have a demo so I'm gonna show you like where to copy the access key and the secret key the next we need to specify the resource so in Google Cloud example we have seen the resource name as Google compute instance but here we need to specify that instance name as AWS instance terms because we are going to provision this instance on AWS environment then you need to give some suitable name so I have given the name as ec2 example the next component inside the resource is Ami so you need to find the Ami ID from your Amazon console or AWS console actually I'll show that uh ID but to give you a hint what is Ami Ami is a machine ID because in Google Cloud platform you need to specify operating system like a Debian tan but here you need to choose the predefined images so Amazon provides you a list of images one for in that list you can find one for Ubuntu right ahead sentos or Fedora so you can choose any of the machine ID out of that list and put that ID over here so that it can provision those machines for you and is instance type here and that again denotes the CPU power so if you are looking for a demo purpose then you can choose T2 micro or something similar and if you are looking for production grid CPUs then just look for the documentation and choose some very high processing power CPUs okay moving further now we have sufficient knowledge about terraform and what are the terraform commands so the next task we have for us is like we need to install terraform on our local system so I have prepared this guide where I have put all the instructions and the commands which is needed for installing terraform so I'll put the link of this guide into the description section so you don't need to copy of any of the command just come back and check this guide and copy all the commands for installing terraform onto your local system so here on the right hand side you can see this is my guide and I'm gonna click on install terraform on Ubuntu so first utility which you need is you need to install the unzip utility on your system because we are going to download the installer of a terraform in a zip file so you need to have this unzip utility if you are working on a Linux operating system so uh I'm assuming that you have installed the unzip utility because I have already installed on my system so I'm not gonna perform that step again the second step is we need to go to terraform.io and click on the download CLI and Below there you should choose the installer based on your operating system so since I am using Ubuntu so I'm gonna use Linux so I'm just gonna select 64-bit right click on here copy the link head back to your terminal on the left hand side and I'm gonna download this ZIP file using W get command paste the URL and here you can see the terraform zip has been downloaded on my Ubuntu system now we need to unzip the file so the command is unzip terraform and the unzip has been completed and we can see the file terraform which has been extracted now we need to go back to our guide for the next instruction so now we have copied the link we have downloaded the zip file now we need to move this file inside user local bin so for that what we need to do is we need to use the command sudo move move the terraform file into the user local bin and for that you need to have the sudo permission otherwise you won't be able to move this file inside the user local band directory so I'm just gonna copy this command and paste it over here so here you can see my file has been moved and you can verify using user local pin command and here you can see the terraform has been moved to the user local pin directory and now we can verify the installation by running the command Tera form Dash B and here you can see we have installed the terraform version 0.14.8 all right demo time so the first demo which we are going to perform we are gonna set up a virtual machine on a Google Cloud platform using terraform if you are interested in AWS then you can skip this demo part and directly jump to the AWS demo but if you are interested in setting it up on a Google Cloud platform then you can follow this session also okay so I think we have sufficient knowledge which is required to provision a virtual machine on a Google Cloud platform using terraform so we are going to use all those Concepts which I have explained earlier and we are going to implement all those Concept in this demo also I have prepared a guide where I have put all the instructions which is required for this demo so during the demo if you forget to note any of the commands or any of the bash script which I'm gonna run so you can come back and check this guide I'll put the link of this guide into the description section so you don't have to copy or make a note of any of the instruction which I am going to perform during the demo so you just come back to this guide and copy and feel free to copy any of the commands to start the demo the step number one is we need to create a project on a Google Cloud platform so here you can see on the left hand side this is my Google Cloud platform account where we are going to create our first project but before that you need to sign up if you haven't done before so just go ahead and create a Google Cloud platform account and they offer you a one year free trial where you can expect element on a Google Cloud platform so I am assuming that you have an active account on a Google Cloud platform okay so how to create a project that's our first task so here you can see on the left hand side this is my Google Cloud platform console and here uh in the left left side of this Google Cloud console you can see yeah there are some projects which I have already created but if you haven't created any of your project then just click on this new project and you should be able to create your new project but anyway that you can do it it's very simple step but here I have already created a project with the name gcp terraform so that's going to be your first step which we need to perform in this demo moving to the step number second uh here you can see in the left hand side I have already selected map project that is gcp terraform the step number second is where you need to create a service account for your project and this service account we are later gonna use for authorization okay so in the left navigation menu click here and you need to go to IM and admin section and inside it I am an admin section you need to click for service accounts so here you just click on service account and here you can see I have created some service account earlier but you can create a new service account for this demo click here to create your first service account and here you can mention your service account name so I'm just gonna put demo service account and then just click on create and here on the right hand side you can follow the same structure over here also okay so then step number third where you need to assign some roles so the first rule which we are going to assign we are going to assign the project on a role so you need to select the project so just drill down into this drop down menu and look for the project here and here you need to select the role as honor okay there is a one more role that is compute admin role that you need to assign so click on add another rule and then you need to again drill down and you need to check for compute section A compute option over here and it should be somewhere yeah this is compute engine and here you need to select for compute admin role so that's the compute admin role which I'm gonna select and here you can check in the guide also this is the screenshot which I have taken from the same console okay after assigning the compute admin role we need to assign one more role that is compute Network admin so click on add another rule and again just go for compute option I think I just saw somewhere here here there here it is and we need to go for compute Network admin just select that one also okay so that's three role which you're gonna need for this demo and after that just click continue and then just click done once you're done creating your service account then you can see over here this is the service account which we have created that is demo service account and here if you look carefully there is a key ID column so there is no key associated with this account so the next step is we are going to generate the keys for this account and that is going to be our step number four so here you can see on the screenshot uh we need to click on this three dots over here for the account which we have created so here the account which we have created is demo service so just click on this option and then you need to look for manage key option just follow along with this guide all the instructions are there so and after that you need to look for this add key drop down so that is over here and here we need to click on create new key so once you click on create new key then it will pop an option for Json or p12 so we need to create a Json file and if you go back to our slide which I have shown you I think it's over here so if you go back to our slide then here you can see we need to download a Json file so this is the same Json file I am talking about okay sorry it is over here so this is the same Json file which we are going to download so just click create and you just need to save this file somewhere onto your system so I'm just click ok and here you can see I have just downloaded the Json file and this Json file will be useful for you to authorize your terraform to communicate with your Google Cloud platform okay moving further uh now we have completed the step number four we have now downloaded the Json file that will use later during the authorization process but the Step number five is we need to create a main.tf that is our terraform file which I have already explained but we are just gonna create this file from the scratch so what we need to do first of all switch back to our terminal and this is where I have already installed the terraform and that you can verify using this command okay so we are running the terraform version 0.14.18 sorry 8. so I'm just gonna create One Directory over here uh mktir data form gcp demo and just switch to the directory I'm just going to clear the screen and I'm just gonna first of all create a file that is main.tf yep I'm gonna open this file into VI mode okay so this file is empty and we are just gonna first copy a provider code okay so just copy this code snippet I'm just gonna explain what this is all about so here you can see the provider is Google although we have downloaded the key file but we haven't renamed that file to be like a Json account.j sorry gcp account.json so I'll do that part later secondly we need to mention the project ID that is gcp terraform so if you go back to your Google Cloud console over here and if you click over here then this is your project that is gcp terraform and this is your Google Cloud project ID that is gcp terraform 307199 so you need to copy this project ID into your terraform file so that is going to be your project okay so we just only left with this gcp account.json that we need to specify or we need to create or we need to copy the Json file into this my virtual machine which I am running over here okay and this is my region which can be anything uh it can be Asia or US based on your location and the Zone also okay so this is our first provider code which we are gonna put inside our main terraform file so I'm just gonna save and quit moving to the next step we need to copy the code snippet for the Google compute instance because that's the actual virtual machine which we are gonna run and that is going to be the E2 micro machine okay so again we are gonna update the main dot TF file which we have uh inserted just the provider code there so we are just now going to insert the Google compute code snippet inside that main. terraform file so first of all just copy this uh code snippet and again open that file into the vi mode and here you can see we already have the provider code so I'm just gonna go into the edit mode and I'm just gonna paste the compute instance code also so here this is our Google compute instance that's the keyword which one I keep and this is the name I have kept it as a default but you can keep any name of your choice that's absolutely fine and here this is going to be the tag or again the name so it is absolutely okay to keep any name of your choice but be careful about the machine type you need to specify the exact name what kind of machine you want so I am keeping it as E2 micro I just want the lowest CPU as possible or the lowest configuration CPU as possible third thing are the boot disk I am using Debian Debian 9 Ubuntu machine and the fourth thing is we need to specify the network interface and I just created this configuration over here and I kept the network to default so I'll get so that I can get some public IP for my virtual machine okay so I think we are done with this uh main dot terraform file and that is sufficient enough to start your virtual machine inside your Google Cloud platform the only thing which we are left is with this Json we haven't created this Json file yet although we have downloaded the Json file after creating the service account but we haven't copied that file into this same directory so that it can be used okay so first of all I'm just gonna quit here and save this configuration or the main.tf file I'll clear the screen and just to re-verify whether our content has been saved we I'm just gonna run the cat command and here we can see everything has been saved properly okay so I'll clear the screen and I'll do the ls command one more time and here you can see we only have the main dot PF File over here we don't have the gcp account.json file that we need to create it okay so first of all once again do the cat I just want to copy the same name so I'll just copy it I'll clear the screen I'm just gonna run the command touch and this so it's gonna create that file and now you can see we have the Json account sorry gcp account.json file over here and if you want to view the content of it then it will be empty and here you can see this is completely empty so first of all I'm just gonna copy the content of the key file which you have downloaded after creating the service account so that is I'm gonna do let's head back to browser and see where we have downloaded our Json file after creating the service account so this is the file which we have downloaded so I'm just gonna open that file so this is the file and I'm just going to open in one of my editor so this is the content of my Json file or the key file which we are going to use for authentication so I'm just gonna copy the content of this file I'm just gonna head back to my terminal and I'm just gonna open the Json file which we have created that is gcp account.json and which is empty right now so I'm gonna open in edit mode and I'm gonna paste the content over here and after that just save and quit okay so now we have placed our gcp account.json find file just parallel to our main dot terraform file so we have everything in place now so if you move further into the guide and there is a step number six where we are gonna perform these commands the first command is terraform Elite terraform plan and the third one is terraform apply so we are just gonna look all these commands one by one so the first command is terraform init command okay so just copy this command uh clear the screen over here and just run this command I'm just gonna explain what this data from init command is doing so if you remember uh if you see our code snippet in the main. terraform file here is the provider that is Google so whenever you mention a provider over here in the Mando terraform file and when the first time you're gonna run the terraform init command it is going to download all the dependencies for the provider that is Google so here this is the line in which it is going to download all the dependency which is required to communicate with our Google Cloud platform so in the terraform init command these are the prerequisite step which is done by the terraform so this is a mandatory step whenever you are going to work with a terraform module so you need to run the terraform unit command so that you can download all the required dependency which you have mentioned in your provider or in your main.terraform file so here the provider name was Google so if in case the provider name was AWS or Azure that it is going to download that dependency when you issue the terraform init command now you might be wondering there are lots of thing which are happening behind the scene when I issued the terraform unit command so don't worry we are just gonna see what it has done in the background so if you run the command LS lart so you will see a directory which is created as a DOT terraform so this is the directory which is get which gets created once you issue the terraform in it command so in that directory you will find all the required dependencies which is downloaded when you issued the terraform init command so to view the content just run the command LS and then inside the directory terraform and there will be some more directory that is provider ashy chord and here you can see Google and there will be version and that Linux AMD 64 machine and this is going to be your uh final I think uh file or the dependency that is terraform provider Google 3.60 that's the version so as I told you whenever you issue this command so it is going to download all the dependencies which you mentioned into the main.terraform file so we have mentioned Google so it has downloaded the whatever latest dependency of Google provider which was available during the demo so it has downloaded that dependency into your local machine so here you can verify those dependencies okay moving further into the guide we have completed the terraform init command and here you can see this is the console log which I have tested here in the guide so in case if you forget or if you want to verify your output then you can just cross check with it okay so as you can see we have completed the terraform unit command the next command is terraform plan okay so the what is terraform plan command so terraform plan command tells you like how many resources either you're going to add either you are going to update or either you are going to destroy or delete so it is just going to tell you what will be the plan are you adding it are you deleting it or are you destroying it so that is the main intention behind the terraform plan command so I'll clear the screen and I'm just gonna run the command terraform plan okay so the output will be little bit big on your console and it will be a little overwhelming but don't worry we are just gonna go through the complete output so this was the command which I have run that is terraform plan and as you know if you see our code snippet then we are trying to create a virtual machine a virtual compute instance so this is the compute instance which we are trying to create so here you can see uh it first highlighted what we are going to do so we are going to create a resource so that's the first thing it is going to mention you when you run the terraform plan command so you are going to create a resource so that's correct yeah we are going to create a resource and what kind of resource it is so that is a Google compute instance so that's the the resource which we are gonna create and if you look carefully at the bottom of this So the plan is one to add that means you are trying to add one resource that's a new resource actually so that's the thing which you need to notice over here the second is zero to change so are you going to update or change any of your existing resource then it's zero because no we are not going to update or change any of your our existing resource the third is zero to destroy that means we are not going to delete or destroy any of our existing resource so our main intention is we just want to create our virtual machine inside our Google Cloud platform so that means we are going to add a resource that is virtual machine so this is the Google sorry this is the terraform plan command one thing which you should remember this terraform plant command never create a resource it just tells you what it is going to do okay so here you can see we are just trying to add one resource and nothing else so this command will never going to provision or run any of our virtual machine on our Google Cloud platform before we move further and issue our next command that is going to be terraform apply I just wanted to show you my Google Cloud console so that I can show it that I am not running any of my virtual machines already so here you can see this is my Google Cloud platform and these are the resources that resources list is completely empty and you can also verify from here from the left navigation menu going to compute engine and going to VM instances and here you can see the list is completely empty and I am not running any of the virtual machine already so the next command is we are going to run the terraform apply command so that is going to be our next command where it is going to communicate with our Google Cloud platform and it is going to actually start a virtual machine on our Google Cloud platform okay so I'm just gonna copy this command and on the left hand side on the terminal first of all I'll clear the screen and after that I'm just gonna paste the command and it will ask enter a value so are you going to provision this virtual machine so I just gonna say yes okay so here you can see if you read the logs carefully it is first of all it's uh says that it is creating and it took some time to create those machine and after that apply complete and one resource added so that means our virtual machine has started in our Google Cloud platform so just go back to our same Google Cloud VM instance screen and just refresh this page and here you can see this is the test instance which we have started and as you know we have mentioned the zone Europe West 4 so that's our Zone and the name is test so if you go back into our code snippet of our main.terraform file then you will find the test we have mentioned so that's the test name which we have mentioned okay so that's our virtual machine name which we have provisioned using terraform and one more thing uh you can see the external IP which we have got so this is the external IP which is returned by Google Cloud platform and you can use this uh external ip2 SSH into this virtual machine so uh this is how you provision a virtual machine inside Google Cloud platform using terraform so now we have seen terraform init terraform plan and terraform apply command so these are the most three basic command which you need to know to work with terraform and any other cloud service provider okay so we have done the verify steps also so that you can check it by yourself by going back to your Google Cloud console and that you can verify the next command is terraform destroy and that is going to be the last command for this demo and this is it says destroy so it is going to delete everything so here you can see our test instance is running and now we are just wanted we just wanted to destroy this install so just copy this command from here go back to terminal clear the screen and paste the command so uh is the command says destroy it is going to remove and here you can see now there is 0 to add yeah we are not adding it we are not updating it but yeah we are trying to destroy it so that's the command type yes and it's gonna take some time and it's going to delete our virtual machine instance okay so here it has destroyed our resource and we can go back to Google Cloud console and just refresh this screen to see whether it has deleted or not and here you can see the test is instance or the virtual machine has been deleted all right so now we are done with our first demo that is provisioning a virtual machine on a Google Cloud platform using terraform and now we have pretty much seen the terraform any terraform plan terraform apply and terraform destroy commands moving to the second part of the demo where we are going to provision a ec2 instance on AWS console so again I have prepared this guide for AWS also and you can find this link of this guide into the description section of this video to start the demo first of all the main requirement for this is you need to set up your AWS account that is going to be our first step so I would recommend you to create an account with AWS and AWS offers you a 12 months free trial or 750 hours per month limit free tire which you can use for this kind of a demo but you need to provide some credit card information so that's absolutely okay that will charge you one dollar for authorization but after that it won't charge you for anything so I am assuming that you have an AWS account and you have already signed up for that moving further after you have signed up for AWS you need to log in and you need to log in as a root user so that is going to be your second step so I'm just going back to my AWS account and here I need to input my email ID and password so I'll be back after entering my username and password and also remember you need to log in as a root user once you login into your AWS console then this is the dashboard which you're gonna see okay moving along the guide the next step is where we need to generate the access keys so as I told you like whenever we are working with the AWS so you need to have your access key as well as the secret access key so the terraform can authorize itself to communicate with AWS so for that what you need to do is you need to go on my security credentials and that you can do it by clicking on your username over here going to my security credentials and then go for Access Keys over here and here you can see I have already generated my access key but if you are doing it for the first time then you need to click on this create new access key and once you click on it so it is going to show you the access key ID as well as the secret key here I have hidden it because that's some confidential information which I don't want to share but you get the idea like you need to generate this access key and the secret access key here but always remember this secret access key will be visible only once for security reasons by AWS and once you uh close this then you won't be able to use or view the secret access key actually so you need to copy both access key as well as the secret key so I'm assuming that you have copied both the information and after that you can just click on close and here you can see as I have already created one on 18th of March but here I can see there is a one more uh access ID key which has been generated on 23rd of March the day when I'm recording this video okay and both are in active state so that's the thing which you need to notice over here so moving back to the guide same instructions I have mentioned over here with the instructions and the screenshot and here you can see I you need to copy the access key ID in the secret access key number third is where we need to create a main dot terraform file as similar to the previous demo where we have seen for Google Cloud platform also where we have created the main.tf file so this is on the left hand side you can see my terminal and I'm just gonna create a directory AWS demo I'm just gonna switch to directory AWS demo I'll clear the screen I'm just gonna create the file man.tf and I'm just gonna open in the game mode and this file is empty so we don't have anything over here okay so the step number one over here is we need to mention our provider details in adapt for AWS so the Syntax for that is resource provider type and the name name is option uh not optional but it's a user defined or you can skip it also that's also fine okay so here is the actual example where I have mentioned like what is provider what is AWS and region access key and secret key here you can see this is xXx because I don't want to put my actual secret key or access key over here I'll copy this code snippet from here and I'm just gonna paste it inside my main. terraform file which I have just created and I'm just gonna replace this access key because I don't know with the actual values which I have created inside my AWS console okay so now I have copied my access key and secret key as I told you I cannot show those secret key and access key because of security reason but after copying and pasting your access again security key you just save and quit it okay so after that moving further uh into the step number second where we need to create our AWS instance and for that we need to find the Amazon machine image and that you can find by using AWS console so go back to your AWS console click on Services go to ec2 and in the left navigation you should see an option Amis click on Amis here in the drop down mention is drop down option uh always remember to select public images and here you need to type the option Ubuntu because I'm just gonna use the Ubuntu as my operating system for virtual machine click enter and here you can see this is the Ami which we are looking for so once you click on this uh line number then you can just copy this Ami ID from here copy to I'm just gonna copy it okay and then copy this value and store it somewhere because we are gonna need it later okay moving back to the guide uh all the instructions I have mentioned for how to find the emis over here also uh the next instruction is we need to uh mention the AWS instance so that's the next code snippet which we are gonna put it over here so copy this code snippet where we are going to create our AWS instance and this is the name that is we are keeping easy to example so it is a user defined name you can keep it any name of your choice secondly we have copied the Ami ID so you need to copy and paste that Ami ID over here and third thing you need to mention the instance time since it is a demo purpose so I am keeping it as a T2 dot micro but you can increase this instance type based on your requirement and choose some higher configuration also and this tag name is completely up to you what kind of a tag name you gonna put so I'm just gonna put terraform ec2 okay so that's the code snippet which I'm gonna copy from here and paste it inside my main.tf but I'm also gonna update the Emi also okay so copy it and go to your terminal on the left hand side and yeah open in VI mode go into edit mode and paste the content over here so the thing which we need to replace it over here is the Ami ID so I'm just gonna remove the existing one and I'm just gonna paste the new one which we have copied from the AWS console so I'm just gonna paste it over here so now uh save and quit okay and you can re-verify whether you have saved correctly or Not by using the cat command and here you can see everything the provider name and the AWS instance all right moving to the next step now we are ready with our terraform file and now we can run our terraform command so the First Command which we are going to run is the terraform unit command so what does the terraform init command do so first of all just copy this command and I'm gonna explain like what it actually does so just run this command first of all okay here you can see uh our Command has run successfully so the terraform init command is responsible for downloading all the dependency which you mentioned for the provider so the provider over here is AWS so if you'll see our code snippet so that's the provider we have mentioned that is AWS so whenever you are going to run a terraform you need command so it is going to first of all look which is your provider so that is your AWS and it is going to find the latest version whatever available so at that point of recording this video the latest version what 3.33 and it is going to download all those dependencies uh onto your local system okay and you can verify this dependency by running the command lslar T and you should have a DOT terraform file folder which is created after running this terraform unit command okay and as you remember we have only created the main dot terraform file okay and these files has been created after we run the terraform in it command okay so and you can also view the content here also by running the LS command and here you can see so these are the content of our terraform dot terraform directory and here you can see there is a image Linux AMD 64 which means that's the virtual machine or ec2 instance image which is a terraform has just downloaded by using this provider information which we have mentioned so if you go back to our guide so this is the provider information that is AWS once we mention this thing then terraform unit command going to download all the required dependencies for AWS all right so now we have run the terraform unit command the next command come is we need to run the terraform plan command so I'll clear the screen over here and I'm just gonna run first of all the command Tera form plan sorry I just did the type over here I'll just clear the screen once more and I'm just running terraform plan command all right so the output of this command is little bit big but don't worry we are just gonna go through the output and try to understand it okay so what the terraform plan command does it it just tells you that what it is going to do once you uh actually run the terraform apply command so terraform apply is the next step but there is a final command terraform apply which is going to actually run your or install or provision your ec2 instance on AWS but we are still one step behind and we are focusing on terraform plan command so terraform plan command tells you like what is going to happen in future and the future is the next step so it is going to create one resource so that's why there is a green plus sign over here and with the option create so that means once you run the terraform apply command then it will be going to create an resource okay so if you look carefully there are three options over here in the plan that is one to add that means one resource will be added and that's correct we just want to spin our easy to install so we are trying to add a resource in our AWS environment that's why it shows one to add we are not updating anything that's why it's 0 and we are not deleting anything so that's also zero so terraform plan command tells you like what is going to happen once you run the terraform apply command so it will just sketch you the plan like how many resources are going to be added removed or updated all right so now we have seen the terraform plan command so the next command is the terraform apply command so this is the command which is actually going to start our ec2 instance by contacting the AWS server and it is going to start our ec2 instance all right so this is the command terraform appliance which I'm just going to copy from here and paste it on my terminal and hit enter it will ask like do you want to perform this action so just type yes and hit enter okay so our terraform apply command has finished successfully and here you can see it has added one resource and the message is applied complete so now we can go to AWS console and verify whether our instance has been created or not so go back to your AWS console go to Services go to ec2 and here you can see instances running that is one so which we have started recently just click on it and here you can see uh the status is running the name is terraform ec2 and which you can verify by using our main.tf file and here you can see the name is terraform ec2 the same name which we have given over here so you can see over here also and it's running and yeah this is the uh our ec2 instance which we have just provisioned using terraform and you can verify the Zone also and this is our public IP address so that you can use it to access it and yeah so this is how we provision our ec2 instance using terraform all right so the next command which I would like to tell is to destroy so now we have created this instance so how we can destroy it so the command is pretty much similar the only thing which we are going to change is the last keyword that is destroy and hit enter and it will again Ask for confirmation so just say yes and hit enter okay so the destroy has been completed it has destroyed or deleted our one resource so on the right hand side we can just refresh this console and see whether it has been deleted or not and as you can see the instance is gone so we have no longer any ec2 instance running over here okay so you can remove this filter also so you can see uh we have previously terminated two instance but none of us none of any of the instance is running right now all right so with that that was our last command that is terraform destroy and uh that's the demo I just wanted to show you for AWS part also [Music] in today's session we are going to discuss what is terraform input variables how to declare terraform variable what are the different types of terraform variables that is string number Boolean list map and finally we are going to see demo for each variable types and there's also going to be a second part of this session and in that session we are gonna touch upon some Advanced concept about terraform variables in the second session we are going to start with the variable.tf file so we will focus like how to create a variable.tf file and how to store your variable inside that file secondly we are gonna also look on the terraform TF War file so this is a little special where you can also store your terraform variable so we are gonna see that one also and there is a One More Concept where we can create a multiple terraform War file based on your different environment like staging production or testing and lastly we are gonna see how to pass those variable using the command line along with the demo I have prepared this guide I'll put the link of this guide into the description section of this video so in this guide you will find all the terraform file along with all the terraform command which I am going to perform inside the demo so you can just come back and check this guide and you don't need to remember or note down any of the commands so I'll put this link into the description so just go back and check this guide where you will find all the details which is required for this demo to start with what is terraform input variable so this is my terraform file this is a sample terraform file where I have not declared any variable yet so if you look carefully over here this is my provider that is AWS because I am going to provision an ec2 instance on AWS so this is my provider tag over here and with the region access key and secret key secondly this is my resource where I'm going to Define my ec2 instance and the type of ec2 instance which I am going to provision in AWS so here you will find all the details like here I am trying to create a ec2 instance of T2 dot micro and the tag name which I am going to assign is terraform ec2 so the question is how the variable fit into this picture of terraform so considering scenario where you need to change the instance type from t2.micro to T2 dot medium so is it possible to change then the answer is yes using the variables you can change the instance type from t2.micro to T2 dot medium and you do not need to hard code this value inside your terraform file similarly if you need to change the tag name inside your terraform file then that's also possible and that you can also do using the variables here is one of my terraform file where I have declared a variable and in that variable I am going to change or I am going to define the instance type so the variable name over here is VAR dot instance underscore type so this is the variable name which I have kept and just below the file you will see this is my variable where I have defined the actual values what it's going to hold so here I need to Define three thing description type and default and in the default we are going to specify its actual value so this is how you define the variable inside terraform and this is the syntax you need to follow to declare a variable so starting with variable is the keyword which you need to keep and secondly in the double quotes you need to Define your variable name the variable name can be of your choice so you can put some meaningful name to your variable apart from that there are three things which you need to be careful about so first one is description second one is type and third one is default so starting with the description so this is something a meaningful name which you need to provide to your variable so so that you can identify what kind of a variable it is second you need to define the type what kind of a variable it is going to be whether it's going to be string Boolean list number or and any other type the third thing is default what it's going to be the default value so here uh just I'm taking an example so I'm just trying to assign a value T2 dot micro so if you're trying to set up a instance time so here you can change the value so like T2 dot medium also now we know the syntax on how to declare or how to create a variable inside terraform so the first variable type which we are going to take is string as I mentioned earlier first of all we need to take a keyword variable secondly we need to define the variable name that is of your choice so here I have taken the variable name as instance underscore type third we need to define the description so here you can Define some meaningful name of the variable fourth we need to define the type and here I am defining the type as string because we are going to declare a string type variable and what's the value of this string type variable so the value is T2 dot micro before we actually jump into the demo there is one thing which I would like to mention over here if you are trying to learn terraform for the first time then I would highly recommend to follow the part one where I have explained what is terraform and how to create your first terraform file because that will help you to understand these Concepts in a much easier way okay let's head back to our guide and see what is the terraform file for defining the string type variable so if you scroll down into the guide there is a section 2.1 where we have defined the terraform file for string variable type so here if you scroll it down further then here we are going to first Define a provider then we are going to define a resource but in that resource we are going to create a variable that is instance type and in the same terraform file we are going to create a variable so here I have defined the variable that is of name instance type type is string and the instance type which we are taking over here is T2 dot micro so what you need to do is you need to copy this code from here head back to your terminal and you need to create a main.tf file so I'm just gonna create this file and as well as edit it and once you open that file into the edit mode you just need to press the content and just to reiterate we have we are creating the provider we are just creating a tag provider that is for AWS then we are defining the resource for AWS instance and that is T2 dot micro and for T2 dot micro we have taken the variable that is of spring type okay so once you paste those con content inside the main.tf file you need to save and quit okay that's been done you can verify the file so yeah we can see all the details are there before we run the terraform command or apply these changes let's go back to our AWS console and see if we have any previous ECS to instance running or not so this is my AWS console where I need to refresh this page and see if there is any running instance or not so as you can see there is no running instance here okay so let's head back to our guide or sorry terminal and I'll clear the screen now we have created our main dot terraform file along with the variable of string type now we need to apply those changes I'll increase the font size over here so that you can see a little bit more okay so the First Command which I'm going to run is terraform init it is going to download all the dependencies for AWS because we are going to create a AWS resource okay that's been done I'll clear the screen the second command we are gonna run is terraform plan it is going to tell how many resources it is going to add okay so here you can see it is going to add one resource that is easy to instance and that is T2 dot micro okay I'll clear the screen again and I'm gonna run the final command that is terraform apply okay it will ask do you want to perform these actions so you need to type yes now it's creating those ec2 instance on AWS okay so the instance has been created let's set back to our AWS console and verify our ec2 instance so you just need to refresh this page and here you can see the instant stage is running and the name of that is data form ec2 and if you look our guide over here I think this is over here so here we have defined the tag name as terraform ec2 and that is again it is the same tag name over here so that means our ec2 installs has been created successfully and we have created a string type variable inside our terraform file now we have seen like how to create your string type variable inside your terraform file before we jump to the next variable type let's just try this instance so you need to run the command terraform destroy oh sorry I have Mr spelled over here so you need to type destroy moving to the next variable type that is number so again the syntax is going to be pretty much same and all the field description type and default is going to be the same the only thing which is going to be change over here is the type you need to define the type as number and the value that is you need to Define some numeric value to it let's set back to our guide and see the actual terraform file for the variable type number so if you scroll down further then this is the heading for number variable type and this is the terraform file which is we are going to use for number variable type so here the structure and the terraform file is pretty much seen a provider and resource the only thing which we have changed over here is the count so what does it mean by count so in the previous example we have seen like we have created only one ec2 instance but here we are going to provision two ec2 instance on our AWS console so here we have defined the variable that is instance count and the type we have taken is number and the default value which we have given is 2 so that means we are going to provision 2 ec2 instance on our AWS copy this code from here and go back to your terminal and create your main.tf file and paste the content over here save and quit clear the screen verify the content of the file and yeah it looks pretty much okay to me I'll clear the screen again and we have seen the terraform command sequence like first of all we need to run the terraform in it then terraform plan and then terraform apply so I'm just gonna run all these three command because I have already shown those commands in the string type variable all right so now we have applied all the terraform configuration and now we are provisioning two ec2 instance so let's get back to our AWS console and refresh this page and here you can see we have two terraform ec2 instance running over here so this is how you can use the number type variable in terraform to increase the instance count and you can use to provision multiple machines inside your AWS console the next variable which we are talking about over here is the Boolean so again the syntax is pretty much same the only thing which you need to Define is the type and the type which we are going to Define is Bool for Boolean and it can hold two values either it can be true or false in the guide in the point 2.3 you will find the example of a Boolean variable and this is my terraform file and in this terraform file I am just enabling the public IP address so this is the tag which you need to use Associated public IP address and you need to pass the value that is true so here I have created a variable a Boolean variable and the value I have assigned this over true and the same variable name I'm going to use over here so I'm just gonna perform the same step I'm just going to copy this file copy this code of terraform file into our main.tf and I'm just gonna run the commands data from any terraform plan and terraform apply and then we are gonna verify on AWS console so I'm just gonna fast forward all this command and I'll be back once everything is done all right now I have applied all the terraform commands let's go back to our AWS console just refresh this page and here you can see our ec2 instance is running and now we have our public IP address associated with our terraform ec2 instance till now we have seen string number and Boolean those are single data types but now we are going to see a collection of data types so we will start with the list and Ace the name suggests lists contain multiple values so here the variable syntax is again going to be the same the only thing which going to be changed over here is the type so you need to define the type as a list but along with that you need to mention what kind of a list it is it is it going to be a string is it going to be a number or is it going to be a Boolean so here the type I have defined is over here is a string and the string contains the user so I am taking the list of user as user 1 user 2 and user 3. let's head back to our guide and see the example of a list terraform file so this is my list terraform file where I am going to provide the AWS provider and the resource type is AWS instance and the one thing which I have added over here is the IM user to this resource so for this ec2 instance I am going to add a list of user as a im user for AWS and the variable name I have kept over here is this variable username and this is little spatial because I am going to it's kind of a iteration which I'm going to perform over here so here I'm just going to define a function that is length and then length is going to calculate the length of my list variable so list variable is over here which I have defined as a user underscore knee and it contains three values user one user 2 and user 3. so again it is going to put all this value inside that IM user and it is going to create the all three user inside my AWS console using this count dot index because we have already taken out the length of my list so now I'll create a main Dot from file you copying this code and I'll run the command terraform any terraform plan and terraform apply so I'll fast forward that particular portion and I'll be back once my ec2 instance is running all right so now I have applied my configuration of my terraform commands let's go back to my AWS console and refresh this page and here you can see my ec2 instance is running the next thing which we need to verify is the IM user so click on AWS home page and this is going to be your dashboard click on IM and here you will see users so click on it and here you can see we have created three user user1 user 2 and user3 ah there is a typo in the user 3 but anyway you know the rule over here so now we are able to create the three user inside our AWS console moving to the next and last variable that is map and again the syntax is pretty much same but only thing which you need to be careful over here you need to type you need to define the type as a map as well as you need to mention what kind of a variable it is going to be so here I'm I have taken an example for a string map type and as you know map always consists of a key and a value so here the key is uh project and the value is environment so this is how I am going to Define my map variable head back to our guide and here you can see I have defined my terraform file in the 3.2 section so this is my terraform file which I'm going to use to define my map variable and here I have defined the provider as AWS and again I'm going to provision the ec2 instance on AWS and this is the variable name which I have kept over here that is Project environment and in the project environment I have taken the project name as project Alpha and the environment type as Dev okay so again I'm just gonna perform the same step I'm just gonna copy this terraform file and I'm just gonna run the command terraform init terraform plan and terraform apply so I'll just fast forward those commands and I'll be back once everything is applied all right so now we have applied our terraform configuration let's head back to our AWS console and refresh the page and here you can see we have provisioned our ec2 instance we need to verify one thing for this ec2 instance that is environment and project name so you just need to click on this instance ID and once you click on this instance ID then you will be redirected to this page and you need to click on this tax and here you can see the project is Project Alpha and environment is there and if you go back and check our guide and here this is the same environment and project which we have defined inside our map variable all right so that conclude our first part where we have seen the string number Boolean list and map variable type inside terraform in the second part we are going to see the variable.terraform file terraform TF for files multiple terraform TFR files and how to set the variables using command line hello everyone this is Rahul and welcome to the second part of terraform variable series alright so what do we have in today's session so this is my second part on terraform variable so first of all we are gonna see how to create a variable.df file that is variable terraform file because where you are going to store your variables secondly we are gonna see one more advanced concept on terraform.tfr so it is one more file where we are going to create the variables but it is little bit different from a variable.tf file so that is going to be the second topic of discussion don't worry we are gonna perform the demo on each topic which we are going to discuss today so the third in the list we are having is terraform TF War where we are going to create a multiple terraform TF Wars based on the different environments such as staging testing and production so the third in the list we are having is multiple TF dot War file so it is also same concept but in the multiple terraform TFR files are based on the different environment which we generally have so then let's take an example testing staging production so we are gonna see the demo of that one also and the last topic which we are going to discuss is how to pass a variable using the command line argument so that is going to be the our last topic of discussion along with the demo if you haven't seen my previous session where we talked about variable types such as string number number Boolean list and map then I would highly recommend to check my first part where I discussed about this terraform variables because that is going to be the basis for today's session all right so to start with our first topic that is variable.tf that is variable terraform file so first of all we are going to consider a simple scenario where we are going to create only one terraform file that is main.tf and in that file we are going to create variable also so we are not going to create a variable.tf file separately so first of all let's understand this concept so here you can see we have created a variable that is where dot instance underscore type and the same variable we have defined in the same file that is main.tf over here so the variable type over here is string and the value is T2 dot micro so in this approach what we have done we have created only one file that is main.tf and inside that file we are having our actual terraform script along with our variable and this concept is very easy and this implementation is very easy when you are trying to learn terraform but this practice is not followed when you actually work with production or staging environment so you need to have some Dynamic configuration in your terraform file so for that purpose we are going to create a separate variable dot terraform file so how to do that so to improve our code what we need to do is first of all you need to create your main.terraform file so in the terraform file you will see we are having provider that is AWS second you are having a resource that is easy to instance so these two components are mandatory for creating a terraform file but this is just an example it can differ for Google Cloud platform for Azure also but here if you look on the right hand side this main dot terraform file doesn't contain any variable so how to create those variables or how to store those variable inside a terraform file so for that we are going to create a one more file that is called variable.tf and inside that file we are going to write our variable okay so as you can see on the left hand side the same variable we have created in the same file that is main.tf but once we are talking about the variable dot terraform then we are going to create a separate file and the name of that file should be variable.tf okay so here everything is same the only thing which we have did over here we we have created two files one is main.tf and second is variable.tf and everything if you compare on the left hand side will be same okay now we know the concept of variable.tf file let's perform some demo so along with this session I am going to post this link of this guide into the description section of the video so all the commands and all the scripts which I'm going to perform during this demo will be available in this guide so you can follow that guide as of now I am running this guide on my local host but as soon as I publish this video so this guide will be available on my website to start with first of all we need to create main.tf file and for that this is the content we need to mention the provider that is AWS second we need to mention the resource and here we are going to start an ec2 instance on AWS so that's the resource which we are going to provision using terraform so I'm just gonna copy this script from here and this is my terminal on the left hand side and this is my Linux machine where I have already installed terraform and if you haven't done the installation then also I'll put the link and the video session where I have shown how to install terraform into the description section of this video okay so to start with first of all I'm gonna create a main dot terraform file and for that I'm just gonna directly write main.vie main.tf okay I'm just gonna paste the content over here and I'll just quit save and quit I'll clear the screen so here you can see my main.tf file has been created I'll view the content and yeah I can see everything is everything looks okay to me okay if you follow along the guide then the second step which we are going to create is variable.tf file so here the content is pretty much simple and here we are going to declare a variable of type string and the value is T2 dot micro because we are going to spin a easy to instance of T2 dot micro because we are just performing for the demo demo purpose so we don't need a heavy CPU machine so that's why I have taken T2 dot micro so I'll clear the screen on the terminal on the left hand side and I'm just gonna write VI variable dot TF okay and I'm just gonna copy this content and paste it over here after that I'll save and quit and I can see the file has created so yeah my file has been created I can view verify the content of variable.df by running the cat command and yeah it looks okay so the next question is how this whole variable thing will work together so here in this guide I have mentioned the command terraform apply but if you are doing it for the first time then you can't run the terraform apply command directly you need to follow some sequence of terraform command so as you can see on my terminal on the left hand side first of all I'll clear the screen the First Command which you need to run is terraform init so this is the first thing which you need to do it is going to download all the dependency for AWS environment on your local system so that it can initialize all those things all right so now I have run the terraform init command and all the required dependencies has been downloaded for AWS environment so the before we run actual terraform apply command that is one more command that is terraform plan that that is our second step which we need to run so terraform plan command is actually telling you how many resource it is going to be added so as of now we are trying to create a ec2 instance so that's why we are going to create one resource into AWS environment so this command is going to tell you like how many resources going to be added so that is one so that's the purpose of our terraform plan command so as of now we have seen two commands those are the prerequisites command which you need to run every time before you run your terraform apply command so the two commands are terraform init and terraform plan okay so before we apply I'll clear the screen and the final thing which we need to do is terraform apply and it is going to take some time I'll be back once this data from apply command is done all right so as you can see below our terraform apply command has just finished and it has added one resource to AWS so I'll head back to my AWS console and this is my dashboard of my AWS account this is a free account which I am using I'll click on ec2 and here if you see the one stance is running so click on it and here you can see our terraform ec2 instance has just provisioned and it is up and running so this is how you are going to create a variable file and the same variable file can be used to define the t2.micro instance type and this is just an example of a variable dot terraform file and this file can be complex in your real world scenarios but this is just a simple example and the demo for variable.terraform file before we jump to the next topic of discussion the first thing which we need to do is we need to destroy this ec2 instance because we are going to again spin the same ec2 instance but with a different setup okay so how to do that so for that there is a command terraform destroy and it is going to destroy your ec2 instance it will ask enter a value so you need to type yes okay so here you can see the message destroy complete so that means our instance should be destroyed and that we can verify from here we just need to refresh and here you can see our instance has been deleted moving to the next topic uh the thing which we are going to do over here is we are going to create a terraform TF War files and again uh these files are being used to store the variable and we are going to perform the same demo where we are going to provision an ec2 instance on AWS but this time we are going to use this TF War files our setup is pretty much same we are going to create a main dot terraform file after that we are going to create a variable.tf file but here we are going to create a one more file that is terraform TF War file yeah I know it might be little confusing to you we are already having variable.tf file and again we are creating a terraform TF War files and but why do we need it but hold on your question we are going to answer that question into the next topic of discussion where we are going to create a multiple TF War file but just for the time being just try to understand like we need to create one TF War file where we are going to store the variable value so we are going to create three files over here first of all main.tf and here we are going to write the provider second the resource which we are going to provision that is our ec2 instance and that easy to instance we have already stored in the variable that is instance type so in variable dot TF we are going to declare the variable that's it we are not going to store any value we are just going to Define a variable over here and this variable name over here you can see we are using into the main.tf okay the third file which we see over here is terraform TFR and in that file we are going to put the actual value of our instance right that is T2 dot micro and since this is a string type that's why it is under double quotes let's start creating this file so first of all I'm gonna create a main.tf and for that what I'm gonna do I'm just gonna copy the content from here I'm going to head over to terminal and I'm just directly going to write vimain.tf I'm just gonna paste the content I'll save and quit moving to the second file that is variable.tf I'm just gonna copy it I'll clear the screen over here I'm just gonna write the command with bi variable and always remember I try to put the correct name if you make any spelling mistake in the file name then it might cause you some trouble okay so I'll just paste the content here I'm just defining the variable but not putting the actual value I'll just save and quit I'll clear the screen and the next file which we are going to create a terraform tf1 I'm just going to copy the name directly so it will be little easy for me okay so I'm just gonna paste the content that's done okay I'll clear the screen I'll just verify the content and here I can see three file has been created we can verify by going into it and yeah it looks okay the main.tf I'll clear the screen and we can also look for variable also and yeah it looks okay and again we can look for a terraform tf1 and it's look okay to me okay the next step in which we are going to run three command terraform init then after that terraform plan and then terraform apply so I'm just gonna speed up this process because this gonna take us some time so I'll just fast forward this process and I'm just gonna run this three command one by one once you run the terraform apply command then it is going to ask do you want to perform this section so you simply need to type yes now our ec2 instance has been provisioned we can go to our AWS console I'm just going to refresh this page and here we can see our terraform ec2 instance has been created and this is the name which we have supplied using terraform tfvr files now we have seen how to use tf.warfi let's first destroy this instance moving to the next topic where we need to create a multiple terraform DF War files and we are going to answer that question why we need terraform VAR files and why they are important okay so here on the left hand side you can see we are having a main.terraform file and here the example is pretty much same we are going to provision an ec2 instance and the provider is AWS and this is the resource which we are going to provision but here if you look carefully then we are going to create here two variables so the first variable is instance type and the instance type which we are going to Define is T2 dot micro secondly there is a one more tag and we are going to create a variable for that tag also and the variable name for that is environment underscore name so what why we are using environment underscore name because I want to create this terraform file for different environment that is staging and production and I want to supply those values inside this tag variable so if I'll hard code some value over here then I cannot put or assign some different values inside this main dot terraform file so that's why we need to have multiple terraform VAR files to supply those values okay but don't worry we are gonna see the demo as well as this example so here I'm going to create a variable.tf file and this variable.tf file is going to be empty we are only going to create or we are only going to declare variables now we have our main.tf and variable.tf the next file which we are going to create is our stage TF War file and as you can see uh the name of the file is different and we are using stage because we want to have this file for staging environment and again we are going to create a one more file and that file is going to be for production and the name of the file is production.tfr so here the variable assignment is pretty much same we are going to assign T2 dot micro value but the environment name will change here we are going to Define stitch for staging and production for production and then we are going to use this files based on our requirement if we are going to put this file or we are going to apply this configuration on stage environment then we are going to use stage df4 and if we are going to apply the same configuration on production then we are going to use the production TFR so that we can change the value of environment variable either it's going to be stage or either it's going to be production so that's why we have created these two file and we have created these variable along with the variable.tf file then the next question comes like how we are going to supply this file name using the terraform apply command so here is the syntax for supplying the different TFR files along with your terraform plan command or terraform apply command so here the command is pretty much same you can write terraform plan or terraform apply followed with the keyword dashboard Dash file and then your actual file name if you are going to apply this configuration for stage then you are going to put stage dot TFR and if you are going to apply the same configuration for production then you are just going to supply the production tf4 let's head back to our guide for the demo and here first of all we need to create a main.tf along with that we are going to create two variables for environment name as well as instance type but let's first of all create the main.tf file I'll copy the content from here I'll go to terminal and I'm just gonna write main.tf I'll paste the content I'll save and quit next we are going to create a variable.tf so I'm just going to copy the name of the file I'll just type the command VI and I'm just gonna paste the content over here [Music] I'll clear the screen and again we need to create two more files one is stage TF Force so I'll just copy the name and I'll go back to my terminal I'll paste the content over here I'll save and quit and again I have not written that production.tfr but I'm just gonna create that file also speech or maybe question dot PF Wars I'll update the instruction in this guide and meanwhile I'll just copy the content from here for stage and here I'm just gonna change the content to production I'll just save and quit now again we need to run the three commands so the three commands are terraform first of all terraform in it then second I'm gonna run terraform plan but as you can see this command is now little special here you need to supply the name of the file the TF bar file which you need to apply so I'm just going to apply stage TF mod file and then I'm gonna run the terraform apply command again with the same parameter over here so I'm just gonna fast forward this clip and I'll be back once everything is done now we have run all the three commands along with the file which we need to supply in the terraform plan and terraform apply command next we will go to AWS console I'll just refresh this page and here you can see our ec2 instance has been created and you if you look here then here the name stance is stage so that's the name of the instance which we wanted to have moving to the next topic where we are going to supply the same variable using the command line so here first of all I'll explain how we can achieve that so first of all we need to create the main.tf file and the file is pretty much same we are going to create it a provider and then we are going to create a resource that is our ec2 and here we are going to declare a variable that is where dot instance underscore type so that's our variable and it will hold the value T2 dot micro okay then secondly we are going to create a variable.tf file and this variable.tf file will be empty it will only have the variable name inside it okay and third thing since we are going to perform this on a command line so we need to supply some value at our command line so the command like for example terraform apply so here we need to supply the variable name along with its value so how we are going to do that so first of all we need to write the command terraform apply followed by Dash war and then you need to supply actual variable name that is instance underscore type and followed by its actual value T2 dot micro to start with first of all we are going to create a main.tf file so I'll just copy the content from here I'll go back to my terminal I'll open in the vi mode main.pf I'll paste the content I'll save sorry I'll save and quit I'll create one more file that is the variable dot PF and I'll also paste the content from here and inside that file I'll save and quit now we are having main.tf and variable T of variable.tfn please and we can verify using the ls command and yeah both files are there now we need to run those three commands and for that we need to change a little bit so first of all we are going to run terraform in heat command after that we are going to run terraform plan command but remember we need to supply the variable name so the keyword we need to first put is Dash war after that we need to assign the instance type the variable name along with its value okay so I'm just gonna run all these three command and I'm just gonna fast forward this clip so I'll be back once everything is ready now I am done with all the three commands so we can go back to our AWS console and I can refresh the page over here and here you can see our ec2 instance has been provisioned so this is how you pass a command line argument and you can pass a variable into your terraform terraform configuration [Music] you can in today's session we are gonna discuss about terraform locals terraform locals is a very important concept and it comes very handy when you are working with a relatively very large terraform files to understand this concept I have taken a sample terraform file code snippet over here and here if you look carefully I am trying to Define two resources one is AWS VPC resource and another one is AWS subnet resource and here I have created the tags for each resource and each tag start with the name staging because I'm using this terraform file for staging environment that's why I have kept the tag name starting with staging but let's suppose if you want to use the same or similar terraform file in the production environment then what you need to do you need to manually go into the terraform file and you need to update these values from staging to production because that will that will be more suitable from production environment perspective but I don't think it's a good practice to go inside your terraform file and manually update all the tags tag name one by one so for that purpose we have a concept of terraform locals and you can declare a local variable inside your terraform file the main benefit of using the locals variable over here in the terraform file is you need to declare variable at one place and once you declare or modify that variable then it is automatically going to affect your complete terraform where you have used that terraform variable so here you can see at the top we have defined a variable like stitching environment and we have assigned the value staging okay and throughout the terraform file we are using the same variable uh like in the AWS VPC and AWS subnet so if you are going to change or modify this name staging to staging 1 or staging 2 or staging 3 then we don't need to manually go inside each resource tag and update the value we just need to update the value at one place that is inside our local variable before we proceed if you haven't seen my previous session on terraform variables whereby I have explained what is like terraform variable string number Boolean least map and how to use variable TF TF more files and command line variable so please go go back and check those previous session if you are just getting started with terraform and you want to learn more about terraform variable so those will be a really good session uh before you jump into terraform locals all right let's see some demo and this is my main main.terraform file which I have created and here if you'll see carefully here I am to I'm just trying to create an AWS instance and that instance is easy to install so over here if you look carefully so this is the definition or this is the tag which I'm going to use for ec2 and the instance type which I am trying to use is D2 micro okay and apart from that uh here I am providing a provider tag that is for AWS which we need to mention because we are going to perform this demo on AWS and apart from that there are a couple of more tags that is VPC and AWS Subnet in the next upcoming session I'll explain what is AWS VPC and AWS subnet but in the meantime just try to remember we have created a multiple tags over here because we are doing the demo for locals so that's why we are just concerned about these tag names and each tag names over here is starting with the name staging same here same for subnet and same for instance okay so uh we will try to improve this terraform file using the terraform locals the first step would be to create a local variable and for that we are gonna create one tag that is locals and in that you can define a variable and variable name you can keep whatever of your choice so I'm just gonna keep the variable name ads teaching environment and you can assign some value to it so the value name can be let's say staging variable inside our different tags that is VPC subnet and instant so for that how you need to do is uh just remove the stretching tag over here put some dollar sign over here and after that Curly braces and then you need to type in the value over here that is local Dot and after that you need to use your variable name so the variable name we have defined over here is staging environment so just use that variable name over here and you're done so just copy the same variable name from here and replace here as well as here at line number 35. okay so now you can see we have improved our terraform scripting code so here we have declared only one variable and now we are using the same variable throughout a different uh resource block so in case if you want to change this name to 10 as teaching 1 or staging 2 or staging 3 then you don't need to manually go for each tag name and update the value you can just simply uh come over here in the locals and update the value and it will just update uh throughout your terraform file now before we apply our terraform configuration I just wanted to show you my terraform uh my AWS dashboard so that you can see like I am not running any previous ec2 instance here so here you can see instances are zero and I don't have any previously created VPC so you can just check that one also and here you can see there is a one VPC but I don't think I have created it's the default provided by the AWS and here you can see the list there is a one VPC running but the name is empty and this is like something which we get by default from AWS okay and similarly for subnet also we can check uh I'll just click on subnet over here and here everything has been created previously so I haven't created this subnet uh yeah so let's go back to uh my virtual studio and run some data from apply command before we run terraform apply command there are few command which we need to run and the first command is terraform init and once you run the terraform unit command it is going to download all the dependency for AWS environment now I have run the terraform minute command so I'll clear the screen the next command which you need to run is terraform plan [Music] and once you run the terraform plan command then you can see all the variables which we have declared will be replaced with its actual value so in the tag name if you look carefully here so here the name the variable name has been replaced with its actual value that is staging so now you can see the staging VPC tag over here so uh this is how uh we create a local variable and it is going to replace those variable name with its actual value so now I have run the era form plan command and it is going to add three resources because we are going to create VPC subnet and ec2 machine okay I'll clear the console over here and the next command which I'm gonna run is terraform apply [Music] and you need to supply value yes all right so now our terraform apply command has just finished let's go back to AWS dashboard and verify our resources now I am on AWS console and first of all I'm just going to verify my ec2 instance so I just click on ec2 and here you can see one instance is running so just click on one that one installs and here you can see our ec2 machine name that is staging terraform ec2 and if you go back to our terraform file then that's the name which we have assigned okay so here that is our ec2 instance which is running on our AWS console and the name is something over here terraform ec2 but before that we have used the variable name that is staging okay so I just wanted to recheck that thing okay so that is our ec2 instance next in which we need to verify is our VPC so click on VPC and here you can see our two vpcs are running one is by default provided by Amazon and the second one which we have just started and here you can see uh the VPC with the name staging VPC tag and if you go back to our terraform file then you can verify the same uh resource name which we have given inside our Dax so that is VPC tag and it is suffixed oh sorry it is prefixed with the name staging okay so that is our VPC instance and the next thing which we need to check is subnet so click on subnet and it should pop up over here okay so here it is so previously by default we have seen three subnet but here is the fourth one which we have just provisioned and that is staging subnet tag and that is also coming from here uh it should be yeah over here subnet tag and lastly once you are done with everything then we can destroy all the resources because I'm just using it for demo purpose so I just don't want to keep it running so I'm just gonna run the command terraform destroy and it is going to destroy all the resources which we have created using subnet VPC and ec2 instance I hope you liked the today's session on terraform locals in the next session we are gonna look about the terraform output values who can in today's session we are gonna see terraform output values first we are gonna see what is terraform output values and what's the concept behind it and secondly we are going to perform an actual demo on our AWS console now the question comes what is terraform output values so if you are previously familiar with any programming language then you might know like how to print any message on a console so let's take an example with a python so if you want to print a Hello message on a python then you are going to write like print and in the parenthesis you are going to write your message so it is going to be printed on your console and similarly if you have worked with the Java then you can just write down system.out.println and the message inside it and this message will be printed on your console in terraform we pretty much use the same concept and we call it as output values so here in the terraform file you are going to Define your output values and that could be defined using the keyword output and then you need to assign some name to it so here I am assigning the name my console output you can see over here I can highlight it here also so this is the name of my output values that is my console output and you can put any message inside the value okay so once you apply this configuration that means terraform apply once you run the terraform apply command then it is going to print this message onto your console so this message like hello this is output so this message you will get once you run the terraform apply command before we jump to the demo if you haven't seen my previous session where I have explained what is terraform and how to set up terraform on a Google Cloud as well as AWS and after that I have explained like what is terraform variables and what is variable dot TF TFR and command line variables and also the terraform locals so please go and check those sessions those are really useful to understand terraform to start the demo on the screen you can see on the right hand side this is my guide which I have prepared and in this guide I am I have mentioned all the codes which I'm going to use inside my terraform so feel free to copy that code if you are trying to learn the terraform output values and on the left hand side this is my visual studio code where I have written my terraform file okay so I'll put this link into the description section so feel free to use it okay so to start with uh the objective of this today's session is we need to perform the demo for output values so this is the output value which you want to which we want to print on our terminal okay this web message is pretty simple hello this is output this is the sample message which we want to print but before we uh actually run those data form apply terraform plan command first of all let's understand this main dot terraform file so here uh first of all the first section is provider since I'm performing this demo on AWS that's why I have used provider AWS secondly I am going to start an ec2 machine and virtual machine on my AWS instance that's why I have taken an AWS instance as a resource and the Machine ID because we need to mention a machine ID that you can copy it from AWS console and if you don't know then please check my part one where I have shown like how to copy this machine ID and the instance type which I am provisioning is T2 micro because we just want to use this for our demo purpose not for production use that's why I'm using very less CPU over here and the third thing the output value so this is how we declare our output variable or output value inside our terraform file so first of all you need to use keyword output after that you can assign any name this can be a meaningful name for your output values but keep some suitable name and for the demo purpose I have kept it as a my console output but you can change it as per your need and this is the message which I want to print over here okay so this is my overview of main.terraform file the First Command which I need to run over here is terraform init and that will download all the AWS dependency onto your local system and once you're done with the terraform unit command the second command which you need to run is terraform plan okay so uh my terraform plan command has just completed and here you can see on the console this is the output which I have got so hello this is the output so this is the first output which you got after using the terraform output values and you can change it also and I can type the message I have changed it okay that's done let's rerun the command I'll clear the terminal over here terraform plan and here you can see the message has been changed okay I'm not going to run my terraform apply Command right now because this is a very simple message which we wanted to print so terraform plan command is self-sufficient enough to print this kind of messages but in the next uh terraform file I'm going to run the terraform apply command where we are going to print the actual ID of our ec2 instance for that purpose we need to run our terraform apply command so that we can see the actual output okay but this is how you print a message on the console of our terraform here you can you can assume this as a hello world of power terraform moving further let's update this data from file and instead of printing some static messages we want to print some Dynamic values and the what dynamic value which I am talking over here is the public IP address of my ec2 machine so as you know we are trying to start an ec2 machine on AWS so once an ec2 machine starts on AWS machine then it gets an public IP address so we want to print that public IP address using this output values okay so if you move into the guide there is a section like how to print public IP of AWS instance so for that what you need to do is you need to update your output values with this so what you need to mention AWS instance first of all the keyword because uh if here you can see on the studio on my left hand side of my terminal so here this is the AWS instant that's the resource so you need to mention the exact same name over here after that you need to mention the name of your resource that is easy to underscore example because I have taken the name ec2 example you might take some other name so just replace that name over here and after that you need to put public IP that's the keyword which you need to mention so that you can get the public IP address of your ec2 instance okay I'm gonna copy this line This value line from here from my guide and I'm just gonna head over to my main. terraform file I'm just gonna remove this static line where we were printing hello so what I'll do I'll just paste that updated value okay so I'm just gonna save this file and on the terminal I'll first of all clear it since we have already run the terraform init command so you don't need to rerun it again so the next command which you can run is terraform plan okay so as I told you uh this is something uh Dynamic which we are trying to print onto our console but in the previously we have seen we have printed a static message and once we want to print a static message then we can see on our terminal as soon as we run the terraform plan command but here uh we are trying to print some Dynamic value that is public IP address of our ec2 machine and since we are running the terraform plan command so terraform plan command doesn't provision or doesn't start ec2 instance on AWS that's why this is like known after apply here you can see we are not getting any public IP address because terraform plan command doesn't actually start an ec2 instance on our AWS console so that's why we are getting a known after apply so the next thing which you need to do is you need to clear this terminal and the command which you need to run is terraform apply B like this terraform apply so it should take a minute or so and yeah we will get the public IP address after that you need to type in the value yes over here okay so now my ec2 machine has been started and here you can see the output my console output and the IP address which I am getting over here is 18 195 170.33 so that's the actual public IP address of my ec2 machine and now our ec2 machine has been started on my AWS let's head over to our AWS console and see whether this machine has been started or not so here uh on this browser you can see this is my AWS account and yeah I'll increase the size of this screen and we can just click on ec2 over here and here you can see one instance is running so just click on it and here you can see this is the ec2 instance which I have just started and the name is terraform ec2 and that you can check from here also so that's the same name tag which we have given over here and also you can verify the attributes and so if you scroll it horizontally so this is the public IP address if you can see 18 195 170.33 so that's the same IPA address we are getting over here so I'll keep it over here and you can see it over here probably this font is too small for you so I can increase the uh size of the screen so that you can see so now you can see the public IP address over here that is 18 195 170.33 and that's the same IP address we got over here as a console on our output values that is on our terminal all right so now we can destroy our ec2 instances we have seen like how to print our public IP using the output values so I'll just minimize this browser and I'll go back to my terminal I'll clear the screen and the command which I'm going to run is terraform destroy and it is going to destroy my ec2 instance so I'll be back once it's done as you can see destroy complete that means our ec2 installs has been destroyed and we can verify by going back to our AWS console and just refresh this and here you can see there is no ec2 instance which is running right now okay so now we know like how to use our output values and here this is the guide where I have mentioned all those things so you can just check this guide there's also one more important attribute actually uh that's called sensitive and once you put this sensitive flag to True over here so it will not print any sensitive information onto your terminal so let's say if you don't want to expose your public IP address or any password or any hostname from your staging or production environment then you can use this attribute along with your output values so it will just save you from printing those sensitive information onto the terminal okay so how to do that what I'll do is I'll just copy this sensitive equals true over here and I'll just increase this size and I'll just hit enter and after that what you need to do is I'll just remove this value I'll just gonna print some static values hello this is sensitive okay I just don't want to provision my ec2 instance again and again but we just wanted to see the sensitive flag okay so I'll just do clear that console and since I have already run the terraform init command so I don't need to run it the command which I'm gonna run is terraform plan because I'm printing the static value so I don't need to run terraform apply actually so I'm just gonna run terraform plan and here you can see once you run the terraform plan command with the sensitive equals true into your output values then it is not going to Output any values or it is not going to print any values onto your console so with this feature you can pretty much save your sensitive information from getting printed onto your console along with this today's session I'm just gonna share one guide with you and you can find all the command and the instructions which we are going to use throughout the session into this guide so I'll put the link of this guide into the description section so feel free to use it let's first start with the count but before we begin with the count let's understand some concept these four count and for each Loop work on a list variable or either map variable because a list or map contains more than one values so as you know whenever you are working with the for for each Loop or count in any programming language then you are working with the collections and the collection which we are talking about is either list map or SATs so here you can see this is the variable which we have defined and this variable is of typed list and that is the string so here the variable name which I have kept it over here is user underscore names the description which I have given is IM usernames in terraform you can put any uh meaningful name over here into the description section and the type is list and that is of type string and the three values which I have assigned over here that is user one user 2 and user 3. so uh when we are working with the count in terraform first of all you need to define the variable and this is the variable which we have defined is a type of list containing three users and if you see this diagram over here then this is how it is going to store the values so as you know the index position starts with the zero so in the zero you are going to store user one in one you are going to store user 2 and in two you are going to store user3 and this is just a normal thing okay so the next thing uh how we are going to use the count variable so here uh I'm going to create an IM user in AWS and in that resource we are going to first of all Define the count and here you can see we are going to use the length function and in the length function we are going to pass the variable user underscore name so if you look carefully over here this is the keyword which we have used that is where dot followed by your username variable so if you look carefully the username variable which we are going to use over here is the same which we have defined above so this is the username okay so first we have defined the variable and then we are going to create a resource that is IM user in AWS and inside that AWS user we are first of all going to count how many number of users are there and then we are going to use this uh feature of terraform where we are going to put count dot index and that will just iterate over the values that is user 1 user 2 and user 3. and it is going to create 3im user inside our AWS so as I shown you previously in the diagram like user the index position 0 1 and 2. so this count dot index will iterate over those index position that will start with 0 1 and 2 and it will put those values over here so once this uh configuration executes then it is going to create three w three AWS users inside our AWS console this is my terraform configuration for the demo of count and first of all we are going to define the provider yeah because we are going to work on AWS that's why we have kept the provider as AWS if you are going to work on Google Cloud then just you need to substitute the value over here with the Google cloud or azure but since we are working with the AWS so that's why I have used AWS region you need to select it based on your location and you can just skip like if you are in Asia region or America reason then please choose suitable reason which is close to you access key and secret key if you don't know how to use those access key in secret key and from where to get it then please uh see the part one of this terraform Series where I have shown like how to get the access key and secret key anyway I'll put that link in the description section of this video so you can follow that one also secondly uh we are going to create an AWS instance and that is going to be our virtual machine and the instance type is T2 dot micro because we are just starting it for demo purple that's why I just kept a very small machine and the tag name which we are going to assign is terraform ec2 uh it is just a user defined tag name you can keep an anything of your choice and this is the interesting part the IIM user because we are going to create a variable and then we are going to iterate that variable so that we can use the count so as I explained uh previously uh in the guide so this is the variable which I have declared over here that is usernames along with three value user one user 2 and user 3 and this is how I'm going to create IM user using the account let's move the terminal a little bit up and the First Command which we are going to run is terraform in it [Music] this command is going to download all the required dependencies which is needed to communicate with AWS and now we have completed the terraform init command I'll clear the screen the second command which I am going to run is terraform plan a terraform plan command what it does it actually tells you like how many resource it is going to add so here you can see it is telling us like four resources to add so that means we are going to create three users user one user to user 3 and the fourth resource is our AWS instance that is T2 dot micro so these are the four four resources which we are going to create but terraform plant does not create those resources into AWS it just tells you it's kind of a dry run so it just tells you like how many resources it is going to add it is going to change and it is going to destroy okay so now we have successfully run the terraform plan command the next command which are going to run is terraform apply [Music] and this command is going to create our resources and it will ask do you want to perform this section just simply type yes and hit enter and now we have completed the terraform apply command successfully and it tells you like four resources has been added let's go back to our AWS console and here you can see this is my AWS dashboard and I am into the section I am dashboard because we have created three aim users and we just wanted to verify whether those three users has been created or not so before running the terraform apply command I had a zero user so now I have successfully executed my terraform apply command so I'm just going to refresh this page and here you can see three users and we can verify those three users also so user one user 2 and user 3. so these are the three resources which we have created uh using our terraform configuration uh we just wanted to verify our fourth resource that is our ec2 instance and just type and click on here and here you can see uh one instance is running click here and here you can see uh this is this is our fourth resource which we wanted to verify that is terraform ec2 and if you go back to our terraform configuration so here you can verify the tag name also that is terraform ec2 okay so this is how we are going to create our IM user using the count of terraform now we have seen the count example and the demo I'm just gonna destroy all the resources and I'll just clear it first and the command for that is terraform destroy so it will take some time so I'll be back when terraform destroy command has been completed as you can see now we have destroyed our all the four resources let's head back to our guide and now we have seen a loop with the count in terraform the next topic which we are going to discuss is the for each Loop so click over here and again I'm going to take the same example and in this example again I'm going to create the some same three users user one user 2 and user three the only thing which going to change over here is the resources of a AWS IM user so previously we have used count over here but this time we are going to use use for each so here you can see uh we have to mention the keyword for each along with the variable keyword and our username variable sorry yeah so we need to use this user underscore names variable which we have created over here as you can see over here and after that you just simply need to type the each dot value so this each dot value is going to iterate over each value of user that is user one user 2 and user 3. and it is going to uh put it over here under the name so this for each Loop is little bit simpler than the count but this is how we are going to perform it so what you need to do over here is you just simply need to copy this for each part of this terraform script and here is the complete terraform script if you wanna see over here but I'll go back to my virtual Studio and here I'm just gonna remove this resource where we have used count I'm just gonna remove it and I'm just gonna paste the for each Loop and I'm just gonna save it okay all right so I'll increase the terminal size over here I'll clear the screen and now we have already run the terraform init command so you don't need to run it again and again the next command we I'm gonna run is terraform plan just to verify whether there is any problem or the re and there is any syntactical mistake with my uh this this terraform configuration oh I forgot to mention one important concept regarding the for each Loop you cannot use list over here uh so this is how a terraform plan command really helps you to identify the issues before running the terraform apply command so here uh with the for each Loop you cannot use list you need to use SATA or map uh you can see the same instruction over here in the guide I have already mentioned I think this is the set you need to use you don't you cannot use list over here so that's the thing you need to change and let's go back to my visual studio and I'll clear the screen again and I'm just going to run the terraform plan command again and now we are able to execute it successfully and again it is showing four to add so now what I'm gonna do I'm just gonna run the terraform plan command actually no sorry terraform apply command so I'm just gonna run terraform apply it one all right so now we have successfully completed our terraform apply command and it says four resource has been added so let's go back to our AWS console just refresh this page and here you can see three users has been created and these are the three users one two and three and similarly we can verify the ec2 instance also and here you can see our one ec2 instance is ready yeah it's up and running so now we have seen like how to use the for each inside your terraform configuration and remember to use set instead of list either you can use that or map for to work with the for each Loop inside your terraform configuration so I'll just destroy this terraform configuration and I'll be back when the terraform destroy has finished now my terraform destroy has just finished I'll clear the screen over here let's head back to our guide and the next topic which we are going to discuss over here is the for Loop so here in the for loop again I am going to take the same variable example where I'm going to create the three users user 1 user 2 and user 3. but this time I'm not going to create our actual AWS user instead I'm just going to use the output functionality of terraform configuration if you haven't checked the what output variables are in terraform then please go and check I'll put the card over here so that you can just check how to create a output variable Center form so instead of applying and running to styrofoam configuration we are just gonna see how to print those values on to our console okay so here uh I'm just going to copy this section because we have already created the variable inside matter of form file so I'll go back to my terraform configuration file and here I'm just gonna remove this resource and paste this one over here this output variable so it is just simply going to print user1 user to and user tree so here I'm just going to use the for Loop instead of for each Loop okay so what I'm gonna do I'm just simply Gonna Save this file and then I'm gonna run the terraform uh plan command I'll just to increase the size of this terminal but you already know the drill like how to run the terraform plan and terraform apply command if you actually want to perform this step onto AWS but instead I'm just simply going to run the data from plant command [Music] and here you can see this is the output which I got back that changes to the output is print name and this is the very output variable name which I have taken print the names and here you can see the output user1 user 2 and user trade so uh I just wanted to show you this concept of for Loop also instead of running it on an actual AWS because we have seen the previous count and for each by implementing on AWS but this is how you are going to implement the for loop the loop is pretty much simple this is the variable name and that is user underscore name and this is how you create the for Loop inside your terraform configuration [Music] and in today's session we are gonna talk about terraform state so the topics for today's discussion is what is terraform State and how to use terraform State inside your terraform projects apart from that we are also gonna look how to manage your terraform State and what are the best practices and along with that we are also going to look on like what is terraform local state and what is terraform remote State and to demonstrate that we are gonna see a demo where we are gonna push a local terraform State into AWS S3 bucket if you are new to this Channel and if you are just getting started with the terraform then I'll put the link of my previous session which I have uploaded for terraform into the description section so feel free to go through those sessions so that you can understand those Concepts really well all right let's switch over to my desktop the first question comes in my mind is like how the terraform State file is created or do we need to create a terraform State file so to answer that like you can see in this diagram on the left hand side we have a terraform plan command and apart from that you can also see there are terraform unit and terraform apply command which we generally use so terraform State file is not created by us but instead it is created by terraform so whenever you execute terraform plan on the left hand side you can see or terraform apply command and this terraform State file is created so in the middle you can see in the red color this is my terraform straight file and it contains all the resource information in the form of metadata and this terraform State file is created onto your local workspace so it is created by terraform not by you and it is created along with your project onto your local workspace and after that all the changes if you have executed terraform apply command then first it will create a terraform State file and then after it will apply all the changes to your AWS infrastructure or a Google Cloud infrastructure in case you are using Google or in case if you are using Azure then it will apply it to Azure so this is the typical flow of our terraform apply or terraform plan command and whenever you execute these commands then this terraform State file is created by terraform and it just happens like in the back end you don't see it but we can actually see the terraform State file now we know the basics of terraform State file let's see a little demo where we are going to create a terraform State file so this is my workspace and I'll put the link of this workspace into the description section it's a GitHub project so I'll put the GitHub link so this is my terraform State file uh project which I have created it's a very small one so here I have defined the provider with the reason access key and secret key and this is my variable where I'm storing the access key and secret key but this is my main terraform file where I'm just creating an ec2 instance of T2 micro onto my AWS account so this is the project terraform remote State and here I have only couple of file main.tf and variable.tf so in this project this is a fresh project I haven't done anything yet so once I'll issue the terraform uh plan or terraform apply command then we are going to create a terraform Street file not V but terraform is going to create a terraform State file so that's the first step that how the terraform State file is created let's open the terminal from here go to new terminal and I'll just extend it over here clear the screen and let's check the directory so we should go into terraform remote state project I'll clear the screen and I'll also issue the command export PS1 so that I can reduce the length of this command line it's too annoying sometimes let's put a dollar okay clear so now uh this is my terminal and this is my project and I'm inside the directory Tera from remote state so the First Command which I'm gonna run is terraform init you might have noticed that once I issued the terraform need command I am in this command it has downloaded uh some dependencies so if you look into the terraform file this is our provider and the provider is AWS so whenever we issue terraform init command then it downloads all the dependency which is needed for AWS so here it has created a directory.terraform and inside that you will find a file which is which has been downloaded by terraform init command so that's the first step once we issue the terraform init command but still we haven't created or terraform haven't created a terraform State file so I'll clear the screen over here and the next command which we are going to issue is terraform plan command okay so terraform plan command says it's going to create or add one resource so after that we are going to create or we are going to create the resource using the next command that is uh terraform apply type yes okay so now terraform apply command has just finished and we have successfully started and easy to install and now where is the terraform State file so here you can see uh this is the terraform State file which has been created so once you issue your final terraform apply command and this terraform State file is being created and this is done by terraform it is not that we are creating this terraform State file but it is just by default created by terraform and it contains all the metadata information which is like which we have is specified into the main.pf file so this is the information related to the current main.tf file so here you can see this is the Ami which we are planning to or which we are starting to create on AWS and this is the ec2 which is running on EU central region so there are lots of information which is available inside this terraform State file and this is just created by terraform by itself so this is the terraform State file over here and this terraform State file accessed locally onto your terraform workspace so that's why this terraform State file is known as local terraform State file because it is just local to you you have not shared with any of your colleague or any of your team member so that's why we call this a terraform State file as a local terraform Street file so what happens is like whenever you execute any command onto your terraform project so I'll clear the screen so let's say if I execute terraform destroy command then again it is going to update the terraform State file because terraform State file is just like a recorder or a black box or a terraform where it records all the steps which we are going to perform so the first thing which we need to do is to create a terraform State file is you need to execute terraform apply command so once you execute the terraform apply command this terraform State file will be created if this is your fresh project so this was my first fresh project that's why I have it has created a from TF State file now after that after creation of the terraform State file whenever I execute any new command so let's say I have executed terraform destroy command so it is simply gonna update the terraform Street file because it is just updating the meta information related to my main.tf file so that's why for each step it's act as a recorder so it's just like a black box for this terraform project and it's Gonna Keep on recording whatever happening inside my terraform project so if you are updating deleting or destroying anything then it will just keep the information into the terraform State file and also it is going to apply on actual AWS Cloud infrastructure also all right so now I have destroyed my terraform uh resource or ec2 resource which is what's running on ec2 oh sorry on AWS so let's see the terraform State file once again and verify our uh changes so here this is my terraform State file and as you can see it has like it has shrunken quite a lot now it contains very limited information so previously uh before issuing the terraform destroy command it has a lot of information because during that time we were trying to create a easy to instance onto AWS but now we have destroyed it so it has removed all the information uh related to our ec2 instance which we were which we have created previously so now you can say like a terraform State file is updated every time whenever you issue or do any kind of activity and activity what it's mean is like a terraform apply terraform destroy I mean anything which you do on your resources using terraform all right so that was the first part where we have seen how to create create a terraform State file and how to create and store that terraform State file locally onto your workspace the next part which we are gonna see like how to store the same file remotely onto S3 bucket so we are going to create an S3 bucket and inside that S3 bucket we are gonna store this terraform State file and that all we are gonna do with the same terraform script now we are not going to do anything manually everything we are going to do we are going to create that terraform State file and we are going to store the terraform State file onto S3 bucket via terraform all right so this is my uh the same main.tf file and previously I have disabled the remote section of my terraform script so where I have mentioned like what is the backend that is S3 so I'm just gonna enable this section because previously we were only interested in creating a local terraform State file so I'm just gonna save it and here there are a couple of things which I would like to mention so whenever you're trying to store the state file remotely on the S3 bucket then you need to define the resource into the terraform main.tfy so here we have defined the block that's terraform and that block is backend and it is using the S3 bucket and then you need to specify the name of the bucket so this is the bucket which I'll I'll be creating on my AWS account and this is the key so this key is little special so assume this as a a directory inside a S3 bucket so how do you store a file inside your computer so what you do you just create a directory and inside the directory you store some file so similarly this is the bucket and inside that bucket we are going to create a key which we call it but it's it's assumed that it's a directory and inside the directory we are just gonna store the terraform State file and apart from that we also need to mention the reason so in which reason you are working so in which region you are going to create this S3 bucket so again we are just gonna create it into the same reason that is EU Central One all right so now we have enabled this uh just save it and also take a look at this terraform Street file I think it should be yeah because we have previously destroyed uh all the resources which we have created uh that's why this file is quite shrunken a lot uh one more thing uh we need to create this S3 bucket uh before running this uh terraform apply command or before running or executing this main.tf file so this is my I can show you my AWS account so this is my AWS account and here I have already created an S3 bucket so the name should be same otherwise you might get into some error and the creation of S3 bucket is pretty simple you can simply go here and type S3 and it will give you an option for S3 and then once you click on this S3 then it will redirect you to this home page and here you can just click on this orange button that is create bucket and it will help you to create a bucket so this is how I have already created an s bucket and after that this is my terraform project I think yeah this is the one so now I'll clear the screen over here and I'm simply gonna execute the command terraform apply ah it's giving me an error back-end initialization required run terraform in it so I'm just gonna run the terraform unit command once again so error form you need because we have previously destroyed all the resources that's why it is asking me to rerun the terraform I need command okay then I can run the Terra form client command also just to see okay it is going to create a one or add one resource that's fine I'll clear the screen and then I'm just gonna run the terraform apply come on type yes now my terraform apply command has just finished and we can first see the terraform State file and I'll just open it maybe okay ah sorry I forgot to mention yes we are creating this uh remote terraform State file that's why it has not updated this local state file so let's get back to our AWS console over here and I'm just gonna refresh it uh click on this bucket and here you can see this is the key this is the directory which I mentioned you earlier so here uh this is the bucket uh this is the key so key is just like a folder inside your bucket and inside that the folder it is going to create a terraform PF State file so this is the folder click on it and here you will find terraform State file so this is the terraform State file which we have stored remotely onto our S3 bucket so this is how you are gonna create a remote terraform State file Where We Are Gonna Save the same terraform State file instead of locally we are gonna store it remotely so this is how you create a terraform remote State file now the question comes like if you are having a terraform State file locally then why do you need to store the terraform State file remote so consider a scenario you are working in a company where lots of developer are working on a same terraform project and as you know if you have messed up your terraform State file then you are not able to execute any of your command because your terraform State file is not properly synced so if multiple developers are working on the same terraform file like in our case we had created a main.tf file that is our main dot data file terraform file so if there are more than one developer is working they then you cannot have a two-state file of terraform so you need to have a single copy of a terraform State file so that's why you need to store the terraform State file remotely and but that means you need to store somewhere so that's why we use S3 bucket because uh AWS has provided a really good support or terraform has a really good support for S3 bucket so this is how you're gonna share this terraform State file with other fellow developers so that they are also in sync with the same terraform State file now we have discussed the importance of a terraform remote State file when we are working with a multiple developers or you're working in a team then next question comes like if you have taken some days off and you are not working on your project and how you're gonna pull the latest changes which has happened in your absence and for that terraform provides one more feature that is terraform State pull command and with the help of this pull command you can pull all the latest changes which has happened in your absence and you can get those information onto your local workspace of your terraform so terraform State pull command is a very similar command to the SVN pull or git pull command so with the help of SVN pull or git pull command you bring all the latest changes which has been done by other teams onto your local system so that your code is up to date so similarly terraform State pull command will help you to update your local terraform State information so let's head back to desktop again and see how to use that command this is my workspace and the command which I'm gonna run is terraform straight pull and remember we have already started our ec2 instance and we have already stored the state file remotely so here you can see it has pulled all the information related to terraform State file which we have stored remotely so that contains all the information about our ec2 instance and that you can verify from here if you look here then this is the Ami and this is the ec2 instance that is running on an EU Central and you can copy the Ami just in case I'll just scroll it down again over here copy it from here go to your main.tf file and Ctrl F and find it so here you can see so this is how you can pull the information back which has been stored on a terraform straight file using the terraform State pull command and similarly uh if you follow this guide which I'll also share into the description section so here you will find a terraform State push command also but this terraform State push command is the really uh like not a good practice because you might destroy some of the other team member changes because terraform State push gonna update the transform State file and once you update the terraform State file without knowing other team member changes or without knowing the gravity of the situation then it might destroy quite a lot of thing so terraform provides a terraform stat push command but it is not recommended to use it so but just to know you that terraform State push command is also uh testing there so you can use the terraform State push command also so this is the guide so whatever I have discussed during this session is available onto this guide so you can follow this guy guide whenever you feel [Music] so in this session we are going to discuss all these three provisioners along with the demo apart from these three provisioners there are some vendor specific provisioner that is Chef habitat puppet and salt masterless and these uh vendor specific provisioner requires little bit of special setup where you have shap and puppet running onto your system but those are the topics for some future discussion but in today's session we are just gonna focus on these three provisioners along with the today's session I'm just gonna share this guide with you and this guide will contain all the examples which we are going to cover into this session so let's take an example like a file provision so here you will find all the code snippet for the terraform script which we are going to use for the demo and similarly you can use the same guide for local as well as the remote exe provisioners here so here uh the only thing which you need to change over here is the access key and the secret key because that differs for each AWS account so I'll put the link of this guide into the description section so feel free to use it all right let's start with our first provisioner that is our file provisioner and as you can see the purpose of the file provisioner is to copy a file from our local development machine that we call it as a source to the destination and the destination is going to be our Cloud so here I'm using the AWS ec2 instance that is remote machine and that is going to be my final destination and here I am trying to copying the file test file.txt so this is the file which I want to copy so basically file provisional help you to transfer a file from a source to the destination and here at the bottom you can see this is the code snippet for our file provisioner so it starts with the keyword provisioner and then you need to mention like which kind of a provisioner it is so here I am using the file provisioner that's why I have mentioned the keyword as a file and then we need to mention the source so Source here it contains my local directory or the local hard disk where I have stored my file that is test file.txt so this is the location where I have stored my file and this is the destination so destination is going to be our AWS ec2 instance so what it's going to do it's going to copy the test file.txt to this path home Ubuntu test file.pxt so this is the code snippet which we are going to use for our file provisioner and here is my terraform script for file provisioner to start with the first block this is my provider block that is AWS and here we need to mention the reason access key and the secret key since I am working in the Euro prison that's why I have used EU Central along with that you need to mention the access key and the secret key and if you don't know how to fetch the access key in the secret key then please do follow my part one where I have shown like how to fetch all those information to work with the AWS along with that we need to mention the resource block and as you know in the previous slide I have mentioned like we need to start a virtual machine and that machine is going to be our ec2 install so that's why we need to create this resource block which will start a virtual machine running inside our AWS cloud so here we need to mention some details and uh as I have mentioned everything I have explained like how to start your ec2 instance in the part one so if you don't know anything uh or if you feel like it is too complicated then do I do recommend to follow the part one but anyway you need to mention the Ami the machine ID which we are going to start since I am starting Ubuntu machine that's why I have used the Ami of the Ubuntu machine and then secondly you need to mention the CPU or the machine type so since this is an example that's why I have used 32.micro uh if you are using it for some other purpose then you can use some powerful CPUs and then we need to mention the key uh for SSH I'll explain uh when we are going to perform the demo and where when we are going to move to the AWS and we are going to create this key also and for the same key we need to create a security group also that's why I have created this Security Group I'm going to show you that one also and the main thing that is a provisioner that is file and this is my provisioner where I have mentioned the source and the destination so this is the source where my file is located and this is the destination where I'm going to copy my file and this destination is going to be my ec2 machine apart from the provisional we need to mention the connection block also in the resource block just if you'll see carefully then I'm still inside my resource block so here I need to mention the connection block also and if you have worked with the Linux server then you need to copy the files securely that's why you need to use SSH and that's why we are using the connection block over here so that we can copy our files securely and this provisional is going to use this connection block to make an SSH connection and copy that file securely moving to the next block uh that is AWS Security Group so we need to Define some increase and egress resources over here so egress resource is going to be the default one where we you only need to mention the cider you can read about the cider block on the wiki or Google it it's very simple but we don't have to Deep dive into those details but there is an increase block which you need to pay a little bit more attention here you can see the port Is 2020 sorry 22 and here we are using the protocol as a TCP so if you have worked with the Linux servers then we need to SSH and when we want to SSH into then we are going to use the port that is 22. so that's why we need to Define an inbound Port that is 22 which needs to be open on our ec2 machine that's why we need to create this uh egress resource or egress block here inside our terraform script which is going to open that port and we can securely copy our file uh this test file.txt to our remote machine and the last block is we need to define the AWS key pair and as you know we have created a AWS key and we have used the same reference of AWS key from here into our resource block you can see over here also so this is the same AWS key we are going to use so here you need to mention the public key we are going to create a key pair where we are going to create a private as well as the public key but here we need to use only the public key and private key we are going to keep it on our local development machine so I'll show you how to create those key pair but just to keep in mind we need to create the key pair and we need to put the public key only the public part over here public key part the next thing which we need to do is we need to create a AWS key there and we are going to put the name AWS underscore key so if you head over to my blog post in the terraform professional so here I have mentioned the link of how to SSH into AWS ec2 instance and once you click over here then it will open this guide where I have shown like how to create a key pair so you need to use the SSH keys and utility to create our public and private key pair so simply copy this command from here and go to your terminal and here you need to Simply paste the command and hit enter by default is going to create a SSH keypad at this location but I don't want to create my key pair at this location so I what I'll do I'll just mention my own custom part where I want to the my key pair file to be so if you follow along this guide then you need to you can copy the path uh but this path can be different for you because your development machine will be different from mine so you can use your own local development machine path so okay so I'm just gonna paste it over here but since I have already created this AWS key uh on my system so I'm gonna simply create a one more key that is AWS underscore key one and hit enter and you don't need to mention any password phrase so simply keep enter then again enter and it is going to create a SSH key gen or the key pair that is public as well as the private key to verify that our key has been generated go to the location where you have created the keys and this is the location which I have mentioned and here you will see we have created key pair the first is the private key AWS key underscore keyword and this is my public key AWS underscore keyword dot Pub and we are going to use this public key inside our terraform script and this private key which doesn't have any kind of extension so this is the private key which we are going to use for SSH purpose so that we can login into our AWS machine once we start our AWS machine okay so what you need to do is you need to open with any editor and here you will find the content of our public key so this is the public key which I have generated along with my privacy so you simply need to copy this content go back to your terraform script which should be here and you simply need to paste that content over here so this is my terraform this is my public keyword here so you just simply need to replace this public key and that should be the the key pair part and always remember to put the correct key name over here and always put the correct key name uh with which you have created the public and the private key so if you have created a key with the AWS key one then please put the same name over here and apart from that you just simply need to copy this code of the security group you can copy as it is you don't need to change anything over here so you can just simply copy this e-grace and increase Resource as it is it is not going to change for you also because you are going to again use the same 22 port for SSH into and for TCP also all right now we have done all the prerequisites which is needed to perform an example of our file provisioner so now we need to execute our terraform script so the first thing which you need to do is you need to run the command terraform in it and once you run the terraform you need command and it completed successfully then the next command which we're gonna run is the terraform plan and this command is going to tell you like how many resource we are going to add inside our AWS so here you can see it is going to tell you like we are going to create the three resources or it is going to add a three resources so the three resources which we are talking about is is this ec2 instance along with that we need to create a security group and also we need we are going to create a key there so these three resources are going to be added so that's why this count is three once you successfully run the terraform plan command the next command which you are going to run is terraform apply and then you need to Simply type yes I want to apply those changes all right here you can see it has applied those three changes successfully let's head back to our AWS console and see our ec2 instance has been created or not so click on ec2 and here you can see there is a one instance which is running click on it and here we need to click on the instance ID click on connect and this is the command which we are going to use to SSH into our AWS AWS ec2 instance but before that we need to remember that we need to change the key uh permissions also since as you know we have created AWS key one but that I have shown you just for example purpose how you need to create but still I'm using the AWS key I'm not using the AWS key one I'm still using my AWS key which I have created previously so before you run or before you create uh before you connect to our AWS instance using this SSH command you need to change the permission by default what happens is it might have assigned some additional permission it might have like a read write access to other users also so just simply go and copy this command and execute this command so you need to go to terminal once again and here this is my terminal and what you need to do is you need to go to the same path so this is the location where I have created my keys so I'm just gonna use the same path place the path oh sorry I need to remove this key name also clear it and here I can see I have created a AWS key one okay and this is my public key AWS key dot Pub so these are the two keys but we need to change the permission of our private key this is the key which we need to change the permission for so by default uh like AWS says it has a DOT Prime extension but you don't need to worry about it you simply need to copy this command go to your terminal paste the command over here and just remove the dot plane extension and hit enter so it will change the permission and once you change the permission and then you are going to copy this or you're going to execute this SSH command from here so simply copy this SSH command and paste it over here into the terminal and one thing which you need to change over here is we need to remove that dot time extension because we have created a private file without any extension so that's why I'm just going to remove the team extension from here and simply hit enter if you are doing it for the first time then it will ask you for some do you want to trust this resource then you just simply need to type the yes so once you type the yes and then hit enter 10 you will be into your virtual easy to install so here you can see this is my ec2 instance which is up and running I'll clear the screen and here you can see this is my IP that is of my Ubuntu machine and same thing you can verify over here also into the ec2 instance click here again go to this instance ID and here these are my details of my ec2 instance and you can verify with the private ip4 address also so the address is 172.31 uh I will put it over here so 172 to 31 5 and 10. so this is the 172 31.5 and 101 sorry so this is my ec2 instance which I am able to connect it uh the next thing which you need to verify is that we have copied the file successfully or not so simply type Alice and here you can see the file is test file.txt which we have copied so our terraform file our terraform script has executed successfully and we are able to copy the file um see over here so this is my source file from here that is TST file.txt and we are able to copy it to our destination directory so here we can see this is my terminal and if I can do that unless so yeah this is the final we can do the content of the file also so this is my test file the content of the file is so this is how we work with the provisioner that is file provisioner and by this way you can simply copy the file from your local machine to your destination all right to summarize our first part of provision what we have done so far is we have created our ec2 installs that is our virtual machine and that is going to be our destination so that we have started and also we have created a public key in the private key we have used the public key inside our terraform script and we have used the private key to SSH into our terraform uh the instance which we have created using terraform and also we have seen how the provisioner works and how we can create our first file provisioner so all the concepts like public key private key ec2 instance is going to be the same for other provisional that is local and the remote provisioner and only the provisioner block we are going to change inside our terraform Square so let's head over and see our next provisioner that is local as the name suggests this is a local exe provisioner so it is going to work on only on your local machine and that is local development machine so here on the left hand side you can see this is my source machine that is local development machine and on the right hand side this is my AWS ec2 instance which is running on AWS Cloud so the local provision only affects your local development machine so this is the provisional block which I have written so here again you need to mention the provisional keyword and then you need to mention the name of provision that is local local dash exec the actual provisional name and then you need to mention the command which you are going to execute on your local machine so here the provisioner is pretty simple you need to mention the bash command or the Unix command so here I am just trying to create a file so the file name is touch the file name is hello j-hope txt and the command which I'm going to use is patch so touch command is pretty simple in the Linux and it is going to create one empty file and that is hello joh.txt here is my terraform script for local provisioner and this is the provider and this is the resource section and in the resource section we need to create a provisioner and we need to put the name so that is local exec provisioner and here we need to mention the command which we are going to execute on our local machine so here I am going to create a file that is hello J hook.txt so once you create this data from script you simply need to run the command terraform in it and after that you need to run the command terraform plan and before we running the before running the command terraform apply just see here we don't have anything file named hello J hook.txt over here so we don't have that file so once we execute the terraform apply command then it is going to create that file over here so simply run the terraform apply and type yes and once terraform apply has completed successfully here you can see this is the hello Dash J hook txt file has been created it has been created on the local machine it has nothing to do with the remote instance which we have started so this is how our local provisioner works with the terraform all right so till now what we have seen we have seen the file provisioner for transferring the file we have seen the local provisioner for performing some bash script or executing some shell commands on our local development machine and the next provisional which we are going to talk about is the remote exec provision or a remote provisioner so as the name suggests we are going to perform some action on a remote machine running on a cloud so if we are talking about AWS then we can use the remote provisioner to perform some or to execution shell commands on running on our AWS Cloud so in our instance we will be executing sub commands on our ec2 machine which we have running inside our AWS cloud and similarly for Google it will be virtual machine running inside our Google machine Google cloud and we will be executing some command or shell commands using our terraform remote provisioners so let's see what is the remote provisioners are here you can see on the left hand side this is my local development machine and on the right hand side it is my AWS ec2 install so here on the local development machine we will be executing our terraform script and Below you will find a provisioner script for a provisional code for terraform remote provisioner and here the keyword is provisioned again and the name of the provisioner you need to type in is the remote exec that's the name of our remote provisioner and here there are some commands which we want to execute on our remote machine so how you need to Define is you need to put inline that is keyword provided by terraform and then you need to uh put some Square brushes and inside that you need to put some double quotes and there you can write some command so here what I'm doing is I'm just trying to create one hello.txt file in my first command and then I'm just trying to write some text so I'm just putting this hello world remote provisioner into hello.txt so I'm executing two commands using my remote provisioner so by this way you can execute multiple commands so if you want to add further more commands then you can just put a comma and double quotes and you can add a new more line with your bash Scripts so this is how you define a remote provisioner and once we execute this remote provisioner or once we put this remote provision inside our terraform script then it is going to execute this or create this file and write some text and put it inside our AWS ec2 install so this is how uh our remote provision is column board here you can see this is my terraform script for remote provisioner and as usual we need to Define some provider after that we need to Define some resource over here and again I am using the same t2.micro and I'm using the ec2 instance with this Ami so I'm using the Ubuntu as a base operating system for starting my virtual machine in the same AWS key which we have used in our file provisioner so the only part which is changed over here is this one that is our provisional that is remote exec so here I have added this inline character and along with that I have added the touch hello text so it is going to create an empty hello text file and inside that file I'm just going to write this line that is Hello World remote provisioner so this is how our remote provisioner script of terraform is gonna look like everything is else is pretty much same and on the only part which you have changed is only this provisioner to start with our first command so the first command is terraform unit okay so now we have finished our data for a minute so the next command which we are going to run is terraform plan okay here you can see uh three to add that means it's going to create a three resources that is ec2 and Security Group and after that it is going to create a key pair okay so these are the 3D source which is being created and the final command which we're gonna run is type yes okay so now our terraform apply command has completed successfully let's go to AWS console and verify the details so here you can see this is my AWS dashboard so there are zero instance running but I need to refresh the speech and here you can see now we have one instance so click on this instances okay so now the beat has been refreshed completely and here you can see the instance is running so I need to click on this instance ID and click on connect and from here I can copy the SSH command of my ec2 instance so copy the command go back to the terminal and just go to the directory where your key file is so the my key file is AWS key and for this demo I have just shown you like how to create a AWS keyword but I will still use my old key so this is the directory where my key file is so I'm just gonna simply paste my command over here and always remember to remove this dot pane extension we don't need it okay and just hit enter and here you can see we are now logged in into our ec2 machine I'll clear the screen so that you can see probably I can increase the font also so it will be easy for you to see and simply run the command Alice and here you can see our hello.txt file which has been created we can verify the content of this hello.txt and this is the content hello world remote provisioner so this is the content which we wanted to write and we can look back to our terraform script once again so this is our remote provisioner here here it is so this is the first command which is going to create a hello.txt and which we have verified over here so if I simply type the ls command again so this is my hello.txt and the content which you want to write over here is the hello remote provisioner so I can I get the same content which we wanted to show onto our file so this is how you can create a remote provisioner and you can complete your remote provisional script with this blog with that we have concluded all of our three provisional that is file local and remote provisional provided by terraform and all the code which I have used into the demo will be available on my blog post so I'll put the link of my blog post into the description section of this video and there is a one recommendation which I would like to tell you like it is not recommended to use provisioner too much into your terraform project because you will be using quite a lot of or you will be introducing quite a lot of bash scripting to your terraform so that is not recommended so try to avoid or try to use less of a provisional inside your terraform module and in today's session we are gonna talk about terraform modules the today's session has been divided into two part in the first part we are going to see what are the basics behind terraform modules and how we can create a terraform module in the second part we are going to perform a demo where we are going to create couple of modules and we are going to deploy those two modules onto AWS to start with our first part let's take an example to understand the terraform module because if we'll go with the official documentation and official definition of a terraform modules then it will be really hard to understand what the terraform modules are so here in this example you can see our developer is working on this terraform script which will be responsible for installing Apache HTTP server there is a one more developer in the same team who is also working on the terraform script but that terraform script is responsible for managing the e-commerce application so that terraform script is completely different and that is used for provisioning the infrastructure which is needed for an e-commerce application with the time there is a need for installing Apache HTTP server alongside with the e-commerce application so here developer 2 has its own terraform script but into the terraform script he has the infrastructure setup only for e-commerce application there is no instruction which is provided for installing Apache HTTP server but on the other hand developer one has a terraform script which is responsible for only installing Apache HTTP server so is there a way our developer too can borrow the terraform script from developer1 so that he can install Apache HTTP server so there comes the terraform module so what we can do over here is we can uh create a module in which we can Define how to install HTTP server and we can we can import that module into the developer 2 terraform script so what will happen with it is we can reuse the functionality of installing Apache HTTP server and we can use the terraform script inside our developer to terraform script so with that what will happen is you don't need to rewrite the whole function or the whole terraform script for installing Apache to http server and we can just reuse this complete terraform script as a module for developer 2. all right now we have seen an example and now we got an idea like how terraform module Works let's head over to demo and in the demo what we are going to do we are going to create a couple of modules and we are going to name them as a module 1 and module 2 and we are going to reference both the module inside our main.tf that is going to be our main terraform file so once we execute or once we applied the using the terraform apply command then both the module is going to be executed and whatever code or whatever terraform infrastructure code written into that module is going to be applied so for this case we are going to apply this on AWS all right here you can see this is my terraform module projects and once I open this terraform module project then you will notice there are couple of module which I have created so here this is module 1 and this is module 2. along with that you will also notice there is a main.tf that is going to be your parent TF or parent terraform file so here first of all you should notice over here is that we are referencing module 1 as well as module 2. so how to reference a module or how to call a module so first of all what you need to do is you need to create this man.terraform file and into that file you need to First Define the provider since we are going to work on AWS that's why I have defined the provider as AWS apart from that you need to provide the reason access key and the secret key if you don't know how to fetch this reason access key and secret key then I would recommend you to follow the part one where I have shown like how you can create an ec2 instance and how you can fetch this information like a reason access key and secret key so I would highly recommend to follow that part one all right now we have defined the provider the second thing what you need to do is you need to call a module so here uh the first keyword which you need to put over here is module apart from that you need to define the name of the module so this module name can be user defined so you can put any name or any type of name of your choice for the module so here I have put in the name as J hook web server one because both them in both the module we are going to install Apache HTTP server moving further this is my second module and here you can see again I have used the keyword as module and I have kept a name as J hook web server too and apart from that we have defined as a source so here this is the source and here we need to define the path of the module from where it is going to pick the module so if you look carefully over here so this is my main.tf and just parallel old into the same hierarchy you can find the module one as well as the module two that's why the path is relative over here followed by I think it's a forward slash so these are the forward slash we are going to use to reference the module one as well as the module 2. along with the today's session I'm just gonna share this guide this is my blog post where I have taken the same example which I have just explained to you so here you will find all the description along with the terraform script which I have used into this demo so I'll put the link of this guide into the description section of this video so feel free to use this guide and you will find this terraform script over here so this is the terraform script for module one which you can copy from here and this is the terraform script for module 2. so this is also available here so if you need to make some kind of a changes then please feel free to make and make it to your own use now we have seen the complete structure of our terraform module project along with the module 1 and module 2 and with our parent main.tf file let's head over to module 1 and see what's what are the content inside our module one so if you open this module one directory then you will find a main.tf output.tf and variables.tf so you can easily resemble this structure with a similar terraform project so in a typical terraform project you will have a main.tf output.tfnvariables.pf so module is just similar to a separate workspace or a separate terraform project where it has its own main.tf file so this is going to be our first main.tf file of our module one so let's open the main.tf this main.tf file contains the code for installing Apache 2 server onto our ec2 instance so if you look carefully this is the bash script which is needed for installing Apache 2 server onto our ec2 instance but let's start from the first block so this is going to be the first block which we need to Define and we need to define the required version which is needed for this setup so we will be using the version 0.12 secondly we need to create an easy to install so we need to Define it as a resource and resource name is AWS instance and here uh the name of AWS instance so it can be anything of your choice just put a suitable name over here then secondly we need to put the machine ID and here I have taken the variable that is Ami underscore ID and the variable value I have defined inside my variable.tf so this is the Ami ID which I'll be using for my ec2 instance after that we need to define the instance type and that also I have defined into the variable so if you go into the variable.tf then you will find the instance type which I am using is T2 dot micro so this is just for an example purpose that's why I'm using the t2.micro but in case if you are working on a production environment then just choose some higher CPU maybe T2 dot large or T2 dot extra large that would suit your need so third thing is we need to define the key name and this is just an optional thing but if you are interested in setting up your SSH Keys then just create one key pair and here assign the public key name this key I have defined over here in the bottom so this is the resource which we need to create for keypad so the name should be AWS key pair and the key name should be AWS underscore key and this is my public key over here which I have generated for uh this setup I'll upload a separate video on how to create an SSH key pair and use it with a terraform but in case if you want to know like how to set up your SSH keys on AWS not with terraform then I have already I've put a link of that video into the description section I have already uploaded that video onto my channel so just go back and check that video I'll put somewhere into the description or either into the tooltip section okay coming back to uh the modules the next thing which we need to do is we need to create a security group and here you can see this is my Security Group which I have already defined over here so here you will find the AWS Security Group and the name of the security group that is Main apart from that you need to put the name and description which you can put it by yourself with the with your suitable name secondly since we will be installing the Apache HTTP 2 server so we need to enable the port 80. that is we need to open it otherwise we will not be able to access our home page or the landing page of our apache2 server second these ports are needed if you want to SSH or if you want to login into your ec2 instance after you start your ec2 instance using terraform so this is the port which is needed and this is the default Port which I just kept it and with the cider block so it this port is just optional if you want to keep it that's okay if you don't wanna keep it that's also fine okay so now we are done with our oh no we are left with one more thing the installation script so this is the installation script which is needed for installing Apache 2 server and here you can see this is the bash script actually and if we will go one by one so whenever you need to define a long bash script so this is the way you need to Define inside your ec2 instance resource so that is going to be AWS underscore instance so this is the resource tag inside you need to define the batch script okay so now we have gone through Ami instance type key name Security Group and now this is the bash script which we need to go through so we'll start with first the first command is we will go and update our package manager second we are going to install Apache 2 using this command sudo apt install why apache2 third we are going to check the status of our Apache 2 fourth we are going to start the Apache 2 service fifth we are going to change the permission so as you know once you install Apache 2 then you will get a directory created like a slash VAR www.html so this is the directory location and in that directory you will find the HTML files like the home page or the landing page of Apache 2. so once you access the Apache 2 so that that the default page will be available onto that directory so we need to change the permission of that directory so that user can access it the current user can access it actually so this is the command for that and the last one is we are going to create an HTML page with some Hello message and the message is going to be hello this is module 1 and with some instance ID of the ec2 instance which we are going to start so this is our complete uh terraform file or the main.terraform file of module one where we are going to install Apache HTTP server with some custom message like hello this is module one so this is going to be my module one exactly the same thing which I am doing into the module 2 also and here if you open the module 2 then you will not notice any much of a difference so here everything is same and you can see uh this is the batch script which I am using and it is also going to install the same Apache 2 and the only thing which I have changed over here is the module 2 message and here you can see this is the message which I have changed so this is also going to install the Apache 2 server onto onto AWS now we have gone through all the modules and the parent dot main.tf now it's time to run our terraform script so probably I'll reduce the font over here and I'll open the terminal and I will reduce the path over here so that yeah that's looks much better now what we need to do is we need to go into the directory where our terraform module project is so I will go into terraform [Music] modules and here if you do LS then you will find this is our main.tf that is going to be our parent TF and this is these are the model module 2 and module 1. so what we need to do is the First Command which you need to run is terraform init okay that's been done the next command which I'm going to run over here is the terraform plan okay now here you can see there are five resources which is going to be added so now I have finished my terraform plan command and the next command which I'm going to run is terraform apply I'll clear the screen and I'm just gonna type data form apply there seems to be some problem okay so I think terraform yeah that's the correct command type yes all right so here you can see five resources has been added and applied to AWS let's head over to our AWS dashboard and see if we are able to if we are we have created those resources or not okay this is my dashboard and here this is my ec2 instance which I need to click and as you can see uh we have couple of modules module 1 and module 2 and each module we have started and easy to install so here you can see there are two ec2 instance which is of type T2 dot micro which are up and running so here you can see this is my instance id1 which I can click and open and I'm just gonna open the second one also so this is the first instance ID or the first instance which I have provisioned using either my module 1 or module 2 and this is the second one so both the ec2 instance are up and running that means we are able to execute our terraform module project successfully now after verifying that our ec2 instance is running the next thing which we need to verify is we need to verify that we have installed the Apache HTTP server successfully or not so for that what you need to do you need to copy this public IP address from here go to the next tab paste that IP address and always remember do not put https because we haven't installed any SSL certificate so we just simply need to type in the IP address along with HTTP without any s and just hit enter so here you can see this is the message hello this is module one and instance ID is this this so this was the message which we have put when we were installing the module one and which we you can verify from here also I will reduce the screen size and this is the main.tf of module one as you can see and this was the message hello this is module one and the instance ID and insta in the instance ID we are putting just the instance ID of my AWS instance easy to install Okay similarly if you go back to the other instance in here just copy this IP address go to next tab paste it over here and simply type the HTTP without us and that's it and this is my module 2. so now you can see we are able to execute both the module and we are able to install the Apache HTTP server using module 1 and module 2. and this is my main dot TF which were internally calling module 1 and module 2. so this main.tf was doing nothing it was just simply calling the module 1 and module 2. so now you know like how to create terraform modules and what are the concepts behind the terraform module and the today's demo just like a proof of concept you can customize the whole project with your own software packages which you want to install along with the other terraform module so use this uh example just as a reference and I'll share the guide along with the code which you can find into the description section of this video so feel free to use it this is the today's agenda for terraform Dynamic block we will start with what is terraform Dynamic block is second we will see the demo on AWS and will write a terraform script for dynamic blocks and third we are going to see what are the best practices for using terraform Dynamic blocks starting with our Point number one what is terraform Dynamic block consider an example using terraform we are trying to start an ec2 instance on AWS so we have set up an ec2 instance using terraform and the next thing what we want to do we want to open the ports and the ports which I am talking about is 22 and the port export is 443 so these two ports I want to start or I want to open on my ec2 instance assume we don't have terraform Dynamic block with us so what is the easiest way using terraform we can open this port so we need to Define increase resource so in that Ingress resource we are going to define the ports that is 22 and the next Port we are going to Define 443 so we need to write these two section of of increase rules and we need to put these blocks inside our terraform script so once we execute this terraform script then it is going to open the port 22 and 443 for our ec2 instance well it looks simple like we only have two ports and we can write this increased resource and we can open the port but consider scenario where you need to open multiple ports so not only 22 and 443 there are 10 more ports you need to open for your different business needs so you need to write this block of Ingress resource multiple times so that is not an efficient way of terraform so that's why uh terraform provides us a dynamic blocks and with the help of dynamic block we can reduce the number of lines of code inside our terraform script and we don't need to write this long uh increase resource rule for each Port repeatedly let's see how we can reduce the number of lines of code which we have used to create our increase resource so on the left hand side you can see this is my normal terraform script without using terraform Dynamic block so what I have done over here is I have defined two rules for opening the port 22 and 443 so this is the normal way without using terraform Dynamic block and if I need to define a dynamic block then this is how I'm gonna Define my Dynamic block uh I'll explain one by one so I'll start with the dynamic block on the bottom right hand corner so here you can see uh you need to add a keyword Dynamic then you need to define the name of your Dynamic block so here I have put in the name in Greece the name is up to you you can put any name of your choice second we are going to use a for each Loop so now you can get an idea like why I am using the for each Loop because I am going to iterate uh some values and the values are 22 and 443 these are my two ports on for which I am going to iterate it so this for Loop is being used for that purpose and this local dot increase rules this is the variable which will hold the values of ports that is 22 and 443 so here on the top side you can see the locals which I have defined so locals are kind of a variables which you can Define inside your terraform script so here I have defined the variables like increase rules and in that variable I have put two values like the port 443 and 22 along with the description so this increase rule will contain uh the port number along with their description so this for each Loop is going to iterate over this locals of increase rules secondly you need to define a Content section inside your Dynamic block the content section is which which is going to be repeated for different ports that is 22 and 443 so here this content section is going to be uh repeated or it is going to be iterated uh for the values which we have defined inside our locals so here uh what will happen is it is going to pick the description from our locals like if if it gets the value 443 then it is going to put the description in Grace rule for Port 443 it is going to put over here and in the front Port you will see in Grace dot value dot Port so we are using the same value that is value that is Port so here I am using the same variable like values.port into from Port as well as into two ports so I'll put once I'm iterating on 443 then I'm going to put 443 in the front port and as well as into the two port here protocol is hard coded and the sighter block is also hard coded now you might have gotten an idea like how we can reduce the lines of code using the terraform Dynamic block so we had um two ports uh with the repeated lines of code and now we have created a single Dynamic block with some locals and next time what I need to do is I just need to add a port number inside my locals variable and my Dynamic block will work on those ports so ports are just an example of to demonstrate the dynamic block but you can use it for other purposes and other use cases also this is my terraform script where I have created a dynamic block and let's start one by one so I'll start with the block one that is provider so first of all uh in terraform you need to define a provider so here I am using AWS so that's why I have defined the provider as AWS secondly you need to define the reason access key and secret key and if you don't know from where to get it then I would highly recommend to follow the part one where I have shown like where I have taken a very basic example to start an ec2 instance so I'm talking about this ec2 instance so please do check that part one if you are just getting started with the terraform but let's continue with the dynamic block so this is my provider section second as I explained to you the locals I have created the locals over here this is my locals block where I have defined the Ingress rules for my port 443 and 22. third block is where I am going to create my resource and the resource is virtual machine running inside AWS so the easy that is an ec2 machine actually so this is my resource and where I have defined the machine ID I am using the Ubuntu so I have using this machine ID and again this I have explained in part one and the instance type is D2 dot micro so uh like I'm using this for example purpose that's why I'm using the least CPU machine so that's why I have used micro a key name I'm using AWS key I have created a public key and private key and that's why I have created the key name over here the security group is uh the way you you need to Define it this way if you don't want to create your Custom Security Group so just simply uh use this uh attributes as it is and this is my Security Group in the security group I have created a egress as well as my Ingress rules so I'm not going to do anything with the egress I'm just keeping it as it is I'm just doing the dynamic block over in Grace so this is the increase Dynamic block which I have explained to you so this is my interest Dynamic block where I have forage Loop I'm iterating over increase rules along with the description from port to port protocol and cider blocks so uh this is uh my terraform script and this is my final key pair where I'm using the public key okay I'll upload a separate session uh where I'll show you like how to create this AWS public key and private key pair but for the time being just keep in mind I need to create a public key pair a public and private key pair and in the terraform script you need to use the public key uh so that you can SSH into your ec2 instance but I'll create a separate video for that one so this is my terraform script where I have created my Dynamic block so let's start by running some command so the First Command which you need to run is terraform init after that the next command which you need to run is terraform plan so this command is going to tell you like how many resource it is going to add to your AWS so here you can see there are three resources which is which is going to be added so I'll clear over here the next command which you need to run is terraform apply you simply need to type yes so here we our terraform apply command has just finished let's head back to our AWS console because our terraform apply command had just finished so this is my AWS console and here you can see one instance is running so click on it and here you can see this taste state of the instance is running so click on this instance ID and now we need to verify the ports which we are planning to open so the ports are 22 and 443 so you need to uh go to security tab over here and there you need to scroll it down and here you will find the port range that is 22 and 443 and the protocol which we are using is TCP so if you look back to our terraform script so we were planning to open these two ports so that is 22 and 443 so here you can verify the same thing uh that is 22 and 443 so this is the way uh like you can use the dynamic block to reduce your lines of code and create some reusable uh blocks inside your terraform scrub and once you are done with the demo then or once you're done with your application then you can just simply destroy the instance by running the command Terra form destroy now we have seen the demo of our terraform Dynamic block let's see what are the best practices for using terraform Dynamic block inside your terraform scripts the first practice over here is do not overuse so do not try to overuse the terraform Dynamic block multiple number of times inside your terraform file it will increase the complexity instead of reducing it second keep it clean and so that it can be reused further what I mean by Clean don't try to Nest the dynamic block inside your Dynamic block if you'll try to Nest the dynamic block then it will increase the complexity inside your terraform script so try to keep it simple so that it can be reused in future so that's the second best practices for using terraform Dynamic blocks I hope you liked the today's session on terraform Dynamic blocks so if you have any question related to Dynamic block then please put down into the comment section I'll try to answer those [Music] hello there this is Rahul and today we are going to talk about one interesting topic that is terraform null resource lately I have been reading about the terraformal resource on the official website of a terraform but I've felt those information was not sufficient enough so that I can use the terraform null resource for provisioning of my infrastructure so in today's session we are going to take a look what the terraformal resource is and how you can use the terraform null resource for provisioning your infrastructure before we actually take a concrete example of a null resource let's try to understand what null resource says so the first thing null resource says is don't do anything so what does it mean so don't do anything that means you don't have to provision or start any resource on cloud environment so whether you are working on AWS whether you are working on a Google cloud or whether you are working on Azure so if you are using a null resource then that null resource will not set up any resource onto your Cloud environment here I am just taking in very simple example and this is not a example of a null resource so if you I zoom in over here then if you will see this is code so this code is used to set up an ec2 machine or a virtual machine on AWS and this machine is T2 dot micro so as soon as you execute this terraform code and as soon as you exit terraform apply on this particular line of code then what it will do it will just set up an ec2 machine on a AWS environment so if you can see over here this is the end result of applying this code so you will get an easy to machine setup onto your AWS Cloud okay so now let's check the code of a null resource so this is the code of a null resource if I can zoom in over here so this is the structure for defining a null resource inside your terraform file so first of all you need to define the resource and the resource name should be null resource and then the name of the null resource this particular null resource can be anything you can keep any name of your choice so it doesn't matter so I am referring to this name so just put a suitable name for your null resource and after that you need to Define two parameters one is optional but another one is mandatory so here the trigger thing is optional over here so your null resource can be with Trigger or can be without trigger third thing you need to Define provisioners so here I am using the local provisioner but in case if you need you can use the remote provisioner also you can use the data source or data block of terraform also so this is the structure or the code for defining the null resource all right so now we have defined the null resource what then what will happen when we execute the terraform apply command so I'm just referring to the this command terraform apply so as soon as you execute the terraform apply command on a null resource then nothing will happen or no resource will be provisioned or started on your Cloud environment so here in this example I am taking AWS but if you are working on a Google cloud or Azure and if you are using a null resource and you apply a terraform or you execute a terraform or black command then no resource will be provisioned on to that particular Cloud environment so this is the first important thing about the null resource now you can compare both the example over here and you can see the end result of both the example that is easy to install and the null resource and it will ease out to understand the significance of your the null resource in terraform so here is I am taking one example which will help you to understand null resource in more detail so here I am just taking the same example code over here and if you see carefully then here I have defined the null resource and that is over here and here I have written and command and that command is a shell command and it is simply printing the hello world the shell command I have taken over here is pretty simple so that we can understand the concept so here you can see the output over here this is not output but this is the command over here and which I am trying to execute using the local provisioner available into our terraform so here ah I have defined the trigger and this trigger will execute as soon as we provision the ec2 instance I'll show you the complete example into the demo section letter but just to understand this Concepts over here that whenever you define a null resource then you need to Define some kind of a provisioner or a data source or a remote provisioner with your null resource so here I am using the local provisioner and in the local provisioner I have written and shell command which will be executed as a part of a null resource so as soon as I applied or I run the terraform apply command then this particular command will get execute and I'll see the output hello world into my terminal all right so now we know the syntax of a null resource and the significance of a null resource in terraform but what are the application or the use case of a null resource so here I have just written down some of the use cases but these use cases depends upon your need and your project setup so here let's take one example so I have already shown the one example where you can execute uh the shell command using the local provisioner inside your null resource so that is the first use case also you can execute the scripts so this is just a one shell command which I have written for printing the hello world but instead of that you can just execute a shell script as a part of your null resource so if you are having a big terraform project where you need to execute certain shell script which is big in size so you can just Supply those shell script into your local provisioner along with your null resource and you should be able to execute that particular shell script the third use case which I have seen for null resource is executing the ansible Playbook and this is just an example this is not an ideal scenario I am talking about but if you are using a null resource and if there is a need then you can just run the ansible Playbook also along with your null resource the fourth use case is a little bit specific to programming setup and how you have set up your development environment so ah the machine on which you are executing terraform and if there is a provision not provision if you have set up a python or if you have installed python onto the same machine then you can execute the Python program also using the null resource and that goes along with the Java also node.js also so these are just an example so with the help of null resource you can run a Python program you can run a Java code you can run even a node.js code also so that depends like what kind of programming language you have installed onto your local system and with the help of null resource you can just run those program also so these are the most significant use cases which I have formed which goes along with the null resources before we jump into the demo of a null resource there is one important Concept in null resource that is triggered so here if you see carefully this particular code then here I have defined one trigger over here so trigger as the name suggests it will just trigger the whatever provisioner you have written over here so here I have written the local exec provisioner which is executing the shell command so as soon as this trigger comes into picture of a null resource it will just execute the local provisioner but there are certain condition on which this trigger will happen so if you look carefully over here then there is a ID and there are some value assigned to this particular ID so whenever there is a change in this value if there is a new value coming into this particular ID then this trigger will execute and if there is a no change in value of this particular ID then trigger will not execute and this your local provisioner which is over written over here will not get executed so here I have I have tried to explain over here also if the value is changed and if ID gets a new value then you will see it will execute this particular local exec provisioner so this is an important concept when it comes to null resource because many of the time we use triggers along with our null resource before we head over to the demo I'll just try to share this link of this blog post which I have prepared for the null resource and I'll put this link into the description section of this video so you can just follow along this blog post so here I have documented all the code which I'm just going to use into the demo session so just follow this guide if you feel like you need to have the similar code onto your terraform project let's start with the demo and here I have taken one of the simplest example which I can take for explaining the null resource so start from the top here I have defined the provider because I am working on AWS and this example is for AWS but these concepts are pretty much same and these initial two blocks will change in case if you are using the Google cloud or Azure because if you are using Google Cloud then you need to change the provider over here similarly that goes for Azure also so here this is an AWS provider because I am on AWS here I am sharing the credential this is a credential file which I am using and this is the resource so this is an actual resource virtual machine that is an ec2 instance which I'll try to set up using this terraform code and at the bottom the third resource which I'm talking about is the null resource so here I have defined the null resource and for initially I have disabled the trigger I just I'll just enable it later but I have just disabled the trigger so that I can show that you can write the null resource even without the triggers also and here is the provisioner which I have written over here so in this provisioner I am just executing the command that is Hello World command so this is the code and here I'll try to set up an ec2 machine first and after that we'll just try to execute the null resource and we'll see whether it executes or not let's open the terminal over here and the First Command which I'm just going to write is the terraform unit command and if it's completed successfully then I'm just going to clear the screen the next command which I'm just going to write is terraform plan command and here you can see it is showing it is going to add two resources so one resource is the null resource and the First Resource is easy to installs and if you go over here then you will find the ec2 instance so there are two resources will be added ec2 and null resource okay I'm just gonna clear the screen over here the final command which I'm just gonna run is terraform apply and here I'm just gonna type yes and here you can see the command terminal output over here then you can see it is creating the ec2 resource this is the ID of our ec2 instance which is printed over here and this is the output of our provisioner or the null resource so here it is saying hello world that means it is it has executed our null provisioner and inside the local exec provisioner it has executed that particular shell command and it has printed the output as a hello world and here you can see our terraform apply command has just finished now here is one important concept for null resource so as you can see this is the first time we have executed our terraform code where it has executed our null resource for the first time so by default null resource will execute for the first time but whenever you try to execute the same null resource a second time then it will not execute so here this is the output of our first terraform apply I'm just gonna run second terraform apply and we will see whether it has executed or not ideally it should not execute the local exec provisioner and we should not see the output hello world onto our terminal so I'm just gonna create some space and I'm just gonna write one more command terraform apply and simply hit enter and here you can see since our ec2 instance has already been provisioned or already been started so terraform didn't start at second easy to install because it's the same instance which is running over there and here you can see the complete output and you will not see uh the output that is Hello World the shell command which we have seen in the first time so here this output is missing over here which means whenever you are working with the null resource and if you have written a null resource without trigger then it is going to execute only once and if you try to execute it second time that particular null resource will not execute and you will not see any kind of output let's destroy everything and next time we are just gonna enable the trigger and then we are gonna execute the same terraform apply command and then we will see the output here I have enabled the trigger already okay so open the terminal over here and I'm just gonna clear the screen and I'm just gonna run the terraform plan command because I have already executed the terraform in it so you don't need to do it again so here I have executed the terraform plan command and it is going to add the two resource that is ec2 and null then I'm just going to clear the screen and then I'm just going to run the terraform apply command and type yes okay so here you can see it has completed and we have got the new ID of our ec2 instance and after creating the ec2 instance we have the null resource has been executed and this is the output of our null resource so ah if you put the trigger inside your null resource then null resource is going to execute only once because it will get a new ID into the trigger and if we don't get a new ID inside the trigger then our null resource or the local exec provisioner will not execute so this is the the tricky thing over with the null resource so you have to be really careful when you are looking at the trigger and if there is certain value change which is happening inside the trigger then it is going to execute the local exec professional and in this case we have got that trigger value in the form of our ec2id and here you can see this is the ID and this is the output which we have got after the executing the terraform apply command now since we have enabled the trigger inside our null resource and we have executed our terraform apply command on this particular trigger once so now what will happen when we execute the terraform apply command second time since the ID is going to be the same because we are just simply executing the terraform apply command on already running ec2 instance so that ec2 instance is already running it has already got an ID which is unique and second time when we run that ID is not going to change it is still going to be the same since the ID value is still same so this trigger will not execute and this local provisioner will not execute that particular command so I'm just gonna create some space over here and I'm just going to run the Terra form apply command once again and here you can see it has executed the second time but here you can see there is no output of hello world over here so which tells again that the ID change is necessary inside your trigger and as soon as you get a new ID then you will execute your local or remote provisioner which you have written inside your null resource if the ID has not changed or the value has not changed into the trigger then it will not execute any of the local exec or remote provisioner all right so now you have got an idea how the null resource work and how the trigger can be useful for your null resource now I will take one more example I'm not going to destroy anything I'm just gonna keep the same state we have a easy to installs running and we already got an ID of that particular easy twin stance and I'm not destroying anything so the only thing which I am going to change over here is this ID so instead of ID I'm just going to change it to the timestamp so here I have already commented I'm just gonna enable it and I'm just gonna put a timestamp over here so why I am putting it over here is because I just wanted to change the value of a ID if I'll put this particular ID the ec2 instance ID then it is going to be the same and I will not see a change in the null resource or trigger ID and it will not execute it but since I have put a timestamp over here so timestamp will just return a new timestamp value since I have run the same code a couple of minutes ago and now I'm trying to execute it again so timestamp will change and as soon as timestamp will change then I will see the same local exec provisioner executing again inside the nug resource so as soon as I put this value inside the ID so every time I execute my terraform apply command this null resource will work so this is the a key thing over here into the trigger so as I mentioned if something changes inside the trigger and if some new value comes in inside the trigger then it will execute that particular provisioner inside this particular null resource so here I'm I have not destroyed anything ec2 instance is still running I'm just simply gonna run terraform apply command now I'm just going to type yes and here you can see it has executed that particular local exec provisioner and the reason being we have got a new ID and it doesn't matter over here it is just a key value pair so you can assign a string you can assign an integer and here you can see I have assigned a timestamp over here so as soon as the things changes inside this trigger it is going to execute this particular provisioner now I have described all the resources and I have just executed the terraform destroy command so I hope you liked today's session on terraform null resource and hope this session will help you to understand the concept of a null resource and in this example which I have shown I have just taken a local exec provisioner but you are not limited to local exec provisioner you can use the remote exac provisioner also and I have shown a very basic example that is Eco hello world but you can execute a complex commands also that you will find onto my blog post where I have shown a different different example where I have executed the shell script and multiple command into the local exec provisioner [Music] the today's topic of discussion is terraform data sources so we are gonna see what is terraform data sources are and how to use the terraform data sources inside your terraform project secondly we are going to perform a demo where we are going to create a data terraform data source and then we are going to fetch some information from our AWS cloud service provider so let's start with the first point that is what is terraform data source let's take an example to understand the terraform data source assume that you are a developer and you are working on a terraform script so this is your terraform script for starting an ec2 instance onto AWS so here you will see our instance type is T2 dot micro we have provided a name or the tag that is terraform ec2 and this is the Ami for our ec2 instance so this is the AWS resource block of our terraform script once we apply this resource block or once we execute our terraform script then it is going to create an ec2 instance onto our AWS environment but what if if I want to know the public IP or the instance ID or the tax name of my ec2 instance which I have just started using this terraform script so you can simply say hey Rahul just go back to your browser go to or log into your AWS console and from there you can fetch the public ID instance ID and the tags that's the one way of fetching the information but what if if I want to know these information only using the terraform script so here you can see this is the terraform script for creating a resource of type ec2 so this is just an input instruction for AWS environment to create an ec2 instance using terraform but this input instruction doesn't return you back anything so here you will see this is a simple input instruction so if you want to get any information back from your AWS console or AWS environment then you need to create a data sources so that's why data sources are needed when you want to get back some information using the terraform script so here we have only created the resource that's why we are not getting back any information but if you create a data sources using terraform then you can get the information back in the form of public IP instance ID and the tax a simple resource of AWS contains a lot of information attributes these three are just an example but if you create a data source then you can get many information attributes back from your resource which you have created using terraform so now next thing which we are going to see we are going to see the syntax of our terraform data source and how to create the data source inside your terraform script all right so this is the structure or the definition of our terraform data sources so here are two concepts one is the data block and one is the output block so data block refers to data sources and output block refers to the information which you will get it back from AWS environment and which we are going to print onto your console so here as you can see I'm printing the public IP but I'll get back to this output block letter let's start with our data block so here you can see I have used the resource as AWS instance so what does it mean since we are working on AWS so the data source which I want to create is of type AWS instance second we need to provide the name of my data source that's why I have given the name as my AWS instance so this is just like you can change the name as per your choice it is up to you second thing we need to define the filter but why do we need to define the filter assume that you are working on AWS environment where you have created multiple number of an ec2 instances so now you want to create a data source and you want some information back from your AWS environment but you have a multiple number of ec2 instances so which one to choose so that's why we Define a filter so this filter will be applicable on a particular one ec2 instance so that in situ instances like which has a tag name as terraform ec2 so it will only pick that easy to instance which has a tag name as a terraform ec2 that's why we need to define the filter and if you go back to the previous slide then you can see this is the resource block where we have created an ec2 instance and here we have defined the tag name so if you look this tag name then here we have defined terraform ec2 so we just wanted to filter only this easy to instance using this terraform data source and we want to discard every other ec2 instance all right second we need to define the dependency but why do we need to define the dependency all right so in a single terraform file you will have like a resource block where you have created a AWS instance and ec2 example is the resource block name so we need to find the information or we need some information written back of this easy to install so we need to define the dependency of this resource block so just keep in mind that the name of this resource block that is AWS instance and ec2 example so we are going to create that dependency in this depends on block so here AWS instance dot ec2 underscore example and which you can again verify over here AWS instance ec2 underscore example so that's the dependency we need to Define into data source that's because that is how a data source going to know like how I can fetch the information of our ec2 instance or the particular ec2 instance all right now we have defined the data source second this is the output block and here this output block name you can put any name of your choice like fetched info from AWS so I have kept this just simple but you can change as per your need second we need to define the value like what kind of information you want it back so I want to get back public IP but how to define that public IP attribute over here so that it can be printed on to console so first of all you need to use the keyword data that comes from here data source second this is of type AWS that's why we are using AWS underscore instance third what's the instance so that's the instance which we have created over here in the data source that is my WS AWS insta sorry I just clicked it so this is the instance of our data source uh the name of my data source installed so this is the name which I have kept it over here and what information you want to get it back so that is public IP so this is how you create uh your terraform data source to get some information back from your AWS environment so here I am getting it back the public IP but you can get the tag name or instance ID Also let's head over to the demo and this is my terraform file for my data source and the example which I have taken just onto the slide so first of all this is my provider block that is AWS because we are working on AWS and here we need to mention reason access key and secret key just one disclaimer over here we cannot use the plain text access key and secret key in your production environment because that's not the recommended security practice but this is just an example that's why I am using it but it is strongly discouraged to use this plain text access key and security but anyway let's move forward forward and this is my resource block which I have just explained that is AWS instance the name of resource the Emi which I am taking and the instance type is T2 dot micro and this is the tag name terraform ec2 okay secondly we have created a data uh source resource over here also and the same the name is my AWS instance and here again I am filtering on the basis of tag that is terraform ec2 and this is the tag name which is over here and third that is depends upon the dependency on our this resource that is AWS instance ec2 example so that's the dependency and finally this is the output where we will be printing the public IP okay but before that what I will do I will just comment out this section so that you will know like how the terraform uh resource or the data source comes into picture so here if you'll notice over here then this terraform script is just simply going to start an easy to install so it is not going to return you back anything because we have commented this data sources Stacks over here all right so what I'll go I'm just going to open the terminal and start running some terraform commands one more thing uh this is my GitHub wrapper which is public and I'll share the link of this GitHub wrapper where I have created all the previous example which I have taken into the session so you can find all the code of terraform into this GitHub wrapper and also along with that I have prepared this guide on my blog that is jhook.com where you will find all the instruction and the same code which I have used into the current demo so feel free to use both the GitHub link as well as this guide all right now we have commented out our data source section this one and we are only going to create an ec2 install so which you can see over here so the First Command which I'm gonna run is terraform you need command all right so now our terraform unit command has executed successfully I'll clear the screen the second command I'm gonna run is terraform plan command and here you can see it is saying plan to add one so it is going to create a one resource but it has not created yet all right so the final command for actual creation of a resource or the ec2 instance the command is terraform apply hit enter type yes all right so our terraform apply command has just finished and it has created one resource onto AWS so let's get back to our AWS console on our browser and verify the resource first so this is my AWS dashboard in the search console we can type ec2 click on the ec2 and here you can see this is our terraform ec2 instance which we have just created and if you click on the instance ID then you can get to know all the details about your terraform ec2 install so here this is the public IP 370 199 241 and these are the rest of the details of associated with our ec2 instance which have we have just started using teraf now we know our terraform ec2 instance has been created let's get back to our terraform script and enable the data source so heading back to our terraform script I'll reduce the console size over here and this is my data source block so I'm just gonna uncomment this block so that I can execute this terraform data sources so now I'll save the terraform file and now you can see we have the resource block where we have previously started the ec2 instance and now we have the data source so it should return you back with the public IP address and as you know that terraform once if we are going to change the same terraform file then it is not going to start again and ec2 instance but instead it is going to take the new change which you have done into terraform file so we have done the new changes that is data sources so this is the block which we have added so what I'll do I'll just increase the terminal size I'll clear the terminal over here and I'm just gonna run the command Tera form plan apply sorry okay and hit enter type yes and here you can see the apply is completed and you can see it has not created any resources because uh in the previous terraform apply command we have already created an easy to installs but here we have created a data source block so that's why this data source block is just an logical block which doesn't needs to be created on AWS environment that's why it has not created any resource but here you can see this is the public IP address which we got back from our AWS environment so this is the public IP which I got back of my ec2 instance and if you go back to our ec2 instance detail this is the same IP address 370 199 and 241. and yeah here it is this is the same IP address which I got back so uh this is how you can use the terraform data sources inside your terraform file our terraform script to get some information back from your AWS or any other Cloud environment provider such as Google cloud or Azure so here I'm just getting the public IP but instead you can get the tag name private IP subnet and any other details associated with your terraform or the AWS resources so with that we conclude the today's short session on how to use terraform data sources inside your terraform script or terraform project [Music] hello there this is Rahul and in today's session we are gonna talk about terraform workspaces uh today's agenda is first we are gonna see what is data from workspace is and how it is different from our normal versioning tool and in the second part we are gonna see the demo where we are going to create a workspace and then we are going to start an ec2 instance using uh two workspaces one is development workspace and second is test workspace so let's start with what is terraform workspaces are so if you are a developer then I am guessing that you might be familiar with the concepts of GitHub or SVN these are the versioning tool where you create your own branches so those branches are sometimes called as development branches where you do your own development and once you are done with the development then you merge those changes into your master or the parent Branch so terraform workspaces also works in the similar fashion so here what we do we create a workspaces those workspaces are kind of like a development or test Branch for terraform and once you are done with the development and testing part then what you do you create your final terraform script that you can ship or you can execute onto your production environment so for today's demo what we are going to do we are gonna create a two workspaces one is development workspace and second one is the test workspace as you can see over here we have created a main dot terraform file that is going to be your single terraform file and after that we are going to create two workspaces that is your development workspace and another one is your test workspace so these are the two workspaces which we are going to create and the purpose of both the workspaces is to create an ec2 instance so here on the left hand side you will get a easy to instance from your development workspace and on the right hand side you will also get a ec2 instance from a test workspace but to differentiate between these two ec2 instance we are going to assign a tag name so the tag name for development workspace would be Dev instance and for test workspace would be test install so here you can see this is the screenshot which I have taken from my AWS console so these are the two instances which we are going to create using the terraform workspaces but important concept over here is we are going to use the same terraform file we are not going to create another one separate terraform file we are going to focus on a single terraform file because terraform workspaces helps you to create an infrastructure from a single terraform file using the different workspaces this is the guide which I'm gonna share with the today's session and you can find the link of this guide into the description section of this video as you will scroll down the guides the first step which you will find inside our table of content is how to create a new terraform workspace so creating a workspace in terraform is pretty simple so here we need to keep in mind that we need to use three keywords so the first keyword is terraform second keyword is workspace and third one is new so as you have understood like new is a keyword for creating a new workspace workspace with is the keyword which we need to specify which tells terraform to create a workspace and finally the dev so this is my workspace name and it can be changed and you can keep any name of your choice for creating your workspace this is my terraform file and this terraform file is pretty simple so we'll start with the provider so since I am using AWS that's why my provider name I have kept it over here is AWS so here I need to mention three thing reason access key and secret key and if you don't know or if you are just getting started with the terraform and you don't know how to fetch this information then I would highly recommend to watch the part one where I have shown like how to use the AWS and how to fetch this information like reason access key and secret key so now we know like how to create a provider the next thing which we need to look over here is uh the resource I'll get back to this locals part but uh first of all let's see resource so whenever we work with the terraform and whenever we work with the AWS so what we need to do is we need to create some virtual machines in AWS environment so that's why we have to create a resource uh block over here so if you look carefully then this is the keyword AWS instance so which tells terraform that we are going to create a AWS instance and which type of instance we are going to create so that we are going to Define over here at line number 16 that is instance type so this is the variable which I have taken so this variable value I am going to assign inside my dff tf.ward so if you open this then here you will find the instance type so here I'm trying to create a t2.nano since my this is the example which I'm using it for demo purpose that's why I have kept the instance as a T2 dot Nano it is a very small instance which can be used for development or testing purpose but in case if you are using for production then please choose some higher instance type all right uh coming back to my terraform file so here this is my resource here I have defined the instance type then here this is the Emi that is the machine ID since I am using Ubuntu so I'm using this machine ID of my Ubuntu machine and again this all information like Ami reason access key and secret key I have explained in part one so please go back and check if you don't understand from where I'm fetching this Emi all right the next thing which over here is like a tag so tag is just a useful information which we put on our instance since we are creating an ec2 instance so I am putting a tag over there so this tag is just a string you can keep a suitable name like this is a development ec2 install so you can put a tag name onto it so this is the uh tag this is the purpose of actually the tag all right the next block which we are gonna take is the locals so here I previously explained like we are going to create a tag and we are going to assign a tag name so here I am using the locals to create my tag name so here consider local as a local variable for this file so this is the instance name which is the variable and this variable will contain the name of the tag and this tag is pretty much a string as you can see over here so this screen can be any name so you can simply hard code the string value over here so here you can say like my tag name okay and also if you don't want to put the hard-coded string name then you can just simply use the terraform workspace variable uh don't worry I'm just gonna explain this what terraform workspace variable later but just to consider like this is a string and you can keep any name of your choice inside this locals block so this is the instance name which I am going to use for my resource tagging now we have seen our terraform file and we are going to use this terraform file to create our ec2 instance but in today's session we are going to talk about terraform workspace so the first thing which we need to do we need to see like how many workspaces right now we have so this is on the left hand side you can see this is my terraform workspace project and on the right hand side at the bottom you can see this is my terminal so the First Command which I'm gonna run we I'm gonna check like how many workspaces I have right now so the command for that is terraform space workspace followed by the list so once you hit the center then it is gonna tell you like how many workspaces you have so here you can see it is telling you default and it's also mentioning this is star so I'm just gonna explain one by one so by default if you if you haven't created or if you do not have any workspaces then that there will be a one workspace with you that is the default workspace and there is a star what does it mean so that means this is your active workspace so if you haven't created any workspace before and this is going to be your default workspace and which will always be active that's why we have a star over here so this is the way you can look on how many workspaces you have currently onto your terraform workspaces or terraform project let's create our first workspace and the command for that is terraform workspace new followed by your actual workspace name so my workspace name currently I'm using over here is tab so once you write this command just simply hit enter and once you run this command then you will see a message like created and switched to the workspace div so what does it mean so if you run the command terraform workspace list then you will get to know that it has it has made the dev workspace as your active workspace so that means previously you were having a default workspace which was active and there were no any other workspace present so once you created a new workspace that is dev then that will become your default active workspace let's create one more workspace so that we can play along with a couple of workspaces and see the differences between them so the next command which I'm gonna run is again the same workspace create command and here I'm just gonna specify a new workspace named test and here you can see it has created and also switched our active workspace as a test workspace and that you can verify using the command terraform workspace list so here the star is now in front of the test workspace so that means test is going to be your default active workspace coming back to the guide so the first step was to create a new workspace so now we have seen like how to create our workspace and we have created a workspace named Dev as well as we have created a workspace named test so once you create the workspace then the next command is how to list all the workspace which you have created so the command for that is terraform workspace list which we have already seen and it will list out all the workspaces which you have created along with the star so that means it will also show you like which is your active workspace okay so the next command which I'm gonna show to you is like if you want to know like which is your active workspace right now so you want to see only the active workspace so the command for that is data form workspace show so you simply copy this command from here and go back to your terminal and just paste this command so it will show you only the active workspace so here you can see this is showing your test and if you remember like the previous list command it was showing you all the workspaces which has which it has been created along with the active one but this workspace show command will only show you the active workspace for you now the next question comes like how to switch between the workspaces in terraform so what does it mean so let's consider a scenario where you have created a multiple workspaces around three or four workspaces and you need to work between those multiple workspaces then how you are gonna switch those workspaces so the command for that is terraform workspace select select is the keyword for switching the workspace and then the actual workspace name onto which you wanna make a switch so I'm just gonna copy this command from here and head back to my terminal and here I'm just gonna paste the command but one thing over here I'm already on a test workspace so I need to switch to Dev workspace so I'm just gonna change the name to Dev and simply hit enter so now it's gonna show you the message like switched to the workspace Tab and that you can verify by using either the command shoe that will show you Dev which is your active workspace or either you can use the list command also so here now we have created the couple of workspaces and now we are able to switch from test to the dev workspace moving further the next thing which we are going to talk about is terraform workspace variable so I have shown you this variable inside my terraform file where my workspace project is so this is my locals this is my locals block sorry this is the my locals blog and inside this locals block I'm using this terraform.workspace variable so this variable is going to send me or tell me the workspace name so since I'm working in a development workspace then it is going to shoot a print the development workspace name over here so that's the main purpose of this variable now let's see how we are going to use this terraform workspace variable to create our ec2 instance and use the tag name as the terraform workspace name so here this is the tag name and here I am using the instance name and this instance name is coming from this terraform workspace variable let's apply this terraform configuration and right now I am on active workspace that is Dev so Dev is my current active workspace right now so the First Command which I'm gonna run is terraform init command all right so now my terraform unit command has completed the next command which I'm gonna run is terraform plan command and then in this terraform plan command we are going to supply a war file that is dev.tf war file so here in this TF Dev dot tf4 file we have used the instance type as T2 Nano so that's the only variable which we have defined over here that is T2 dot Nano and the variable we are going to use over here in here in the instance type okay so that's the war file we are going to specify along with our terraform plan command so I'm just gonna simply hit enter and here you can see our one resource is planning to be added so that means we are going to create a one resource uh once we run a terraform apply command so this is just a plan command which just tells you like how many resources is going to be added so yes we are going to create a easy to install that's why we are it is telling me that one resource to be added now I'll clear the screen and I'm just gonna run the terraform apply command so I can update the same old plan command so I'm just gonna switch from plan to apply and I'm just gonna Supply the same dev.tfr5 because we are going to use the same variable file in our Dev workspace so simply hit enter here it will ask like would you like to create simply type yes all right so now terraform apply command had just finished and here is the message let's verify our ec2 instance by going back to our AWS dashboard so this is my AWS dashboard so simply you can type over here ec2 click on the first option and here you can see our Dev instance is ready and the status is up and running so that means we are able to start our ec2 instance using our terraform script and we are using the workspace as our Dev is our default workspace so which you can verify by going into the tags also so here you can see the tag name is div Dash instance so if you go back to our terraform script then I have uh shown to you like this is our terraform workspace variable and this variable will return you the actual workspace name and after that I have concatenated or I have added my instance as my spring so the tag name which I have created over here is Dev Dash instance so that means you can use the terraform workspace variable in that manner to separate or differentiate between your different ec2 instance or any other AWS resources now after creating an ec2 instance inside our development or Dev workspace let's create a same ec2 instance using the test workspace so first of all what we need to do we need to switch our workspace to the test workspace so I'm just going to copy this command from here and I'm just gonna paste it over here and hit enter so as you can see our workspace has been switched to the test workspace now after switching the workspace to the test workspace the next command which I'm gonna run terraform plan and this time I'm gonna Supply my test TF War file so I'm just gonna change the name over here for my TF mod file and simply hit enter and here you can see it is showing me like it is going to add or create a one resource the next command which I'm gonna run is terraform apply and here I am gonna use the same plan command but I'm just gonna change the plant to apply and the next thing which I'm Gonna Change oh I don't need to change the war file name that is going to be test.tf War so I'm just simply gonna hit enter so it will ask like whether would you like to create this resources simply type yes and here you can see our one resource has been created or our ec2 instance has been created let's head back to our AWS dashboard and here you can see this was our previous tab instance let's refresh this page and here you can see we have a one more instance that is test instance which is up and running so if you remember or if you know like the how the workspaces works so now we are running the same terraform script using different workspace name so the first workspace which we have used is the dev workspace and we have created a ec2 instance for development workspace then we have created a second ec2 instance using the test workspace so both the workspaces are completely separated or completely different and those first spaces doesn't interfere with each other although you are running the same terraform script so here you can see I have this the second test instance of my ec2 which is up and running and you can select this instance also over here and we can verify the tag name at the bottom so here you can see that instance tag name that is test Dash instance so that's the same instance name or the tag name which we have specified in our terraform script so if you go back to your terraform script then you will see inside your terraform file that you have used the instance name using the terraform workspace variable so here it is so this is how you can differentiate between the different resources using the workspaces and if you use it logically then it will really helpful for your development and testing purposes the next thing which we are going to see after this is we are going to destroy the ec2 instance but the destroy also works in the same way we have created the instance if you are using a test workspace and you are trying to destroy the ec2 instance then it is going to destroy the resources only for the test workspace it is not going to touch the resources for development workspace so you need to switch to development workspace to delete the resources which you have created from your development workspaces let's start deleting the resources which we have created but first of all let's check on to which workspace we are so the command is terraform Works ratio and we can say we are on a test workspace which is active so I am simply going to run the destroy command and using the test tf.par file because we are into the test workspace and simply hit enter it is going to ask whether you want to destroy simplet IPS all right so now our ec2 instance has been destroyed from our test workspace let's head over to our ec2 dashboard and refresh this page and here you can see this was our test instance and the current state is terminated but our Dev instances is still running so what does it mean so the way you have created the workspaces and the one workspace which is active you should be able to delete or create any resources into that active workspace it will not going to touch the other workspaces so here in this our current example we have seen we use the test workspace as our active workspace and we have created our resource and after that we are trying to destroy it so once we hit the destroy command that it is going to only destroy the resources available into the test workspace now let's try to delete the ec2 instance from our development or the dev workspace and the command for that first of all we need to use is we need to select the development workspace and for that we need to change the workspace name over here so the command is terraform workspace selective hit enter and now you have switched to development workspace and now you can issue your terraform destroy command so here I'm just gonna update I I don't need to update uh wait sorry I need to update the DF War file so I'm just gonna switch to Dev dot tf.or file and after that we are just simply gonna hit enter and remember in previously we have destroyed the resources for test workspace now we are trying to destroy the resources for our development workspace so here on to ec2 dashboard we you can see previously we have destroyed test instance and that is terminated but our Dev instance is still running and now I have issued the destroy command for my development workspace so it will going to ask are you want to share so I'm just simply type yes and it might take some time and then your development instance will also go into terminated state after running the destroy command so here you can see our instance has been destroyed and if you go back to our dashboard and just refresh this page so this state from running it should be terminated now and here you can see our both are easy to instance of our test as well as the development workspace has been terminated I hope you liked the today's session on terraform workspaces and how to create the workspaces for your task development or staging environment try to use the workspaces efficiently rather than creating a multiple number of workspaces stick to some two or three workspaces which will really help you to boost your development process [Music] hello there this is Rahul and today we are gonna talk about the terraform user data block terraform user data block an important concept when you are trying to set up a virtual machine but you also want to do some pre-configuration or a bootstrapping setup so what does it mean by the pre-configuration or bootstrapping setup suppose you have started a virtual machine which is empty but you don't want an empty virtual machine you want to have certain packages installed already onto your virtual machine so what you're gonna do you're just gonna use the user data block and with the help of user data block you can execute some shell script and in those shell script you can specify you want to install python you want to install ansible you want to install Apache you want to install nginx server so this user data block is a part of a terraform code and whenever you're just going to run the terraform code then it is going to set up an virtual machine as well as install those packages also so yeah it might sound complex right now but it is really easy and that we will see into this particular demo so let's switch over to the demo and see what the user data means all right let's start with our first point and what is the user data and why should we use inside our terraform so just to explain a bit so here I have taken a very basic example and in this example first of all I'm just trying to set up an ec2 machine over here so this is the initial code which you can see where I have specified the Ami and the type of the machine and in case if you are using gcp Google cloud or Azure then this part will change because there you will set up a different instructions for provisioning the virtual machine in Google cloud and Azure but the concept is same pretty much so here I am just writing this code to set up an ec2 virtual machine which is based on Ubuntu and second here I am just writing this user data block over here so in this user data block I am just writing a shell script so if you'll see carefully over here so this is the first beginning of the shell script and this is the actual shell script which is just creating a hello.txt file so this is the hello.txt file which I'm just creating over here and this is the Eco message which will be get written into this hello.txt file so this is a very simple example which I have found to explain the user data so whenever you are working with the user data then you use user data block to do a pre-setup for your virtual machine so what will happen when you will execute the terraform apply command on this particular example so it will create an easy to virtual machine but as well as it will just execute this user data block and once it execute this user data block then it will just create this hello.txt file inside your ec2 machine so that's the purpose of our user data block to do the initial setup or a bootstrapping setup whenever you are provisioning any virtual machine on any Cloud platform whether it's AWS whether it's a Google cloud or whether it's Azure let's switch over to the IntelliJ which is my IDE and do some demo with the example which I have just explained to you so here is my example code so here I'm using the AWS because I am working on AWS environment so here I have specified the reason and my credential file secondly this is my resource which is AWS instance and here I am specifying what kind of instance I am using so that is D2 dot micro which is a small machine which I am using for the demo and here are some setups which I have done uh don't worry you will get all this code onto from my GitHub repository so you can just copy everything but the important thing over here is this user data block so here I have written the bash script and in that bash script I'm just writing an Eco message and just writing that Eco message into my text file so this is just an example so I will just switch over to my terminal and here the First Command which I'll just execute it's terraform in it that's been done I'll clear the screen the next command which I'm just going to execute is terraform plan just to see how many resource it is going to add or create so here you can see it is going to create three resources that's fine I'll just clear the screen and the final command which I'm just going to run is terraform apply it might take couple of minutes so I'll just fast forward this one I'll just type yes all right so as you can see our terraform apply has been completed successfully and here you can see the message it has added the three resources now the next thing we will do we will SSH into our ec2 machine and we will just verify that hello.txt file has been created or not using our user data block before we SSH into our ec2 machine I just wanted to show you the AWS console of my ec2 instance so here you can see this is the ec2 instance which is running and if you click on connect then you can see the public subnet of my ec2 machine which we are going to use to SSH into our ec2 machine okay so now coming back to the terminal I'm just clear this screen and I'm just gonna paste the SSH command and I'm just going to Simply hit enter type yes to accept and here you can see we have SSH into our ec2 machine which we have just set up I'm just gonna clear the screen so that I can get a little bit more space and first of all we just want to check where we are so I'm just going to issue the PWD command and here you can see we are on a home and Ubuntu but our text file has been created at the root location because that's the default location where user data executes so I'm just gonna go back so I'm just gonna jump One Directory back verify here we are at home directory I'm just gonna one jump one more directory back and now we are at root location here you can see and I'm just gonna issue the command LS and here you can see this is the hello.txt which we have just created using our user data block and we can just issue the hello Cat hello txt command also to verify the content so here you can see the hello world dot which is like a return uh using the user data block using inside our terraform example now we have seen the basic example of a terraform user data so the next question comes like what Advanced thing which you can do with your terraform user data block so here you can see one example which I am taking it over here so here I am writing a shell script file which is a separate shell script file which will be responsible for creating some directories and installing some Apache onto our ec2 virtual machine so here you can see if I zoom it over here so here it is first of all updating the package manager for our Uber ec2 machine which we have which we will set up using our terraform then it is going to install the Apache then it is going to start the Apache and then it is going to enable the Apache so that we can access the home page of our Apache 2 server and then this is the message which we are going to create this is just going to be an HTML file which we will be accessing so this this is a little bit more advanced stuff which you can achieve with the user data block inside your terraform but now you have created this file and this file is you have saved with the name install apache.sh so this is the file which is residing separately so how will you call this shell scrub file inside the user data block like in the previous example we have seen we have actually hard coded our shell script but here we are just creating this file separately so here this is the example code for how to refer or how to call that particular shell script file so here you will notice we are pretty much using the same thing here we are trying to set up an ec2 machine over here so this is an ec2 machine but here if you look carefully so this is the function that is the file function we need to use and here we need to specify the name of the file so this is going to be the file where we have written our shell scrub and this is the user data block uh the concept which which we are talking about so you need to specify the user data block you need to specify the file function because our file the shell script file resides separately and then you need to specify the name of the file so once you do this then what will happen as soon as you start your ec2 machine it will just read this user data block it will just execute this particular shell script and it will do all the things which is written inside that particular shell script file okay let's head over to our IntelliJ and see this demo in action all right so here is my IntelliJ so I'll just comment out my previous example where we have created a hello.txt so I just don't want that so I will comment out this section and I will enable the file function where we will be calling the install apache.asset so I'm talking about this and if I show you over here onto my screen so if I can increase it a little let me give me a second so here you can see I have created the install apache.sh separately and this is the content of my install Apache dot asset so where we will be updating the package installing the Apache is starting the Apache and then finally enabling the Apache let's head over to terminal and run some terraform command so this is my terminal and the First Command which I'm just going to run is terraform in it then I'll just clear it and the second command which I'm just going to run is terraform plan and here you can see it is planning to add three resources which is fine I'll just clear the screen once again and the final command which I'm just gonna run instead of form apply and type yes it might take a few minutes but I'll just fast forward and I'll just come back when it's ready so here you can see our terraform apply command has just finished and we can I can just show you the AWS console also I'll just refresh it over here and here you can see our instance is running and these are the public subnet to connect to our ec2 instance all right so I'll just move it to the right hand side and this is the command which I'll be using to connect to my ec2 instance I'll just copy it I'll just clear the screen and I'll just paste the command and type yes to accept it and here you can see we are able to uh SSH into our ec2 machine let's first verify whether we have installed the apache2 server successfully or not so the First Command which I'm just going to run is to verify the status of Apache 2. so I'm just going to run system CTL status Apache 2. and here you can see uh the service is up and running so next thing which I'm just going to verify is by curling localhost because I am inside the machine that's why I'm using localhost and enter and here you can see I can access the basic HTML page like deployed via terraform and here you can see this is the echo message which I have written inside my shell script and which was creating a very basic HTML file ins for our Apache so this was just a little bit more advanced example like how you can use the user data block to achieve some complex functionality using the Shell scrub so this was just a small demo on how to use the user data block inside your terraform projects and I hope you can use this basic example to implement a complex functionality based on your needs I'll put the link of the guide which I have just shared into the demo into the description sections just so feel free to use it and also share the GitHub repository where I have created all this example so just feel free to clone that GitHub repo also let's discuss the terraform depend on meta attack today let's first discuss like why do you need a terraform depend on meta tag inside your terraform project so when you use terraform then with the help of terraform you can create multiple resources inside your Cloud platform so if you are using AWS then you can create S3 bucket ec2 instances you can create kubernetes cluster and similarly if you are using terraform for gcp then you can create a compute instances you can create a cloud storage and many more things but terraform depend on tag is really important when you want to define a dependency between these resources let's take one concrete example and how we are going to use the terraform depend on meta tag so here onto the screen you can see this is the code which I'll use to create a S3 bucket onto my AWS environment and similarly I have this next code which is available for me to create an easy to instance on the same AWS environment so with this terraform code I can create two resource this one is F3 bucket and second is easy to install this all right let's rearrange the code and try to define the dependency between these two resources so for example I have taken an S3 bucket and ec2 instance so what I need to do is I need to create a dependency on a S3 bucket so first of all I want to tell terraform that first create an S3 bucket and once after creation of S3 bucket successfully then try to go and create an ec2 instance so I don't want ec2 instance to be created before S3 bucket so here what I will do I will just write my terraform code and first of all I'll just create an S3 resource and after creating an S3 resource then I'm just going to write the resource block for my ec2 instance and inside that ec2 instance I'm just going to use the depends on meta tag so here on to the screen you can see I have used the depends on meta tag and inside that depends on meta tag I can provide a reference to the S3 bucket and as soon as you provide the reference to the S3 bucket then terraform knows that we need to create this ec2 instance but we need to create the cc2 instance after I create a S3 bucket so here we are just trying to define a sequence so first terraform will read this code it will first create an S3 bucket and after successful creation of a S3 bucket then it is going to create an ec2 instance so that's how terraform will know that which resource it needs to create first and which resource it needs to create last let's switch over to the desktop and see the demo all right so this is my desktop and we'll start from the top so here on the top you will see I have created a provider section where I have specified like on which Cloud platform I will be working so I'll be working on AWS second I have specified the region where I'll be deploying my resources so that is EU Central second I have specified the credentials file so where I have stored my project secrets so in case if you don't want to use the credential file then you can just replace the secrets over here but just hard code the secret only for your learning purposes don't do this in production all right moving further so this is the first resource which I will be creating so that is going to be my AWS S3 bucket resource so here I have just used the S3 bucket resource this is the name of my resource and remember we need to uh use this name later on into my depend on block now depend on meta tag so here this is my name of my S3 bucket resource and this is the bucket which I'll be creating the name I will be giving to my bucket and remember don't confuse with this name and this name this is the name of the resource which I'll be referencing inside terraform but this is the name which will be create assigned to the bucket once it is created so that's the difference I just wanted to make it clear and here I have created few tags moving further the second resource which I'm just going to create is the ec2 instance so here I have just defined the resource type that is AWS instance the name of the resource inside terraform is going to be easy to example this is the machine ID which I'll be using the instance type is going to be T2 dot micro and the tag name which I'll be creating is terraform ec2 okay now that's the necessity uh necessary code which you need to Define for creating an ec2 instance now comes the depends on meta time so here I'm just trying to define the dependency over a S3 bucket and how to define that particular dependency so first of all you need to define or you need to put the correct and resource type so that is AWS as three bucket so this is resource type you will find it over here so that's the dependency you're just going to Define on that particular resource but then you need to define or you need to reference to the exact name of the resource so name of the resource is my test bucket and here I have just specified my test bucket reference so that's how you just create a dependency on S3 bucket so you just always be careful like what kind of a dependency you are trying to Define over here so here I'm just trying to Define with the resource type and the resource name so that can be any resource inside your terraform project one more thing over here this is an array so here here you can create a one more resource or here you can Define the one more resource dependency also so this is just an example purpose I am showing you showing it to you but in case you might be wondering that I have so many resources and I have so many dependency I need to create so this depend upon tag does not restrict you with the one resource you can assign multiple resource name over here and that will be the dependency which terraform will create all right let's open the terminal and run the terraform command so here I'm just gonna open the terminal this is my terminal the First Command which I'm just going to run is terraform indeed I'll clear the screen after successful execution the next command which I'm just gonna run is the terraform plan command and here you can see it is planning to add two resources which is fine because we are just creating an S3 bucket as well as an ec2 instance all right so I'll just reopen the terminal once again I'll just drag it over here I'll just clear the screen and the next command which I'm just gonna run is terraform apply hit enter and type yes let's see what's the error says so bucket test one for import is not available so I think I have taken this name before so I just need to rename it so let's put a name over here is the two go to your terminal clear the screen and rerun the command type yes and here you can see task bucket 2 for import I think task bucket one I have already taken so that's why it is what it was not allowing me to use it all right so now my terraform apply command has just finished and here you can see that two resources has been added and if you check the sequence of resource creation over here then first of all it has created an S3 bucket and after that it has created an ec2 uh easy to instance over here so these two lines which will indicate that first S3 bucket has been created and after that we have we are able to create the ec2 installs so that's how it creates a dependency uh inside your terraform project using this terraform depend on meta tag well this was a very small example but important thing over here is to understand the depend on meta tag so this is how you're just gonna create a dependency and use this dependency inside your terraform project but always try to remember don't put a cyclic dependency so what does it mean so if you are trying to create an S3 bucket and then after after that you are just trying to create an easy to instance then don't do the reverse order in the same way otherwise it will create a lot of cyclic dependency inside uh inside your terraform project so always keep the things linear in and sequential so always go from top to bottom where you create First Resource and then you create a second resource but don't create a cyclic dependency where First Resource is dependent on the second resource then it will screw a lot of things inside your terraform project I hope today's session will help you to understand the terraform depends on meta tag and I'll share the link of this repository inside the description section so feel free to clone that repository and if you have any question then please put down into the comment section I'll try to answer those so see you into the next session of a terraform our devops till then take care and bye bye as you can see on the drawing board we are just gonna cover the six points in today's session so we will first start with what is terraform template and what's the importance of terraform template is also we are gonna see like how to create a terraform template and what are the extension by which you can save your terraform template and also we are gonna have a look on to where to put your terraform template in your project and how to call a terraform template inside your terraform script and at last we are just gonna perform a demo where we are going to set up a terraform template and using that template we are just gonna set up AWS IM user and the different role associated with that user to start with let's first try to answer the question what is terraform template so as you can see over here I have taken a very basic example and here I have taken a word template so I'm just trying to explain the concept of a template first so here you can see this is a very standard template which is used for preparing the later so here you will find the place folder for your name so you can enter your name over here similarly here you can enter your street address City ZIP code and here also you will find a placeholder for that company again City and sub code so this is a pretty much standard template in the word Ms word which you can use for writing or preparing the letter so you don't need to create this design every time you need to write a letter you can simply reuse this template and generate hundreds of letters now you might be wondering like do we have a very simple example to see the terraform template so here you can see on the drawing board I have taken a very basic example uh to show a terraform template so here you can see this is a terraform template which is we are using to create a bash file so this is a typical based bash file syntax and here you will see a placeholder so the placeholder over here is this one which is like a dollar sign curly braces and inside there is a variable so that variable name is console address so this is actually a placeholder where actual value will be inserted okay so this is a typical or the smallest template file which I have found over here to show it to you so that you can understand how the terraform template looks like secondly the extension like how you are going to save your terraform file so here you will see so this is the name of the file and if you look carefully over here then this is a extension that is dot TPL so this is the extension by which we save our terraform template also there is a one more popular extension for storing the terraform file that is TF TPL so this is I have written it over here so you can save your terraform template using this extension Also let's take one more example to show you the terraform template and this time I'm just gonna take an example of a Json template so here on the screen you can see a Json file but this is not a Json file this is a Json template which we will be using inside our terraform code and also this is the same template I will be using in the demo into the letter session okay so uh in the file if you look carefully then you will notice a placeholder so this is a placeholder and this placeholder will be used to generate the ec2 policy name so this is the value we will be passing later into the demo so there we will insert some actual policy names over here so this is a one more example on how to create a terraform template all right so now you are familiar with the concept of a terraform template and you have seen couple of examples of a terraform template file now let's see the project structure of a terraform and where you can place your terraform template file so here you can see over here this is my main. terraform file and along with that I am putting my terraform template file over here so you can place your terraform template in parallel to your main terraform file but you need to specify the path when you are trying to use the terraform template and this which we are going to see later into the demo so this is how you are gonna put your template file so similarly I have some more projects over here so you will notice this is is our main.terraform file and this is our template file and here this is a one more template file with the extension tftpl so and here this is our main dot terraform file so this is a typical structure on how you are going to create your main. terraform file and how you are going to put your template file inside the terraform project so this was the intro part of a terraform template let's switch over to desktop and see some demo in action before we jump into the demo I have prepared a readme file over here so let's have a look onto that file first so in the readme file you will notice I have taken some code snippet on how to call a terraform template so let's have a look into more details so here you will see this is the resource which I have created so this is a typical syntax which is provided by a terraform to use the terraform template so what you need to do you need to write a resource and the resource name should be template file after that you need to put the name of that resource so here I have named the resource as a policy so this is a user defined name you can keep any name of your choice second you need to define the path from where you will be importing or using the terraform template so here you can see this is the variable that is template and this is the name of the file or the file path where our template file is available so if you go over here so here our main.tf and here you will see that I am using the same thing over here and this is my template file which is just present parallel to our main.terra form so this is a typical syntax which you need to use and if you look carefully so this is our policy and here I am using the variable as name so this is a placeholder for our terraform template and if you go back to our main.tf file so here I am passing that variable so this is the value which I just wanted to insert inside my terraform template so this is over here so if you open again so there is also one more thing which is important so this is the name of the variable so as of now I have kept the variable name as name and that should be same inside your terraform template because that is the placeholder by which you are going to pass that value and that value will be inserted into this terraform template coming back to the readme file and the next syntax which I would like to show you is the template file function which is also provided by terraform so here you will notice now now this is a template file function which I am using over here and in this function you need to pass two things so the first thing is the template file the path of the template file and the variable name so this variable name can be a single variable or it can be a list or it can be a map so you can pass those variables over here this could be the second parameter of our template file function and one more thing this this particular resource the template file resource has been deprecated into the terraform so this is not recommended to use anymore but instead you can use the data source for implementing the same feature you can check the data source tutorial which I have uploaded previously onto my terraform playlist alright so now I am assuming that you are a bit familiar with the terraform template file function and the resource template file so let's have a look like where you can use this terraform template inside your terraform template configuration so here the first thing which I have said over here is the locals so if you create terraform locals then also you can use this template file function inside that locals similarly if you create a data source or data block then also it is possible to use inside that data block of terraform and also one more thing if you are creating an any resource or AWS resource so let's say for example a AWS IM policy or ec2 instance or S3 bucket that it's also possible to use this template file function inside those resources so as you can see the template file function is pretty generic and it can be used in most of the resources which is provided by terraform all right let's take our first example and here you will find this is our main.terraform file and here I am creating the template file resource not template file function so here I am just creating a template file resource and here I am mentioning the path of my terraform template and the variable name switching over to my terminal the First Command which I would like to run is that I form init command and after the terraform unit command has successfully finished let's run the next command that is terraform plan and as you can see I have already told you that that template file resource has been deprecated by terraform but still we can use it I'll clear the screen the next command I'm just going to run is terraform apply and here you can see the output which we get over here is the policy for Rahul bar so if you go back to our policy.tp also here policy 4 this is the tax which I have hard coded and the value of the variable that is my name talking about our next example where we will be using template file function inside our data source block so this is the directory where my main dot terraform file is present so just open it so here you can see this is our data source block and here I have used the template file function and remember you need to specify the path as well as the variable name over here and again I have I have defined this as a inputs inside my data source block let's switch over to the terminal and run some terraform commands so this is smart terminal and the First Command which I'm gonna run is terraform init and after it successfully runs the next command which I'm gonna run is terra.com plan and yeah it's also successful the next command which I'm gonna run is data form apply and here you can see this is the output which we are getting policy for Rahul work data source example and if you open the our data source main.tf file it should be here so here you can see data source example so it is the correct output which we are getting uh when we run the terraform apply command now talking about the next example now we are going to call a template file function inside the terraform local so as you can see over here on to my IDE so this is my locals block of terraform and here I am using the template file function and as I have already told you you need to specify the path as well as the variable name so here the variable name I have using my name along with just to identify that I am calling the correct terraform template or the terraform main file so here I have added a locals just to differentiate from my previous example let's switch over to the terminal and run some terraform commands for this example so this is my terminal and the First Command which I'm gonna run is terraform in it and it's successful I'll clear the screen the next command which I'm gonna run instead of form plan and that's also successful I'll clear the screen and the final command which I'm gonna run is terraform apply type yes over here and as you can see this is the output which I am getting policy Rahul work dash local so that's the value which I have provided into the variable so this is how you can use the terraform template file function inside your local and in the previous example we have seen how to use it inside our data source block all right so now you have a familiarity with the terraform template file and how to call that template file inside your terraform configuration so now we will take a very real example where we will be setting an IM user and we will be setting the policies for that particular user and all the things which we will be doing through our terraform template okay so here you can see on my IDE so this is under AWS IM policy directory and this is my main.tf file and there is a user policy.tftpl that is the terraform template we will talk a little bit later so let's have a look on to the code so here first of all if you are working with the AWS then you need to define the provider so which I have done it over here then I need to create a im user so that's why I'm going to use the resource that is AWS IM user over here and the name which is a user defined name so I just put it as a test user but the name of the actual user is the test terraform user so this user will be get created inside our AWS cloud all right secondly we need to define the access key so here I am using the resource because access key is needed to generate the access key and the secret key for that particular user so this is the resource for that and I have mentioned the step number one over here because before we actually use a terraform template I just wanted to show you the code if we don't use the terraform template so now what we have done so far we have defined the provider we have created we have added the resource for creating a user and we have added a resource for generating the access key and the secret key and now we need to define the policies for that particular user and remember we are not using terraform template over here so what we will do we will just use the resource I am user policy and here we are just gonna use the same user which we have created so I am user test user dot name so it should be similar to IM user trust user and the name so we are referring to the same user which we have just created over here and then here we are just defining the policies so here you can see I have hard coded these policies over here so what will happen it will create a user it will generate access key and secret key and it will attach these policies to that particular user and everything is done and this is done via hard coding it there is no problem with it so what we will do let's run this example without using terraform template and this is my terminal and remember I'm just skipping the terraform a neat and terraform plan command because I have already run it just to save the time so I'm just gonna run the command terraform apply and enter and type the value yes and here you can see it has added three resources over here let's switch over to our AWS console and see that user has been created or not so I'll just go to my browser and this is my IM section of my AWS account so here you can see the test error form user has been created and if we open the policies then these are the policies which has which we have assigned to that particular user and remember everything which we have done it's done via hard coding everything into our main.tf file okay now we have seen the example without terraform template I'll just run the terraform destroy command to destroy everything over here and then we will be using the terraform template to implement the same thing using our template file so as you can see on my AWS console that resource or that particular user which I have created has been destroyed or deleted so let's switch over to our IDE and see the example once again so this was the step one I just mentioned it because I will be commenting this resource block again because this is where I have not used the terraform template so let's comment out this section over here and till here and then enable this particular second step over here so I'll just enable it and here there is a one thing which I'd like to mention over here that I am using a terraform template now so here this is the template file and previously if you notice we have not used anything over here okay that's fine so here I'm using the template file function I'm specifying the path of the template file which is uh present inside my project so here if you go into this directory AWS I am policy this is my main.tf file and this is my user policy template file so this is the typical template file which is needed for assigning the policies to that particular user so here this is the template and here this is our placeholder so where we will be putting our values all right so one thing also which I would like to mention that whenever you create a AWS policies then that that should be created into the Json format so that's why this is a typical Json format structure and I have told you into the introduction introduction session that we will be using this template later so this is the same template which I have shown you earlier so since it is in a Json format so we have to use the Json in code function otherwise you will not be able to render those value into the Json format so here I'll be using the Json encode function and inside that I'll be passing the variable name that is easy to underscore policies and if you go back to our main.tf file so this is our template file function this is the path which you have just seen and this is our variable that is ec2 underscore policies so this is the same variable which we are using over here along with our Json in code function so that it can generate the policy into the Json format so this is the change which I have done and these are the policies which I will be passing over here so this is how I'm just gonna change my existing hard coded ec2 policy and using the template and passing those as a variable let's switch over to the terminal and run some terraform commands onto this example so this is my terminal over here so the First Command which I'm just gonna run is terraform in it and I'll clear it after successful running the next command which I'm gonna run is terraform plan and as you can see it is trying to add the three new resources that's fine I'll clear it again and the final command which I'm gonna run is terraform apply type yes over here and as you can see it has created the three resources so let's switch over to our desktop and refresh this page over here and here you can see the test terraform user has been created and if you open that particular user and go to the policy section then these are the policies which has been added over here so this is how you can implement the terraform template file function and you can create your own template oh well this is a very basic example which I have taken but you can automate this example or you can just take the reference of this example to implement a terraform template file function for your complex business functionality one more thing uh in this example the Improvement which you can Implement is here still I am hard coding the policies inside my terraform file so my recommendation over here is to move this section or this comma separated values the ec2 policies into a variable file so that you can get rid of all of these things and you can insert some variable over here so that what it will do it will reduce the size of your main.terra form file and it will look more readable and that way you can improve your code a little bit more so that's something you can do I think the implementation of this thing into the locals or variables is pretty easy in terraform so that's the Improvement which I would suggest on this particular example so I hope you liked the today's session on terraform template and if you have any questions or suggestions then please put down into the comment section and I'll try to answer those and you can find the same content on my devops bug which I'll be updating later after preparing this session so you will find all this instruction and the GitHub link into the description section along with the blog post with the detailed steps so you can go there and follow all these things and let me know if you have any questions or some queries related to the today's session [Music] debugging is a very crucial when it comes to the development insufficient logging or the missing log press can really be a show stopper for your development and troubleshooting today we are gonna see how to enable the debugging inside your terraform projects and how to set up a different log level that is debug price info and one in the second half of this session we will also look at the terraform validate module and which you can use inside your terraform project to put some additional validation and to help you with the troubleshooting and debugging to enable the debugging in the terraform project you need to set to environment variable the first variable is going to be the TF log where you will be setting the different log level it can either be debug Trace Bond or info the second variable where you will be mentioning the log file name and the variable name will be TF log path and inside that variable you will be mentioning the log file where you will be storing the lock generated by you there are object let's switch over to desktop and start setting these environment variables as I have already told you you need to set the environment variable for setting up the log level so the first environment variable which I am going to start is the TF log and here the value should be debug because we will be setting the log level to the debug I'll just copy this command from here go to my terminal and I'll just paste it over here and we can verify the variable value by running the command equal so that we have set the correct value over there so I'm just gonna paste it over here and here you can see the value is set to debug the next environment variable which we need to set is for our log file and the variable name should be TF log path and the value should be the path of the log file where you want to store the terraform log so here in my case this is the path where I will be storing the terraform log this path May differ based on your operating system and your directory structure but you can update the path as per your need so I'm just going to copy this command over here go back to my terminal and I'm just gonna paste it over here and we can verify by running the command PF log path and here you can see this is this has been exported successfully now after exporting the environment variable we will switch over to the terraform project this is my terraform projects and we will run some terraform commands so that we can generate the logs so this is one of my simplest terraform project which is running on AWS I'm just trying to set up an ec2 instance onto my AWS account and the instance type is T2 dot micro I'm just setting a very small virtual machine onto my AWS so it's very basic example I'll drop the link of my this GitHub project into the description section so you can follow this and the commands which I'm just gonna run the First Command which I'll run is data form in eight the second command which I'm just gonna run I'll just clear the screen over here there are formed plan and the last command so that I can apply the changes here you can see in the terraform plan it's planning to add one Resort but it has not added yet so the final command which I'm just gonna run is data from apply so I'm just going to run data form apply and it will create an easy to instance onto my AWS account I'm just gonna type yes and it might take a couple of minutes to set up an easy to installs as you can see my terraform apply command has successfully finished and I am able to create an easy to install AWS and you can verify onto my ec2 dashboard you can see terraform ec2 and this is the same thing which we have provisioned using the terraform and if we jump into our code then you can see terraform ec2 so our ec2 instance has been created successfully the next thing which we need to verify is our log file so go back to your path so this is the path which I have given user Rahul documents and log and if you check our uh you know environment variable then it is like same path user travel walk documents log and debug.log file the name of my log file so here this is my debug.log file and you can open with any of your editor so I'll just open with the sub line and here you can see these are the logs which has been generated and we can find our ec2 instance here also so we can type example and here you can see AWS underscore instance ec2 underscore example so these are the logs which has been generated with our terraform project and you can verify the same thing from our Visual Studio code so here you can see ec2 example so I just wanted to show you the log stress which has been generated once I have set the log label to be debug so that was just in one example where you can set the log level to debug and that was just a very small project but if you have a big project then you can set the log level based on your requirement like error info press or form now the next topic which we are gonna look for debugging is the validation we have a terraform validate module which can really help for troubleshooting and debugging purpose so let's switch over to our desktop once again and see the terraform validate module again jumping back to the project which I have explained to you earlier for setting up the debug log level so here I have declared a variable inside that we have created a validation block so here uh I'm just gonna put some validation condition so that before creating any ec2 instance we should validate what kind of a ec2 instance we are just going to create so this validation condition puts up a check like you can either create a nano micro or small ec2 installs and if you are trying to create any other ec2 instance let's say large or extra large then it will not allow you to create it we'll just throw an error message into the logs so with that way you can put some kind of a certain validation inside your terraform projects to verify this what I'm gonna do I'm just gonna change the type from T2 dot micro so I'm just going to put the two dot large and I'm just gonna save the file and from the terminal since I have already run the terraform unit command so I don't need to run it so I can run the Terra form command and here you can see you got the error message invalid value for the variable because our validation has caught and here you can see we are just trying to pass a T2 dot large but the allowed values are Nano micro and small so here you can see the error message also like uh invalid instance type you can only choose T2 dot Nano micro or T2 dot small so this kind of a validation which you can also include inside your terraform project which can help you to put some error full messages into the console so whenever someone is trying to run or execute this project then he will or she will face this kind of error message onto the console I hope you liked the today's session on terraform debugging and validation and I hope these tips can help you to improve your terraform projects troubleshooting and debugging today we are gonna discuss one of the utility of a terraform that is terraform version manager and this will help you to manage the different version of a terraform which you can run and install onto your development machine this utility terraform version manager doesn't come default with the terraform you need to install this utility explicitly after installing the terraform but you might be wondering like why do we need to have a terraform version manager so as a typical software development cycle terraform releases the new versions of a terraform which either include bug fixes and the new feature but sometime what happens is like with the newer version of our terraform some of the provider or some of the plugins or the existing stable plugin doesn't work so in such scenario you either need to roll back your terraform version or if you are running some older version of a terraform then in that case you need to upgrade your terraform version so in such kind of scenarios where providers and plugins are not compatible with the latest version of a terraform then in that case you can use the terraform virtual manager to roll back or either upgrade your terraform version well this is just an example which I'm just talking about but you can either roll back you can either upgrade your terraform version so I think this utility is a really good to have and you must install this utility along with your terraform let's switch over to desktop and see how we can install terraform version manager as I have already told you terraform version manager doesn't come default with the terraform you need to install this utility separately and this is the GitHub page which you can follow and I'll also show you like how you can install it so I'll put the link of this GitHub page into the descriptions action so once you open this page and if you'll follow along with the instruction then you will find the respective instructions for Mac OS and Linux if you are using Mac OS and you are a fan of a brew then you can just copy this command to install onto Mac operating system or else if you are using a Linux operating system then I would recommend using this instruction where you need to clone the GitHub repository and then you need to transfer the executable into the print directory I'm just gonna use although I'm using the Mac operating system but I can pretty much follow these instructions for installing the terraform version manager I'm not just gonna use the Brew I'm just simply going to follow this manual way of installing the terraform version management so I'm just gonna start with the cloning the GitHub repo offer terraform version manager copy this link over here go to terminal and here it will just clone the GitHub wrapper second it will export the environment variable or add to batch profile so I'm just going to copy this command and paste it over here after that there is a one more command for bash RC so I'm just gonna copy it it is also exporting the path and I'm just going to paste it over here and once you're done with these commands then you can run the command TF EnV Dash V that will verify the version which you have installed so here you can see the version which I have installed is 3.0.0 after installing the terraform version manager let's run some data from version manager commands to verify the available versions of a terraform so the command which I'm just gonna run is terraform EnV list remote and here you can see it is giving you the list of a version so these are the versions which is available for you for installing the terraform so here you can see this will start from I think the very basic or the first version of antenna form and it will go all the way to the latest version that is 1.3.0 Alpha and here you can see the latest stable version is 1.2.6 so here you can see uh I mean it will show you all the available versions of terraform till the date now let's assume you want to install some older version of a terraform then how can you do that using the terraform version manager so the command which you need to use is terraform e and B Then followed by install and the version so I'm just gonna install the version two point let's say four [Music] and here you can see it has installed the terraform version 1.2.4 one more interesting thing you can install multiple version of a terraform using terraform version manager and you can set which one to use it so I'm just gonna install one more version that is 1.2.3 and after that we will tell terraform environment here terraform version manager to use one 1.2 Point uh let's say three and here you can see it is switching the version from the 1.2.6 to 1.2.3 and if you're not sure like which version of a terraform you should install and you want to install the latest version of a terraform then what you can do you can simply run the command TF EnV followed by install and then you can just simply Supply apply the letters so it will install the latest stable version of a terraform onto your system also the good thing about this terraform version manager is you can install multiple version of a terraform but you can explicitly tell terraform version manager to use which version of a terraform so here you can see I have you know tell terraform version manager to use 1.2.6 so already I have installed 1.2.3 as well as 1.2.4 so I have the multiple version which is already installed but I can tell like just to use the 1.2.6 which I want to run onto my machine so these are the benefits of using terraform version management hello there Rahul this side and this is one of the most requested videos from many of you guys and like how to import the resource back into our terraform code so I have prepared this session where I'll be taking two examples in the first example I'll be just creating an easy to instance on AWS manually from the web GUI console so where I'll go and create an ec2 instance and then I'll use the terraform import command to import that easy to instance back to our terraform code and once I have imported that resource then I'm just gonna write the terraform code or reconstruct the terraform code for that particular ec2 instance so that is going to be one basic example so that you know like how terraform import Works second we are just gonna take one more example where we will be creating an S3 bucket manually from again from the GUI console of AWS and then we are just gonna run the terraform import command to re-import the whole S3 bucket back into our terraform code and reconstruct the terraform code for our S3 bucket so these are the two examples which I'll be covering this session but these Concepts and the command are pretty much common uh which you can use uh whenever you're working with any other resource on AWS as well as on a Google cloud or Azure so let's switch over to my desktop and let's see some demo in action let's try to understand the situation over here so on the right hand side you will see this is our AWS environment and in this AWS environment we have already started an ec2 machine which is running and for that ec2 instance we have got a unique ID associated with that ec2 instance but that ec2 instance has been started manually or someone has already done in the past but if we'll take a look on the left hand side then we don't have the terraform code for that particular ec2 machine so whenever we talk about terraform then we should have a infrastructure as a code for each resource which we are trying to start on cloud environment but in this scenario we don't have the code or the terraform code for that particular ec2 instance so so I'm just talking about this code this code is completely missing so in that kind of a scenario we need to import the resource back and reconstruct our terraform file so that we can later on use the terraform init plan and apply command now you might be wondering like what's the syntax or what's the import command to import any resource from the cloud environment so here if you look look carefully over here into this screen then I have written and Syntax for importing a resource so first of all you need to write terraform then import and here you need to mention the resource type over here after that you need to mention the resource name and the resource ID so if you compare with the current example so this resource ID we are going to put I one two three four five this is just an arbitrary example I'm just taking but I'm just trying to refer to this example over here onto the top and the resource name this resource name you can keep anything whichever you want so it's up to you you can keep any resource name of your choice but the resource type you need to mention the exact resource type so if it's easy to instance then you need to use AWS instance over here and if you are using S3 bucket then you need to use that S3 bucket name over here so you need to carefully Define or put the AWS instance type over here so here is the example command which is I have written considering the scenario which I have explained to you so here this is terraform this is import and here I'm just writing the type of the instance that is AWS instance then the name which is easy to example and the ID which I want to import path now we have a little background on how to import the resource back let's try to simulate the scenario so first of all I'm just gonna do I'm just gonna manually create an easy to instance onto my AWS console so this is my AWS account I'll just go and click on launch instances I'll put the name of the instance that is easy to example I'm just gonna use any Ubuntu machine and here I'm just gonna verify all the details so I'm just gonna use T2 micro because I'm just doing it for demo that's why and here uh let's keep the key pair name I just don't want to associate any key pair and I will just go with the default configuration actually I just wanted to set up something uh for ec2 instance and I'll just click on the launch instance oh I need to create the key pair so I will just go over here and I will just select this one which I have already created and I'll just click on launch instance so this will set up an easy to instance for me and I think it's already done it so if you go on instances so you can see over here so this instance is just getting started so the key thing which I you need to remember over here is the instance ID which you can see over here I'll zoom in a bit so this is the instance ID which is important for us because that instance ID we will be needing for importing our resources so now I'll switch back to my ID and we will run some terraform import commands okay so here is my ID and this is the terraform code which I need to write so first of all you if you look carefully so I have written only the resource block over here the resource block is empty because we have not imported anything yet so first of all just create a provider since I'm using AWS that's why the provider over here is AWS and the resource type so here I'm just trying to import the ec2 instance so that's why the resource type is AWS instance and here ec2 example this is just my own defined name which you can keep by your own choice so and after that the body of the resource should be empty because we are just trying to import the resource all right now we need to create the terraform import command so just this is an example I have already written for terraform import command which I have commented so here we need to replace this instance ID so I will just go back to my AWS console and here I will just copy this ID go back to my IDE I'll just replace the command okay so here I'm just trying to write the terraform import on AWS instance the instance is ec2 example which is a user defined name and the instance ID so this is the command which we are going to use for importing the resource let's copy the whole command and go to your terminal so this is my terminal and just paste the command and hit enter and here you can see import successful which means you have imported the ec2 instance from AWS console or AWS environment to your local terraform workspace now once you have imported the resource from AWS console you need to reconstruct the terraform code for your ec2 instance so as of now you can see our body is still empty but we have already imported our resource so what we are gonna do we are just gonna insert the missing tags or missing attributes which is necessary for creating an ec2 instance so the important attribute which we are talking about is the Ami the machine ID which is needed for provisioning and ec2 instance the instance type which is T2 dot micro if you remember previously from the UI console we have set up this T2 dot micro and the tag name tag names are kind of optional if you have inserted over there then just try to keep the same tag name all right so now we have reconstructed the body of our ec2 instance and we have already imported the resource now we need to verify that our terraform code is correct in accordance with our ec2 instance which is running on AWS environment so for that what we can do we just need to run the terraform plan and after that we can just run the terraform apply command so both the commands should be successful and it should not create any new ec2 instance because our ec2 instance is already running our aim is to recreate the whole terraform code not to restart or not to recreate the resource so here once we will run the terraform plan and once we will run the terraform apply command then nothing will get provisioned it will just complete successfully without adding or removing any new resources let's open the terminal over here and the next command which I'm just going to run is terraform plan just to verify that whether we have imported the resource successfully or not so the command which I'm just gonna run is terraform plan and here you can see after importing the resource we are able to run the terraform plan command and it says like no changes your infrastructure matches the configuration so that means we have successfully imported the resource pack and similarly if we are gonna run the terraform apply command then also it's just it's just gonna run successful without adding or destroying any resource so I'm just gonna run terraform apply and here you can see all apply complete without any resource has been added or destroyed so that means you have successfully imported the resource back into your terraform code now we have seen in the previous example how to import an ec2 instance now the next example we are going to see how to import an S3 bucket back into our terraform code so for that I'm just going to first create an S3 bucket so I'm just gonna go over here onto AWS console and S3 and then click on create bucket and here you can mention the name of the bucket so I'm just gonna keep my demo J hook bucket just for an example I'm just going to correct the typo name over here sorry it should be bucket yeah I'll just go with the default configuration over here and I'll just create a tag also so I'll just put the name and I'll just put the name of the tag that is Trust sorry bucket and after that I'm just gonna click on create bucket it might take few time and here you can see our test bucket has been created now we are just gonna re-import this test bucket into our terraform code and now I'll switch over to my ID and then we will run the terraform import commands so this is my ID and here we need to reconstruct the terraform import command with the correct bucket name so if you go back to our browser and here you will see the name of the bucket is my demo j-hook bucket that's the name of my bucket so if you go back to IDE then you will find I have written the command with the bucket name so the command is again pretty simple terraform import the resource name is AWS S3 bucket this is the resource type name for AWS you need to be really careful and then uh the resource type name this can be user defined name but this one should be AWS S3 bucket because it signifies the resource type and this one signifies the name which you are trying to assign to that particular type and this is the actual bucket which we have created over there onto GUI so this is the command and also here I'm just creating the resource block which is empty because first of all I just want to import and also whenever you're trying to import the bucket then you need to create the AWS S3 bucket ACL resource block also otherwise you will not be import all the attributes of S3 Bucket from AWS all right so now we have placed everything uh so I'm just gonna copy this command from here go to my terminal and paste the command over here and hit enter and here you can see we have imported the S3 bucket uh successfully onto our uh local workspace now we have only imported the one part of the S3 bucket the next part which we need to import is the AWS S3 bucket ACL and the command is again pretty simple terraform import type of the resource that is AWS S3 bucket ACL and the name which is I am taking over here is example so I'm just kept the same thing over here and the name of the bucket for which this AWS S3 bucket ACL belongs so that's the My Demo j-hope bucket which is same as over here so I'm just gonna rerun this command also so that we can import the second part of this AWS S3 bucket ACL also so I'm just gonna head over to terminal I'm just gonna paste the command over here and hit enter and here you can see it has been imported successfully now the next thing what we are gonna do we are just gonna reconstruct the piece of code in our terraform file coming back to my code so here first thing which we need to insert the attribute is the bucket and that should match with the name that is bucket name and this is the bucket name which we have we are trying to import then secondly the tax which is the important attribute so this is the tag which we have created at the time of creation of a S3 bucket manually from the GUI console so these are the two important attributes which we need to put in over here and secondly uh for AWS S3 scl bucket we need to I have just copied it already so you need to reference the original bucket so here AWS S3 which is this one the my test bucket which is this one and the ID so the whatever ID which we have got in AWS the same ID we are going to refer over here so this way this is going to point to this particular bucket so these are the block which or the terraform code which you need to fill in after importing the resources after that we are just gonna run the terraform plan and then we are just gonna run the terror from apply command and again same this data from plan and terraform apply command will not do anything but it should complete successfully so that you can verify that you have important imported everything successfully all right so this is my terminal and I'm just gonna clean the previous output the next command which I'm just going to run is terraform plan [Music] and here you can see it says no changes and your infrastructure matches the configuration which means we have successfully imported all the resources related to our S3 bucket and here you can see the resources name that is our S3 bucket and AWS S3 bucket ACL so these two resources we have imported successfully and at the same time if you are trying to run the Terra form apply command also then also it's gonna execute successfully without adding or changing any kind of a resources so here you can see no changes your configuration matches the configuration so this is how you are gonna import the resource I have just shown a very basic example for ec2 instance and S3 bucket but if you have any kind of a resource which is already provision then you need to look for that resource definition uh like how you can you create that particular resource using terraform and then you need to run the terraform import command and reconstruct the terraform code for that particular result I hope this tutorial will help you to understand the concept of a terraform import and these terraform import concept can be used for any Cloud environment whether it's a Google cloud or whether it's Azure so you can just look for the like actual code like what kind of a code or what kind of a resource you are trying to provision and then try to uh look for the IDS which are associated with that the resource and try to run the terraform import command and then you need to reconstruct the whole code of your terraform and that you can reconstruct by referring to the terraform documentation because they have a really good documentation for all these so you just need to reconstruct the body of your terraform code and then you should be able to reconstruct the whole terraform code for already provisioned resources so I hope you liked the today's session on terraform import and if you are interested into the similar concept on terraform or devops then please consider subscribing to this Channel and if you have any comments or suggestions then please put down into the comment section below and I'll try to answer those so see you into the next session of a terraform till then take care and bye bye
Info
Channel: Rahul Wagh
Views: 31,562
Rating: undefined out of 5
Keywords: terraform, terraform full course, terrafrom beginner to expert
Id: OHzZ7KuioMA
Channel Id: undefined
Length: 325min 40sec (19540 seconds)
Published: Mon Oct 02 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.