Beginners Guide to Terraform with Azure: PART- 01

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to the channel today in this session we are going to discuss a topic which is really interesting if you are azure devops resource and if you write a code to automate your infrastructure with azure whether you use arm template azure cli powershell or even if you use the terraform so as the title suggests this particular tutorial is a beginner's guide to terraform with azure so if you are new to terraform and even with azure then this is the place where you can learn right from the beginning how do you set up the terraform how to use the terraform with azure to provision your infrastructure what are things you need in order to provision or to automate your entire infrastructure in azure with the help of terraform this module will be dividing it to multiple sections and will try to cover each and every aspect what you need as a beginner or as a new comer to understand the different topics of the terraform with azure just a quick introduction about myself i am rakesh suryawanshi i own this channel i am azure certified technical architect in this video we have divided uh the section into 12 different parts uh right from the installation of terraform to some of the advanced configuration which you will require including csv configuration configuring the multiple uh resources multiple instance of resources and things like that so which is going to be helpful for you so let's see how how do you like to associate with that this video is divided into 12 different section right from the installation of the terraform to some of the complex setup of your terraform infrastructures code when we are setting up the cicd pipeline deploying multiple environments or declining resource to multiple environments some of the advanced from commands and configuration which you might want to learn things like that and each section is you know configured to a timeline in your youtube video so you can anytime go and switch to one section from one section another section using the timeline and also we have the the section highlighted at the bottom of your window and so whichever section we are discussing that will be highlighted at the bottom section of your before we start discussing this topic please take a moment and subscribe to the channel hit the bell icon so that you get the notification for all my future videos and also you can scroll to the playlist and the videos which are already been uploaded on my youtube channel so if you are interested to any of the past videos which i have uploaded you feel free to watch those videos don't forget to hit on a like subscribe button if you have any question please write it down into the comment section i'll get back to you as soon as possible let's first of all understand what is terraform and why do we need so terraform is a tool written in hashicorp language which supports you to write your infrastructure infrastructure as a code in any cloud providers which is available in the market whether it's azure aws google cloud flare you just name it the cloud provider and you'll have the you know support available or the support available for those cloud provider in terraform so using the terraform you basically use those providers and write your own infrastructure as a code now question why do we need the terraform especially in azure because our topic is dedicated for azure so we'll check why do we need a terraform when we go for infrastructure as automation in azure whereas the azure is supported by the air template and the rm template is the way to poison the infrastructure so it's just rest api but now the problem we got with the arm template is just it's foreign forget you don't have a mechanism to roll back any changes which has been which went wrong things like that or let's say if you're using azure powershell or cli to provision any of the azure resources in on your azure subscription then basically in that case you it just let's say you run this as your cli it's just foreign forget if anything goes wrong you don't have the backup option to roll it back the changes whereas in the terraform basically telephone maintains the state of your changes so when you publish a change or run a command to publish a change onto a target azure or target cloud provider in this case azure so basically a terraform provisions a state or keeps a state of what has been run successfully and when you run next time let's say next time if you are trying to make an upgrade to what has been update created last time then it compares the state between the last deployment and then this deployment which is going on and based on this state comparison it makes a decision what exactly it requires to perform now is it going to be a differential update for example in this case a change which it requires to update or it requires to create the entire infrastructure or delete something or destroy something something like that right so it uses this state management within the terraform itself so stat can be managed in a multiple way which we will discuss it later on just but just to high level this is one of the best feature alike second is the all the arm templates are written in json so understanding and modifying the json when it grows up to thousands of lines so i've seen the project when you create the now the third and the last is you get the plan for example if i use the arm template to poison the infrastructure i have my ci cd pipeline in place so when you provision through a rm template you ask your pipeline to execute and create a resource and you with the help of arm template but you did not you do not have any option wherein somebody can validate what is going to be deployed and based on the those validation they make a decision whether do they want to make those changes or not whereas in the terraform you have multiple commands you have a command for it and apply in it and plan so plan basically generates the plan between what has been run in the last time and what we are trying to run it now so that that difference is something which is generated as a plan and that plan will showcase to you to to reviewer that this is what the changes it's going to deploy and then you a reviewer can make a decision by looking at the plan by reading the plan and they can make a decision whether they want to accept the uh request change onto their production environment or not so that these are the three four major differences which i found which are really helpful which you can use it and in your infrastructure is code and these are kind of a key feature we'll discuss through all of them in detail let's see so first thing to from our topic is terraform installation so let's install the terraform on our machine so currently for this demonstration i'm using a mac os or a linux server but the installation process for terraform is fairly easy you just need to download the desired version of a terraform from the terraform website and you just need to you know it's just either you download the exe or you download the zip file unzip it and then if you're using windows unzip it and then put it in your c drive or whichever place you want to put it if you're using macbook or linux or any any other system like that then you can use the brave command to download and install the terraform i'll show you how you can do that basically you can search for telephone download in google and then go to the link of the terraform repository here you'll find the terraform download link for your operating system specific download link follow through the link for example if you're using windows then go ahead and download the windows bit so it will allow you to download the exe and then if you are using mac os like me then go ahead and download this one as well now if your project is running to the older version of terraform then you can use the older version of terraform link available here go to the terraform link available here let's say i would like to go to the term link available here so you can go to the link and then based on your desired operating system you can go and download the link available now how do you set up a terraform installation guide you can find it here once you download the terraform on your operating system you can run the terraform command like the form version and if it displays the appropriate version like in my case i am getting a version as in 0.14.5 and the latest version is available as code.14.7 so this is how you can verify whether you have the terraform installed correctly on your operating system that was pretty much about the terraform installation next we are going to look at the project setup for our infrastructure so for that i recommend you to download the vs code on your machine so if i go to the vs code this is what my projects look like i'll close all the things here so first of all let's create a new open a new folder it is going to be a fresh new folder or new project which we are going to configure so we are going to call beginners go to terraform bgt and let's open this so now you can see that i have the empty folder here and here is my project i have started with it now what you can do is now let's understand different parts of terraform so in terraform we have variables dot tf file which includes all your terraform variables so you can declare the variables like that so i have added an extension for terraform so you can if you are new to visual studio code then you go to the extension and then search for terraform and then you should install the telephone extensions this one hashicorp expansion this is what the extension i'm using this will give you an intellisense while writing a telephone code so i was discussing about the variable sections variables is basically defined with the is the place where you define all the terraform variables you need to have a df either you need to have terraform of tfos you need to have if you want to take an output of any of your terraform resource property then usually you declares or the terraform output file you have the terraform main.tf file where you define your logics and usually i prefer to create a provider or tf file where i declare all my providers which i'm going to use within the azure within my terraform code so these are the five things you need to know so just to recap your variables where you are going to keep all your variables name for example let's say i'll call it prefix and then description so basically you need to define a type and then description uh what these variables is used for things like that you can also have variables with different types for example you can have a different type such as object or map map object string number something like that or you can declare the variable as in like this empty data okay so when you declare a variable like this it means it is a variable and it is the type is not defined it means any type will be accepted on this particular variable whether you pass integer object or any other type basically one more thing on the variable is you can define a variables with a type without type or you can also say a default value for example let's say i have a variable called location and i would like to say the default value of the location is east u.s which is one of the reason of azure data center i can obviously provide the description location of where your resource needs to provision energy and then type is string obviously but these are the these are the thing which are basically optional now this is what how you should define the variable the terraform tfware file is a reserve file which you set the very value of those variables which you have defined in any of your terraform files so it's not a unnecessary that i should create all of this file and this is just the different differentiating factor i have defined here so that you understand better that this is what the different things you need to know while writing a terraform code but i can still declare a variable in any files like i can declare in output file i can declare a verified file as well but this is what a level of segregation i would like to prefer while writing the terraform code so now let's say we have declared a location full value the default value we have given for me and also the prefix we have declared so what you can do is prefix we can prefix equals to let's say dev or prefix you can say learning and location you can say east us though we have declared the location values like that okay next you have got the so we have discussed variable file where you declare a variable tf terraform dot tf where file where you provide the value of those variables right provider is the provide provider file is is again optional file it's not mandatory to create a provider file you can put a provider in main.tf as well so many example on terraform website you will find the drawer provider they have added in the main.tf file itself but again i prefer to put my provider file in a separate file so that i know that this is where my all my providers are declared it's just totally optional so provider let's understand the provider now so if i go to the terraform registry or terraform website and search for provider here you will see all the listed cloud providers which are available and supported by cloud is available here and list is quite a vast here now as the as the scope of this particular tutorial is azure so i'll go to the azure and you'll see the information about the and downloads and when last time this was updated and things like that now if i go to the documentation here you'll see how do you configure a provider you'll see how do you you know connect to your azure using the provider with the different credential mechanism i've already created a video about this but again we are going to configure the same thing here in this section as well if you prefer a detailed video i'll share the link in the description this this is how you define your providers so if i go to the azure rm provider you can copy that and then copy it here you can see that as i mentioned you can declare a provider even in your main file as well so this is my content of my main file for example let's say i would like to create a resource group then this is how i can create a resource group i'll come back to this point when we'll discuss that but this is how you discuss different uh define your provider files wherein we are saying that my terraform code should connect to azure rm provider on my azure subscription so here we have not mentioned that which subscription it should connect to so you might be thinking about how do i know how my terraform will know that i need to connect to my project as your subscription or my personal subscription or things like that right so how it will know so here you provide the properties of your azure rm provider that this is my subscription id for example let's say subscription id and then you brought the client id and then secret client secret and then tenant id so these are the property you can provide obviously these you can you obviously are going to declare a variable of these properties and here things like that so let's say variable client id and other slides so you could get it right so this is how line secret and then obviously i'll provide the description of those variables but it's optional so i'm just ignoring it just to save the time now just to use these variables how do i use it i use it like where dot subscription id which is the name of my variable and then things like where dot so i'll copy the rest of the things so that i don't need to hardcode these values okay so this is how you you are defining that my terraform should connect to azure subscription using this service credential or service principle so this is nothing just a solution school which you create in your as your subscription right so we have discussed about the provider as well and as i said you can anytime disk use it here as well but you can't have the duplicate provider file so you need to have at least one and only one of the unique provider type you can have one provider for block for azure one for google cloud one for any other cloud provider like cloudflare or any any other but you can't have a two provider block for for the same provider like azure you will get an error so you need to remember that so you can declare it like this as well okay i hope this is clear now now the next thing is about our main.tf file where we are going to write our main logic to provision the resource now to provision a resource in terraform you have two things uh one is the resource block and one is the data source and the second one is the data source block so the resource block basically helps you to define which resource you want to poison in your cloud okay for example i would like to create a resource group so the pattern for defining a resource block is resource which is a keyboard you can see i'm getting the intelligence so as soon as i press tab i get the which type of resource i would like to create you can see that i'm getting a help here which type of resource i would like to create so i'll say azure rm which is the type of provider which i want to use to poison this off then i'll press underscore and then i can scroll it down for example let's say i would like to call it as a virtual networks so you can say virtual network or virtual machine or whatever it is so you can this is how and then basically this is your defining object of a class if you understand a dot net technology or any of the object oriented language then you can correlate with this with object type and then instance of the object which obviously you are going to give a name any name is fine either you call it test example name xyz whatever it is i would like to call given a logical name to my instances for example in this case i'll say virtual network or i can say vnet pubvnet example okay or simply v net so that i i know that this is what the resource type i'm looking for similarly here i can say rg rg for resource group and then once you define a resource type you need to have define some of the mandatory properties so every resource has certain property which is required to fill in the mandatory property and some of them requires to be set as an optional so if you do not fill in so whatever is the default value terraform will fill in those default value on your behalf okay you don't need to worry about that for example to create a resource group i need to have a name defined for the resource group and i can define the location of the resource group i need to define that is without these property i can't create in to create a resource group similarly on a virtual network i can't create the resource without the virtual network property will discuss what how do you know what properties i need to know to create a specific resource i'll tell you if when we go into the detail for now i'll just comment it out so this is one of the type of file where which shows how do you declare a variable i hope this was helpful and you understand that next you define the output file so you can define the output file like output and then all five here so double tap output with the name of output let's say rg location i would like to get the location of my resource group so in that what you will see is the resource output will always accept the output as in value form so it could be object type string type or numerical value or any type which you want to return right so you can return it like which resource type value you want to return for example i can i would like to return this value so your class name your instance name your resource type basically your type of instance type and then the property for example i would like to return the location property then i'll say azure rm resource group and then rg and then location so this this will give me the location of my resource group so these this is how you can return any available property for that specific resource from the terraform so this will print the output of that particular resource which you can use it on the subsequent resource which you are provisioning it right now for so this is how you uh define your project structure in terraform so it's not necessary to create all this file when you define a structure you can create everything in a single file every uh like for example you can define your outcome output variables dfr everything you can declare in a single file that's no there is no rule of thumb here the only reserve file we have here is tfware file see that is something which you need to give the name as is it is a case sensitive so you need to remember that you are giving the name as a tfr file right if you want to change the value as a t values okay so this is how you declare the value of your teraform project structure now how do i connect how do i run the terraform code so to run the diaphragm code if you are using visual studio code you can right click on a folder or a file and open new terminal this is option number one you can go to the terminal create new terminal or if you are using windows then press ctrl at sign and if you're using mac then use control and then tilt button then you get the terminal open so i'll open with this okay now if i go to the the very first command i need to run is log into my azure subscription because i've declared all these variables but i haven't provided a variable values so this option number one you can do that but i don't want to you know share a while while having in call or checking in this code i don't want to check in these service principal values as of now in my code and i don't want to share with you guys for example so what i can do here is i would like to not to choose this option for now we'll discuss it when what is the best case you are going to use that so if i for for now i have the azure rm empty block i'll go to the command i'll run teraform oh before i do the terraform in it first thing you need to do is you need to log into your azure credential using azure cli so make sure you have the azure cli installed on your system for example in my case i have installed the azure cli on my macbook and on my windows as well so you can test the azure cli with help and you should get the information about the desert login i'll use the command as it login so once i connect to azure login it will ask me i'm connected to my azure subscription and the subscription detail has been given now even though i have not i'm not providing any responsible details to my terraform code as in this session why when i'm running the terraform command i'm logged into azure terminal using my azure cli command so i should be able to run the terraform and terraform should be able to connect to azure subscription by grabbing the information of my azure credential using the as a as your cli okay so now if you run the terraform in it so this is the very first command you you need to run so what terraform init means and what it will do basically init will find and download the providers which you have requested to use in your terraform code so in this case we have requested to be requested terraform to use the azure rm provider so in this case when we run the terraform init command basically it went and download the azure rm pro provider so if you see you have got now a additional folder on your which is starting with dot terraform this is a folder basically which you have started here okay which is downloaded when you run the terraform init command and that is the exe and this folder contains a exe for your terraform provider so when now you are going to run the further commands telephone commands related to this provider this exe will help you to run those commands for example now next command what next command we are going to run terraform plan basically so in the plan it will uh evaluate the plan basically it will validate my code and check what are the different input and how input parameters what is the code look like and what all input parameters are expected to have values so for example in my case i have a variable called tip which i do not i did not specified any values for example if you look at the our variable section in the variable section we have a prefix block we have a temp block and we have a location block prefix has got the diff the prefix has got the type but doesn't got any value them got nothing and location i got the value now it is asking for temp so i can provide any value so let's say let's call any value and then it's not asking me for prefix value for prefix the reason for that is because the value for fix and location is defined in that trigger from where so this plan has already known that okay some of the values you of your variables you have already declared in the terraform variable and some of them which you are not declared you need to specify while running the terraform platform so now as you can see my plan is run successfully and once the plan is run successfully you can see that this is what i am getting it so the plus signs means that this is the resource which it is going to create in my azure subscription okay and this is what the changes looks like and this is one of the output will look like so output location which is the west europe strictly right you might be wondering why we 0 because in the parameters i have declared acos whereas and the resource group is created with west europe and that is also a zero the reason for that is even though we have declared this parameter we have set the value of the parameter but we have not used the those parameters here in the resources so i can use the parameters like this so let's say call it location and now if i'm going to run the plan let's comment out the temp variables we are not using it this particular variable so now if i'm going to run the plan then it should tell me that the resource group will be created in east europe in east us instead of west europe as you can see that it's great trying to create a resource in east us and the resource location will be returned as an output after creation of resource so this is how you can run the plan right now to create a resource it has also told you that you can create a resource like this terraform apply so once i'm going to run the terraform apply there will be a last file which comes into a picture terraform state file so at the moment you can see that there is no straight file which we have created so we haven't created any state file tfs trade file now as soon as i'm going to run the terraform file apply command so it will basically create a resource and it will create a straight file as well so now you can see that as soon as i hit the apply button it has created a straight file straight file is a file a json file basically which will have the state of your resources which your provision now it is asking for my approval so the next time when you run the terraform command we'll see if we can run the terraform command with the auto approval so that it doesn't require any user intervention while running the apply command so now you can see that the resource group is created and we have the resource group available so if i open the terraform state file it says that this is what your state file look like this is these are your resources so i just got one resource which is of type resource group and the name of instance name of that resource group which i have specified in my code block which is rg and this is the detail of your resource group like your location your id of your resource group and things like that i don't have any tags so it's it said the tag value as null because as i said that was optional so it keep the value as an option so now if i go to the portal.zero.com and verify that particular resource let's see if we can so we should have a resource group called example resources so this is what the resource group we have just now created and this is how we have successfully created a very first resource in azure now you might be wondering that okay what if i would like to modify the results so let's say we have got the another variable i'll modify the variable as in and then i'll call the default value as a map so map is just a key value pair which i like you in dotnet you have a dictionary so i'll call it the environment equals to dev and then cost center equal to some time zero something like that i can go ahead and i don't need to declare it here in the tf wire because i've already declared it over there so i can use the tags tags and then as soon as i specified the tags i can use text like this now if i run the terraform plan command again this time your terraform command will okay in just a moment here i miss the yes sir then this should valid compare now not what terraform will do is telephone plan will compare my current state file with the changes which i am trying to deploy and then it will tell me okay as for the current state file this is what the update it should perform and this is you can see that with the help of tilde operator it means that this is the resource type and within this resource this is what the resource property which is going to be modified in the name next subsequent run and you can see that i do not have any tag on my resource group so now if i'm going to run the form apply this time i'll use the autorun command so you can run the atom run command using auto and then approve something like that so now this time it won't ask me for my permission to run the and provision the resource this is how you can run the terraform apply command with user interaction and command has run successfully i've got the resource program successfully let's see the we have received the update here this might take a slight bit of time you can see that we've got the the tags to our resource group so this concludes our very first demo when we have created a project terraform project we understood the different component of terraform files like variables dfr providers output and main file and we have successfully created a smallest possible resource in azure which is a resource group i hope this was helpful in the next session we are going to discuss the next topic which is there in the slide
Info
Channel: Bee-a-Learner
Views: 8,994
Rating: undefined out of 5
Keywords: Azure DevOps, Terraform, Learn terraform with azure, Azure Infrastructure as code with terraform, Azure Infrastructure as code, terraform from start, start learning terraform with azure, step by step terraform guide, terraform with azure, how to start terraform in azure, terraforming in azure, learn terraforming in azure, azure infrastructure with terraform
Id: tTR8KdcWBlw
Channel Id: undefined
Length: 45min 23sec (2723 seconds)
Published: Thu Feb 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.