Terraform debugging and Validation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
debugging is a very crucial when it comes to the development insufficient logging or the missing lock dress 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 bond in the second half of this session we'll 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 one or info the second variable where you will be mentioning the log file name and the variable name will be df log path and inside that variable you will be mentioning the log file where you will be storing the log generated by your teraform project 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 set 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 echo so that we have set the correct value over there so i'm just going to 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 df 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 gonna copy this command over here go back to my terminal and i'm just going to paste it over here and we can verify by running the command df 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 command 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.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 going to run the first command which i'll run is teraform innate the second command which i'm just going to run i'll just clear the screen over here terra form 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 going to run is terraform apply so i'm just gonna run data form apply and it will create an ec2 instance onto my aws account i'm just gonna type yes and it might take a couple of minutes to set up an ec2 instance as you can see my terraform apply command has successfully finished and i'm able to create an ec2 instance on aws and you can verify on to my ec2 dashboard you can see terraform ec2 and this is the same thing which we have provisioned using the terraform and if you 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 you know environment variable then it is like same path user rahul walk documents log and debug dot 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'm just open with the sublime 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 which 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 level 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 uh requirement like error info trace or warp 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 i am just going to put some validation condition so that before creating any ec2 instance we should validate what kind of 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 instance 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 will 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 going to do i'm just going to change the type from t2.micro so i'm just going to put t2.large and i'm just going to 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 teraform plan 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.large but the allowed values are nano micro and small so here you can see the error message also like invalid instance type you can only choose t2.nanomicro or t2.smart 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 on to the console i hope you liked today's session on terraform debugging and validation and i hope these tips can help you to improve your terraform projects troubleshooting and debugging if this is the first time you're coming to this channel then i have a complete terraform list if you're trying to learn the terraform i'll put the link into the description section and there is a complete playlist where i have covered the various topic on terraform and if you have any suggestion or any issues then please put down into the comment section and i'll try to answer those and if you are interested in the similar content on a devops on a weekly basis then considering subscribing to this channel where i keep on sharing similar content on a weekly basis see you into the next session of terraform or devops till then take care and bye
Info
Channel: Rahul Wagh
Views: 8,621
Rating: undefined out of 5
Keywords: terraform, debugging, validation, Iac, devops
Id: MRPrcsFFEO4
Channel Id: undefined
Length: 7min 55sec (475 seconds)
Published: Mon Aug 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.