What is Terragrunt and how to use Terragrunt? | Terragrunt Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well talking about the terragrant it's an interesting tool which you should use along with your terraform code so if you're trying to manage your infrastructure using terraform then i must say you should use tera grunt whenever it's possible when i started working with the terraform honestly i didn't know that teragrant existed so initially what i was doing i was trying to prepare my code using terraform and i was trying to place those code into the different environment like development test staging and production so the same piece of code i have placed into the all four different environment and when i look from the programming perspective then it's not a good practice to duplicate the code across the environment so the next question comes like how can you remove the code wkc inside your terraform projects and for that reason we use teragrand so teragrant is a tool which will help you to reduce the code duplicacy across your terraform projects well there are some other benefits of using teragrand but removing the code duplicacy across your terraform project is one of the coolest feature which i felt after using the teragrant so what are the key takeaways from today's session so we will start from scratch and we will first install data form then after that we are just going to install tera grunt and after installing both terraform and teragrand we are going to create an example where we are going to set up an ec2 instance running on aws using teracurrent and for that we are just going to use the ec2 module so our code for creating an ec2 module will be common and we will be using two environment one is the development and second one is the testing or qa environment so we will just set up this example we will start an ec2 instance and everything we are just going to do we are just going to do with the data current so before we jump into the session let's try to understand the example first so here this is the code for starting an ec2 instance but this code is for terraform not teragrant so as you can see this is the code which you need to write if you want to start an ec2 instance okay that's fine but what if you want to start the same ec2 instance on development testing staging and production so what you will do you will just simply copy paste and you will change the parameters like for development you might want to start a t2.micro or small machine but for qa you want to start a t2.medium and for stitching and production you might go with the t2.large so you will just copy this piece of code and you will change the parameter of the instance type and then you will just execute your terraform code so you are just trying to duplicate this code into the four different different environment so that's what i was trying to say so using teragrant we can improve this code and you don't need to copy paste the same code four times now the next question comes like how can you implement the same ec2 implementation using teragrant so for that what we are going to do we are just going to create a two environment so if you are working with the teragrand workspace then you need to create two directories one for development and another one for testing and each directory you need to create an hcl file that will contains your teragram configuration so just create two file inside each directory and let's take an example for development scl file so i'll open this file and inside that file i'm just going to import the ec2 module because we just want to create an ec2 instance on aws so we are just going to use the module definitions so that teragrant can start or set up an ec2 instance and we don't need to write those ec2 resource code inside our tera grunt configuration so the top three lines will contains the module definition which we are going to import from terraform so here we are taking the ec2 and just for another example if you want to start an s3 bucket or if you want to create an s3 bucket then just replace the definition and use the s3 module over here next thing if you want to change the parameters like if you want to start an small instance let's say t2 dot nano or t2 dot macro on development install then we are going to create some inputs variable inside this hcl configuration file so this configuration file will be different for qa so that you can supply the different parameter like for development you are starting a t2.micro but for qa you might want to start a little bit bigger instance so you can supply like t2 dot medium over there so these are the benefits you will get from teragrand so you will be importing one module definition like ec2 over here and after that you can just supply or tweak the parameters like which you want to ah start on aws so if you want to start small then just supply those parameter if you want to start a bigger instance then supply those parameter so this is just an example which we will be taking into the demonstration which we'll be seeing later also with the today's session i'll be sharing this guide where i have documented all the steps which is needed or which we will be performing throughout this session so just feel free to use this guide whenever you're stuck and you wanna just play around with the teragrand i'll drop the link of the guide into the description section and also if you are just getting started with the terraform and terra grunt then i have the first session where i have shown like how to install terraform and how to get yourself started with the terraform so i'll drop the link of that first session into the description section also so let's switch over to desktop and start with the deragrant as you can see on the screen on the left hand side this is my terminal where we'll be playing along with our terraform and terra grunt commands and on the right hand side i have prepared this guide and i'll share the link of this blog post into the description section of this video so we will be following this guide and here i have documented all the steps which is needed for get yourself started with the teragrand and whatever code we are gonna write for teragrand i have prepared a github repository so here uh this is the terraform topics that's the name of my github repository so i'll mention the same repository link into the description section so you can just go there and download all the codes which is i'll be showing into the today's demo to start with the first thing which we need is we need to install the teragrand and for that we need to download the binary so here is the link for the github repository uh from where we can download the teragrand binaries and based on your operating system like windows or linux you can just download the respective binaries onto your local system but before you install teracurrent onto your system there is a one prerequisite requirement that you need to install terraform beforehand and if you haven't installed the terraform before then you must install the terraform uh before proceeding with the teragrad so this is the instruction guide for installing the terraform and based on your operating system just choose whatever the correct instructions are for you so in my case i'm working on ubuntu as my base operating system so that's why i'm just gonna choose the ubuntu over here for installing the terraform and this is just a beginner session for a tera grunt so i just wanted to start everything from the scratch so first of all we will install terraform then after installing the terraform we are gonna install tera grunt click on this link for following the installation instructions and the first thing is you need to update the package manager so just copy this command from here and paste it onto your terminal so that you can update your package manager definitions after updating the package manager definition the next thing which we need to do is we need to install the hashicorp repository so i'm just going to copy this command i'll clear the screen over here and paste the command [Music] okay that's been done and after that we are going to add that repository so just copy this command from here from the guide and paste it over here after adding the hashicorp repository the next thing which we need to do is we need to actually install the terraform and for that the command is this one sudo epigate update and after that apt get installed terraform so i'm just gonna paste it over here and hit enter it might take some time so just be patient with the installation command and now my terraform installation has just finished and the version which has been installed is 1.2.3 and also you can verify the installation of a terraform by running the command terraform version so that means you have successfully installed the terraform the next thing which we are going to do is now we are going to install the tera grunt jumping back to our teragrand guide uh the link which we need to open is the teragrant github repository where we can download the binaries which is needed for installing the teragrant so this is the github repository where they put the release information along with the latest binaries released for teracurrent so this is these are the binaries so just choose whatever the correct version based on your operating system since i'm using the linux system so i'm just gonna use the linux amd64 right click on it copy the link and go to your terminal just use the command w get paste the url and hit enter so it will just download the linux binary onto your system but also you can just simply click on it if you are using the ubuntu desktop version then just simply click on it and it will just download it for you uh one more thing i forgot to mention so this is the terminal and this terminal is my virtual machine which i generally use for the demonstration purpose so that's why i'm using this terminal otherwise i would have done the same demo on my desktop but and this virtual machine is so handy so that i can destroy after the demo that's why i'm just using all this command line commands for installing the teragrant and terraform i'm moving further now we have downloaded the teracurrent binary now we need to rename it to teragrant so i'll clear the screen over here and i'm just gonna run the command and here you can see this is the teragrand uh linux amd64 which we have downloaded so we need to rename it so what we're gonna do we are just gonna use the command move and i'm just gonna mention the actual name of it and then after that i can mention data current so it is just gonna rename the file and i can verify it by again running the ls command so here you can see uh we have renamed the file and after that we are just gonna move this file to the user bin directory okay but before that uh we need to change the permissions so i'm just gonna use the change mode command and after that u plus x i just wanted to make it executable and after that the name of the file that's been done and after that we are just going to move that file to the user local bin directory so i'm just going to copy this command and go to your terminal and paste it over here hit enter permission denied so i'm just gonna need to use the sudo over here and that's been done you can verify the installation of a teragram by running the command data grunt dash dash version and it should tell you the latest version which we have installed that is 0.38 and you can verify it from the release page also so this is the latest version we have used for installing the data grant moving further the next question comes like how should you write your first implementation of your tera grunt so for that what we are gonna do we are gonna take an example where we are going to start an ec2 instance on aws using the data grunt and for that uh we are gonna use our workspace so first of all we are gonna see how our project workspace for teragrant looks like so if you scroll down further the guide then you will find a screenshot of my project structure which is over here on my intellij which i have already opened so this is the uh project workspace of my teragraph but i'll explain you first so if you switch back to the guide then here you will notice that there are two pro two folders one is dev and one is test environment so as i explained you earlier into the slide when i was explaining the teragrand so whenever you work with the teragrand then you need to define certain environments because uh you want to execute a piece of a terraform code which is a single code but it needs to be executed differently on a dev environment as well as the test environment so that's why i have taken an example where i have created a workspace for teragrant and inside that workspace you will notice there is a folder that is called dev and there is one more folder called task but in actual scenario there might be multiple environment like a development environment test environment staging environment and production environment so that is uh this is a very basic approach uh while working with the teragram so just create first of all the folder structure based on the different types of environment which you have in your current workspace the next thing uh the example which i am taking today is to start an or to set up an ec2 instance on aws so for that what you need is you need to fetch the module definition because data grunt itself doesn't store or doesn't create those kind of information which is needed to start a ec2 module although you can write your own configuration but it is always recommended to use the predefined modules by teragrant so here is a link for ec2 module if you click on it then it will redirect you to this page and this is the page where you can download the module definition or you can just copy the module configuration which is needed to start a ec2 module so if you go over here then you will notice that this is the module definition or a module configuration which is needed uh to create an easy to install so you can simply copy this configuration i'll explain you how to put this information into your teragram configuration but just so you know that teragrant work on a module configuration so to work with teragrant you need to define modules and what i mean by modules there could be a module for ec2 there could be a module for s3 bucket there could be a module for a vpc so these are just an example uh to work with a teragraph so that's why uh data cons doesn't store that much information but it just stores the configuration which is needed to fetch the module okay let's jump to our intellij and see how this is this has been written so this is my terragrand project and inside i have created the one test folder open the test oh sorry dev environment and there you will find a teragrand.scl file so this is the important file whatever folder or whatever environment specific folder you are creating it for teragrant then you need to place a teragram dot scl file so this is the file which will contain all the configuration which is needed for that particular environment so i'm just going to open that file and once you open this file this is a very basic file uh but in production and stage environment you will find a very complicated and very long uh teragrand configuration file but just to keep the thing simple so as i explained you uh we need to pull in or we need to fetch the module definition and you need to know like what kind of a modules you want to work with since i want to work with the ec2 module so that's why over here you will notice i am fetching the ec2 configuration or the ec2 module configuration and this is the source which i have already defined over here and if you go back to our browser where i have shown that configuration so this is the ec2 module configuration and the version is 4.0.0 so the same thing you will find it over here so teragrant will fetch that module configuration or model definition which is needed to start an ec2 instance so that's why you need to mention it on top of your teragraph.scl file similarly if you are working with the s3 then just fetch or just find the correct configuration which is needed to fetch the s3 module configuration from the terraform so that's the first line you need to write inside your teragrand.scl moving further there is a locals the locals are pretty much same as that of terraform locals but i'll explain the locals later because uh that will be needed into the later section where we will be writing the provider and the input blocks so the second important thing is you need to define provider but provider is just an optional thing either you can put the provider definition or either you can create your own provider.tf file so here i have just created this provider configuration so this is the syntax which you need to use and in case if you don't want to create a provider then also you can create a provider.tf file inside your terraform project or inside this dev directory also it is also going to entertain or it is also going to use those provider.tf configuration also but if you read the line number 17 over here if exist then it is going to overwrite so that means this is the terraform provider configuration which you put generally inside your provided or tf file but in case you want to override it then you need to put this line over here if exist and overwrite with teragrant so this line will going to overwrite all the provider definition if there is a provider.tdf file available inside the dev directory so it is just going to overwrite it and i just wanted to overwrite it that's why i have used this configuration so this is the provider configuration and i can show you the old example of mine so this is the example which i have taken previously so here in the provider configuration you need to define the reason your access key and the secret key so these are the minimum requirement which you need to provide inside the provider so here i'm just putting the same thing and overriding it so in case if there is a provider.tf available so it is just going to overwrite with this our custom definition so here i'm using the profile reason and the shared credential share credential file is just a simple file which contains your uh aws access key and the secret key so instead of this i'm just using the credential file over here so this is the second important block which you need to define inside your teragrand.scl configuration also we can parallely compare our terraform code without teragrand so that you can have a difference like why we need to use the teragrand so here this is the terraform code without data grunt so here you will see you need to define the provider that is aws because you are working with the aws after that you need to define the reason access in the secret key just to get uh to access your cloud environment secondly you need to define the resource uh since we are going to create an ec2 instance that's why we have defined this ec2 as an example or the resource block over here then you need to you need to define the ami and then you need to define the instance type that is going to be our t2.micro or medium or large and after that there is a tag and these are the variable so this is the variable like here this is the variable so here we are just using t2.medium so this is a very basic uh terraform code so here we have hardcoded quite a lot of things so here you can see we have hardcoded t2.medium and if i want to use t2.large in our stage environment then i cannot rely on this code i need to update this code and make it as a t2.large but what teraf grunt helps us is that you don't need to change your terraform code you can have that once you have written the terraform code you don't need to change it we will provide those configuration from teragrant and here you can define uh the inputs for that so here we have we are defining the instance type over here so instance type is defined over here in the local so that's why i have said i will explain locals later but here in the input blocks you can define what you wanna send in to your terraform code so here we are going to send the instance type so if you want to start a ec2 instance that is micro then you can send it over here into this one and it will just create a ec2 instance so you don't need to hard code this value at all similarly if you want to execute the whole thing into stage environment then you can change it and you can make it as a t2.large and the same terraform code will be executed with a t2.large so those are the benefits you will get with the help of terraform oh sorry tera grunt okay moving further after explaining the provider block the next block is the inputs block so here i'm just going to define the machine id because whenever you work with the aws you need to or if you are using the ec2 instance then you need to define the machine id so i'm just using this machine id and after this we need to define the instance type so for development purpose we have created a local variable and that you can find it over here so here you can find the local variable which i am fetching it from yml that i will explain it later so that is a one mole m one more level of extraction but in case if you are not interested uh in defining or local then you can just simply hard code it over here also so you can simply put a double quotes and you can just put a d2 dot or sorry d2 dot micro and that should work sorry i'm just making a typo here but this is the idea so and if you want to change it for a test environment or stage environment just just change the value and that will start the more higher powerful cpu so that's the idea behind the input block inside your teragraph i'm just going to revert my changes over here okay one more thing yeah that's been done if you follow along the guide then i have explained all the things in the details over here also like what is the module what is the provider details and and after that how to use the ami tags and the locals and the variable so that you can uh just go through in the guide also the next thing which we need to know is like what are the commands uh if you want to execute your configuration so the commands are pretty simple uh terra current plan uh terra contact grunt apply deragrant output and teragrand destroy so these are the very basic commands which you need to know to execute your teraf tera grunt configuration before running any command make sure you are into the correct directory where you want to work so i am into the dev directory because i just wanted to apply my dev uh configuration onto our aws environment so switch to the correct directory before you run any kind of a teracurrent plan or apply command let's run our first command and the first command is going to be deragrant init command now the next command which we are going to run is the plan command so copy this command from here go to your terminal and paste it over here so this command is just going to validate and tell us like how many resource which we are going to add update or delete so here you can see it has executed and it is saying like one resource to be added so teragrant plan command is pretty much similar to the data from plan command so it is not going to execute anything but it just going to tell you that we are going to create or change or destroy the resources so this is the information which you will generally get from data grunt plan command moving further the next command or the actual command which we are going to run is the teragrant apply command so this is going to create an ec2 instance onto our aws environment so copy and paste it over here and simply hit enter so it might take a minute or so so i'll be back once this data current apply command is just finished now it is asking for a confirmation do you want to perform this action so i'm just gonna say yes and i'll just wait again now my terraform apply command has just finished and you can see over here the message says one added so that means our one resource has been added and that resource is going to be the ec2 instance so let's switch over to aws console so this is my aws console for ec2 and here you can see the one instance is running click on it and here you can see the label which i have put it over there is the teragrand tutorial ec2 and you can verify the same thing from our development teragrand scl so teragrand tutorial ec2 so that means we are able to create an ec2 instance using this module and running the teragrant commands so this is how you are going to create the resources uh you or provision the resources on aws using the data current uh also parallely what we are going to do we are gonna take one more example of a test environment and i'm just gonna open the teragrant as seal over there and here we are gonna start one more easy to instance so that you know what we are talking about and how you can manage the different environments with the teragraph so here i'll just hard code it i'm just not going to use the variable so the variable name i can put it is like a d2 dot micro okay and the tag name which i'm using over here is the test so that we can identify that it is from test environment and although i have not put any specific tag so the development tag is this one teragrand tutorial ec2 but for test we have put a prefix that is test so we are just gonna apply this configuration and just gonna test it out like how you can execute the same piece of code so here you can see uh we are still using the same ec2 module we are not just going to write the actual provider or ec2 resource we are just using the tera grant or terraform modules to uh provision the ec2 instances using teragrant okay so just check the directory where you are so right now i am currently on a dev directory so i need to switch to test directory so i will just jump one directory up and after that i'm just gonna switch to my test directory check again so yeah i'm inside my test directory and after that the first command which i'm gonna run is teragrant init command now my terraform init command has just finished i'm just gonna clear the screen the next command i'm just gonna run is data grunt plan command and here you can see it is saying like one resource to be added so yes we are just going to create one resource after that i'm just gonna clear the screen the next command i'm just gonna run is teragrant apply command and i'll be back once the terrafor apply command is just finished and here you can see there is only one instance on to aws console uh that is from development environment and we are just going to start one more instance uh here it is saying to confirm let's just say yes and now i'll be back once this is ready now as you can see over here my uh thereof teragrand apply command has just finished and it has just started an easy to instance with this tag so i'm just gonna refresh the aws ec2 instance dashboard oh wait so here i have used the eu west one region so i have started a test instance into the different uh region of aws so this is right now it's a central reason so that's why i can't see the another instance so i need to switch the region so that's what you can see in the orange color that's the current region i am in so i need to switch to west one reason so i click on it and here you can see uh this is the test two instance so that was uh one thing which i didn't mention earlier so we are running the test instance in eu west region as well as uh on the other hand side the development instance into the eu central region so here is our test uh ec2 instance which we started using the data current so that's also one of the good example like of using the teragraph like you can provision so here you can see eu west one and in the development i can see eu central one so you can provision the resources into the different region uh just specifying the provider block and changing few of the parameters so this is how uh one example like to manage a different environment using uh teragrant the next concept uh in the terra grant is the common environment yml so as you can see over here so if i close all this so this is my terragrant project and here i have put a common environment via ml so why this why do we need the common environment yml so suppose uh you have a different different environment but there are some uh variables which is common throughout your environment so in such scenario you are just going to create this common environment yml your the name should not be common environment you can put any name of your choice but it just to keep the things simple i have me met the name as a common environment yml and here i have mentioned the variables which i want to be a common between the development as well as the test environment so once you specify the variable inside this yml then you can refer to that yml inside your teragrand scl so here uh this is the common environment yml which i just wanted to include and i need to use the function find in parents folder so that means it is going to look inside this parent folder so there is the child folder while teragrand ec2 is the parent folder so it is going to look into the parent folder for yml and it is going to fetch that into a variable that we have declared as environment variable and then it is going to use inside over here so this is where it is going to use that variable uh for assigning the instance type so this is also one example which i told you like i'll explain it later so this is the way you can use the common environment yml to provide some common variable across your environment and after you're done with the work then you can simply issue the command tera grunt destroy and that should destroy all the resources into your different environment so it will take some time to destroy all those results so i think that's not necessary part i'll just clean my development environment so that was our first session on how to get yourself started with the terra grant i hope you liked the today's session and the demo and if you have any question related to the today's session then please put down into the comment section i'll try to get back to you and if you are interested into the similar content on devops then considering subscribing to this channel where i keep on sharing the similar concepts on a weekly basis so see you into the next session of our devops till then take care and bye bye
Info
Channel: Rahul Wagh
Views: 27,519
Rating: undefined out of 5
Keywords: terragrunt, terraform, aws, devops
Id: XNpSSn-n-fw
Channel Id: undefined
Length: 31min 27sec (1887 seconds)
Published: Tue Jun 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.