Terraform: How dependency Lock file and terraform state file works? | Terraform interview questions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when I connect with the individuals who are preparing for devops interviews they frequently highlighting the significance of infrastructure and automation tools like terraform in these interviews this prompted me to deal these topics and provide in-depth discussions and practical demonstrations in this video hey folks I am Yar Shankar from Galaxy Technologies in this video we are going to see what is dot terraform directory then dependency lock file that is terraform.lock.hcl then terraform State file that is terraform.tf State file how it works then backend in terraform I mean to say remote backend how we can use it next how to use terraform State file in a team so this is one of the commonly asked interview questions so to demonstrate this one even I have prepared all this I will try to cover entire stuff in a single video if it doesn't work I will be split into two parts and I am going to demonstrate each topic with the lab so grab your cup of coffee and sit comfortably if you are new to terraform then please go and watch our other terraform related videos where we have shown how to install terraform on Windows how to write first terraform file how to create VPC how to use variables and how to set up kubernetes cluster and many more so these Concepts helps you to understand current concept more clear all right now let's get back to our discussion so for this one as I said we are going through with the labs I have already prepared couple of terraform files and I just named it as versions based on what kind of topic we are discussing we are going to use that terraform file I have mentioned all the required resources links in the description of this video there you can find this GitHub link as well first let's see the V1 terraform dir demo in this I am using two blocks that is provider and resource block so in the provider we are using AWS as our provider and the reason Us East one and this I have already explained in my other video that is how to write your terraform file next we are creating a resource that is AWS instance and this is the Ami ID we are creating T2 micro and server name is going to be demo server okay so first we are going to use this code let me copy this code in the local system I have created a directory called the terraform State file let me copy it over here so to copy I am going to our Visual Studio code I'm already in that directory you can see here terraform State file directory let me open the new file and I'm just copying it and let me save this file as main.tf okay that is how we are going to use the naming convention in the real world so same I am trying to follow over here so this is your terraform file so that is the reason it is dot TFI so in this directory we have only main.tf at this moment now I would like to execute this terraform script for that we need to open the terminal so go here terminal new terminal so let me open this one and I'm in the current folder that is terraform State file now if I check the resume main dot efl I hope you know the terraform commands if you don't know again you go and check with our previous videos I have already explained that so let me execute our first command that is terraform init okay so initialization let's see what will happen and just see over here we have main.tf at this moment let's see what will happen once we execute this command you can see here initializing provider plugin it is trying to pull the provider that is hashicorp AWS version sorry version 5.15.0 so this is the AWS provider version if you want to check it out you can just go and search for AWS provider terraform so AWS provider over here if you check use provider and the current version is 5.15 this is the latest version it same thing it has been downloaded but where does it kept that is where dot terraform comes into the picture so whenever we are using any plugin any plugins or providers that will be downloaded under dot terraform so if I open this one you can see here provider registry and it downloaded terraform provider AWS version 5.15.0 some cases we will have more providers okay in my organization we are using AWS and data doc because both are required okay based on what is the requirement we will have multiple providers but in most of the cases you can see the cloud provider over here sometimes in the real world we will call other modules from the other GitHub repositories or bitbucket repositories those also get copied over here that is to Advanced concept by for now just think that it will be having all the providers information in the data tariff that's all about the dot error form now let's see the other one that is dot terraform lock.hcl so whenever we are running this terraform unit at the same time even this terraform.lock.hcl also got created and if I click on this one just to open and if you see here it is having the information of the providers answer and version 5.15.0 and hashes okay some additional information is it is there so what is the purpose of terraform lock.hcl it is quite simple whenever we are developing terraform files we will develop with based on the current provider version what is our current provider version that is AWS V 5.15.0 based on this one we are going to develop our terraform files let's take that after some time you found your new AWS provider so whenever you have your new provider if you download it your current scripts might work or might not work because sometimes newer version can able to understand the scripts what we have written sometimes there might be little bit of changes are required to overcome this problem this dependency lock file will help so this dependency log file that is table from lock.hcl will have the information of the what is the provider version so even the latest one will come it doesn't download the latest one so just to check it out okay let me try to run the terraform in it once again you can see here it is saying that reusing provider version hashicorp AWS from the dependency log file so from this file okay it is using the same provider just to experiment okay how this terraform log file does work so we don't have the latest AWS provider version I will try to downgrade the AWS provider version let's take that I want to use AWS provider version Which is less than 5 version that is where I have stored this one in the GitHub repository now let's go to the dependency lock file demo over here if you see okay we are using the same code but extra what we are doing is we are adding one more block called terraform block over here we are mentioning that AWS provider version should be less than five I am saying that I don't require the version greater than 5 less than five let me copy only this snippet and go here and I will add on top of provider and let me save this file okay this time I will try to run the terraform in it once again and we'll see what will happen you can see here error failed to query available provider packages so reusing the provider from the dependency lock but the problem over here is in the dependency lock we are trying to use the version 5. something but here we are looking for lesser version so this is how dependency log file helps us to prevent to use other provider versions in this code so that our code no need to update it will work with the version 5.15 so there won't be any issue but I still want to use the lower version what I can do in such cases you just need to remove this one so that whenever you remove it okay I'm just going to delete it okay and another thing is you just check over here we have only one provider let me rerun this one okay terraform init and this time it is going to download the version which is lesser than to 5 that is V 4.67 and same thing will get update over here okay it's completed and over here you can see there are two AWS provider versions one is 5.15 another one is 4.67 and now it will come up with the latest version based on our main.tf file we are mentioning version as 5.0 so it will be locked with the lower version than 5 okay so that is how dependency lock file or dot terraform.log.hcl does work all right now let's move on to the next step that is terraform State file now this is how our environment is defined at this moment this is my local system where I have installed a terraform and I have written a terraform file as well over here and we have AWS now whenever we run this terraform file how to run this terraform file initially we run terraform plan then everything is okay then we are going to run terraform apply whenever we run this terraform apply at this moment it is going to create an ec2 instance that is how we Define right that is demo server it is going to create it so whenever it creates a instance this instance related information within this AWS will be stored in the terraform file this terraform file is a Json file and this information will be stored in this terraform file now what is terraform file terraform file is a file which contains information related to your current infrastructure which is provisioned by the terraform so whatever is provisioned by the terraform okay which is already present in our AWS account that will be updated in our terraform file and this terraform State file will be created whenever we run terraform apply at first time subsequent times it will be keep on getting updated and this terraform file will be created in our local system so to demonstrate this one I am going to execute further commands in our terminal and so far if you see we have just executed terraform in it so further commands terraform validate we have okay it is not necessary but I am just running it I am sure that this code is going to work okay this is saying that the configuration is valid and then terraform plan okay instead of running over here I can do one more thing that is in this directory I can open git bash so that it will be easy for you to understand so I will run the terraform commands over here and you can review the code over here so that it will be easy main.tf so this is the complete code and I am going to run the commands over here so you can see here I'm in the main.tf and LS minus Lea you can check the dot terraform under data terraform.log.hcl now terraform plan so terraform plan is like a dry run okay it is going to create one resource that is easy to instance and if I go and check AWS instance Amazon server it is going to create it to actually create terraform apply so this is the command and one more thing I forgot to show you so over here if you check now it is creating the terraform State file okay so it is creating one resource and yes to execute it and if I go and check it too it doesn't have any information at this moment once we have created this you can see the content over here and I just logged into my AWS console so this is my AWS console I have already one gitlab Runner and one demo server has been terminated so it should create a new ec2 instance so let's go back sorry so it is creating an ec2 instance okay it has been created successfully let me go and check it out and yes it has been created successfully and another thing is if I go and check my terraform stateful it has the information related to our AWS instance because the current infrastructure information get updated over here and even you can see what is the instance type T2 micro instance ID all this information is updated over here okay now if I run this command once again I'm going to say terraform apply it says that this is already up to date our terraform script is up to date yep you can see here terraform has compared your real infrastructure against your configuration and found no differences so no changes are needed that's what it is saying just to test it out we'll do one thing we'll copy this terraform State file into some other location okay so I will create a temporary folder over here temp and I just copy this file into the temp where we are working so we don't have any any terraform State file over here now let's try to execute the same command and we'll see what will happen okay you can see here it is trying to add one resource that is the ec2 instance which we have already created that is the importance of terraform State file so we need to make sure that our terraform State file is in the right location so let me cancel this operation so no whenever I say no you can see here this terraform file is disappeared and I am going to copy it from the application to current location okay mode mode and now I am deleting this temp temp directory okay so that is the importance of terraform State file now what is the definition of terraform State well terraform State file is a file which contains current running environment information which is provisioned by the terraform okay that is the terraform State file now where is our terraform State file is located it is in our local system but keeping it in our local system is not advised why because if our system is get corrupted our accidentally if you delete this file then how do you know that what infrastructure is running whenever you try to run terraform apply again it will try to reproduce the entire infrastructure which creates the mess that's the reason we need to maintain this terraform State file in a secure location that is where backend comes into the picture there are multiple backends if you take AWS S3 you can use it as a backend for Azure Azure blob storage you can use hashicor fashion Cloud you can use like that various backends are available for us we are already using AWS we are going to use AWS S3 bucket as our remote back-end for that of course we need to provision an S3 packet before provisioning S3 bucket I would like to give you an update I have released a couple of new courses on udemy platform if you are interested you can check it out this is devops Project 2 which which is having a advance to our devops project one so I have discussed about Jenkins files sonar Cube terraform and few other new tools even Prometheus Griffin also discussed at this moment it is highest rated in the devops segment and also we have released one more new course that is how the real-time microservices does work in the kubernetes okay this is done by Mr PR ready you can check out this course again we have terraform dedicated course over here to provision the three-time architecture with help of Jenkins so these are the courses we have released recently you can check it out I am providing the links in the description of this video and also you can follow me in the LinkedIn in the LinkedIn I am going to publish some of the Articles related to AWS and cloud and also if you achieve your Milestone okay please tag me so that I am going to congratulate each one of you who has completed it apart from this I am also in Instagram that is Revolution if you want to know about me personally then you can connect me over here that's all I would like to share and let's jump back to our discussion so now we need to provision our S3 bucket to provision our S3 bucket I have already updated the code in the of V3 so V3 terraform State file demo so over here we have a resource called AWS S3 bucket and we are creating and the life cycle if you check prevent to destroy nothing but we are not allowing to destroy this S3 bucket that is how we will do in the real world so similar kind of thing I am trying to replicate over here because if we lose this bucket you are going to lose your terraform State file so make sure that this is not destroyed by the terraform so let's go back to our code and I will just add this snippet save this resource and let's run it again let's run our terraform plan so it is taking little bit of time so that from next update onwards I will go with the directory terraform apply itself okay it is trying to add one resource that is S3 bucket terraform apply minus minus Auto approve and you can see here it is compared with our current infrastructure and it is going to create only one resource because of the terraform sorry terraform State file otherwise it might create a two resources all right so it has been successfully completed and if I go and check in my S3 bucket we don't have any buckets before creating it and go to buckets and you can see here velocity terraform State file this is the one which just created all right this is how we can create the S3 bucket now we can enable or we can use this as a backend to store our terraform State file so this became a lengthy video so I am stopping it over here I am going to show you that in the next video thank you I hope this video is informative if you like this video please give me a thumbs up so that it will helps to this video to reach more people thank you and see you in the next video
Info
Channel: Valaxy Technologies
Views: 3,628
Rating: undefined out of 5
Keywords: aws, devops, valaxy technologies, security, automation, serverless, linux, valaxy, valaxy devops, devops online training, devops realtime training, valaxy devops training, valaxy devops videos
Id: -WPo4SrCswQ
Channel Id: undefined
Length: 19min 13sec (1153 seconds)
Published: Fri Sep 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.