How to Automate Infra setup in Azure Cloud using Terraform & Azure DevOps Pipeline | Terraform IAC

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back in this video we are going to learn how to automate infrastructure setup in Azure Cloud using terraform and Azure devops pipeline let's say that you are working in a team you are in the process of migrating from on-premises into Cloud especially Azure Cloud now you are in the process of setting up the entire infrastructure in Azure Cloud so what are the best ways in order to set up infrastructure in Azure cloud of course you can use terraform so terraform is one of the popular infrastructure automation tools using terraform you can pretty much set up infrastructure on any Cloud platform okay but let's say you are using terraform and you're creating terraform files for setting up infrastructure in Azure Cloud how are you going to automate that okay so for example let's say that you have committed terraform files and um you know your your reports your SCM tool how are you going to automate that infrastructure setup you know instead of uh executing terraform commands through manually right so that's exactly what we are going to talk about in this video so if you look at this diagram so let's say this is you you are committing your terraform files uh in your SCM tool so I have picked up Azure reports as a CM tool but let's say you're using GitHub or bitbacker yes you can also use those tools as well and then we are going to create Azure devops pipeline okay so I have already created uh you know this is my Azure devops organization and then again right so I'm going to create a pipeline over here okay and then um whenever we are going to create a pipeline so the pipeline what we are going to create the pipeline what we are going to create we are going to install a terraform on that build agent so we will talk about that in a minute how we are going to achieve that and then if we actually see here since we are writing uh you know terraform since we are using terraform we will have to use a remote State information right meaning we have to we must store State information remotely so for that we are going to use Azure blob storage okay so we are going to create Azure storage account and then we are going to create a container that is where we are going to store our terraform State information okay and then what are the resources we are going to create so we are going to create a web app okay so in order to create a web app you need to have a service plan and then of course any resources you are creating you need to uh you know have a resource Group so basically we will create a resource Group and then we will create tab service plan and then we will create web app okay so yeah so basically it's a very simple uh use case right so we are going to create uh Azure devops pipeline that is going to automate infrastructure setup in Azure Cloud so that's exactly what we are going to do awesome so let's go back to go over here let's just talk about prerequisite and then the implementation steps so yes we need to have Azure subscription okay so this is my Azure subscription okay so I do have pay as you go I do have pairs ago but again if you are having let's say you're having you know a free trial of course you can also use that as well not a problem and then uh Azure devops organization and then project so this is my Azure devops RNA session I also have a created a project dashboard here okay so this is where we are going to you know manage our source code which are nothing but terraform files and then this is where we are going to create a cacd pipeline okay um and then we need to have a terraform build and release task extension in azure devops so what do I mean by that is so you click on this particular link here since Azure devops pipeline is going to use terraform right so what you can do is you know you can install this particular add-ons I have already installed this add-on in my Azure devops organization so you know it is very good if you have this plugin installed so that you can easily add terraform task you know in your pipeline okay so that is something we need to have and then terraform files already created in SCM so you know our use cases we are going to create a web app okay we are going to create a web app so you can see the code here right so I have written terraform files already so all this is going to do is this is actually going to create a resource Group and then it's going to create app service plan and then we are going to create a web app right so we are going to create Java based web app okay so that's all we are going to create okay and then of course if you look at the terraform files I also have a variables dot TF file where you know the resource Group name and then location is Define and then this is my terraform.tf War file where you know I'm kind of declaring those variables and then I'm going to use uh we are going to use remote storage right so it is not recommended to use local state information right so you should not use local state you have to go for something called remote state so we are going to use a backend as Azure RM and then like I said we are going to use Azure storage account and then we are going to use container so that is where we are going to store our terraform state that is where we are going to use terraform State information okay and then this is my Azure provider okay so you can see here if I take you to terraform website go to browse provider if you click on Azure so if you see here the latest providers you know 3.72.0 but I just use like you know a little bit older version kind of a stable uh provided version okay 3.70.0 all right so yeah so that's about terraform files very simple use case uh we are going to create a web app and then in order to create the web app we need to have app service plan and then of course we need to have Resource Group as well so that is where all the resources will be created okay perfect and then how are we going to achieve this right so yes let's create a resource Group because we need to have a storage account and then container in order to store a remote State information so I'm going to create a resource Group and then we are going to store all those information over here and then we need to create a pipeline okay so we need to create a pipeline in Azure devops and then we have to add terraform tasks okay and then we are going to go ahead and then execute our pipeline to create resources like I said we are going to create app service plan and then web app and then we will verify whether the resources got created in Azure cloud or not and then most importantly once the resources are created in Azure Cloud we will verify you know whether our terraform.tf state file got updated uh in the Azure storage account and then we will perform some cleanup activity okay in order to clean up all the resources whatever we have created okay all right guys so let's get started now so guys you know this is my website coach devops.com I will be posting the link right below the video as well so let's look at the first step so we need to create a resource Group okay uh but what I have done already I have already created a resource Group here okay but again if you want to create a new Resource Group it's very straightforward you can click on uh Universal group and then you can create a new Resource Group but I have already created that so I'm going to use this particular Resource Group okay so very good and then we need to go ahead and then create a storage account okay so like I said this is where we are going to store our terraform State information okay so how do you create that let me go to show let me go to Resource Group and click on create and then I'm going to search for storage account there we go and click on storage account so we are going to create the storage account okay so let me give some name so what I wanted to do if you actually look at the backend.tf file so this this file already has all the information right like you know what is the storage account name and then what is a container name so let's be consistent so let me copy the storage account name from here so make sure that you know you have unique storage account name um otherwise that's not going to work so let me make sure this is unique for example if you just give my TF storage I don't think yeah see here this is not available okay all right so let me use this one and then let me check the location so yeah let's go ahead with Southcenter I think this is different okay now I think this is different this is like what location we are going to use but again not a problem so let's go with this location okay very good all right so let me go ahead and then create the storage account uh and yeah so this is fine I don't need to have a premium one okay this is just for general purposes right so this is fine and then read it and see I think this is totally fine locally written sorry that's totally fine all right awesome so let's go ahead and then create the storage account awesome so the storage account is being created right now let me go back to my instructions and then we need to go ahead and create a container inside the storage account because this is where we are going to store terraform State information so let me go back to my portal here it is still creating awesome so click on go to resource so go to containers so let's start creating a new container that's a container name perfect so that's the container name and click on create very good and then you know uh whenever terraform is going to update the state information and then this is going to use this particular file so this is the file where you know the state information will be stored perfect I think we are good there and then let me move forward now I think we can go ahead and then get started on creating a pipeline so so basically again right let me go back to this diagram we have already created terraform files in Azure approach okay and then we also created Azure storage account and then container so that is also there all we have to do is create a Azure devops pipeline that is going to check out from here and then it's going to start executing a terraform commands in order to create those resources in Azure Cloud okay great so let's start creating the pipeline all right so how do you do that go to pipeline section so basically what I'm going to do is I'm going to go with the classic pipeline for now okay I know this may not be a recommended solution uh usually at work but again right so this is just for demo purpose um if you are new to Azure devops or if you're new to a terraform so this is a great opportunity for you to learn how to automate infrastructure setup using classic pipeline maybe in upcoming videos uh we will talk about you know how we can create amml pipeline in order to you know automate that all right so let me click on new pipeline okay let's click on classic editor okay and of course we have our report so that is our Repository name click on continue okay so let's start with the empty job okay so that's fine I don't think there are any terraform templates available in Azure devops so let's go ahead and then create empty job so that's fine okay and then let me see here let's start using how's your pipeline here and let's use Ubuntu latest as a build agent I don't want to use Windows agent because that may slow down the performance so let's go with the Ubuntu that's totally fine now all we have to do is you know start adding tasks so one of the prerequisite is you know we need to add a terraform task so I have already added that see here I have already added the task okay so I don't have to add so let's start adding the task right so click on this one so look for terraform so what we have to do is we need to First install terraform on the build agent okay because the build is going to run on agent right so we have to install terraform so click on ADD so this is fine so this will take care of installing terraform on the build agent let me also go ahead and then name my pipeline so I'm going to say my awesome uh infra pipeline job okay I'm just going to save it I'm not going to cue it very good so let's go ahead and then add another task as well let me also again search for terraform and what we have to do is uh we have to initialize terraform so let me let me add this particular task exigo terraform commands to manage your resources on Azure RM or AWS or Google Cloud so let me add that one there you go okay and then this is our provider right because we are going to create in Azure resources in Azure Cloud so we have to select that one so we are going to start with the initialize okay let's go with that and then we have to select our Azure subscription so let me select that one okay and then the resource Group okay so if you guys remember I have already created a resource Group my RG and then we have already created our storage account so if you are wondering so this is that one right this is a storage account okay that is actually part of my RG and then we also created our container as well so let's select our container and then of course our key that is actually nothing but terraform Dot TF state perfect it's a very good right I think this is good for now okay so why don't we do one thing before we go too far uh why don't we start executing this right so but before the let me just change this to init okay so why don't we save and queue and again you can select Ubuntu latest click on save and run so there you go so it is in the process of executing the pipeline task so let's see the build output here so terraform has been successfully initialized and then before that terraform also got installed on the build agent as well wonderful okay so so far so good let me edit my classic pipeline so now we have to add few more tasks okay so obviously right if you're familiar with terraform uh you have to initialize first and then if you want to validate you can also validate that as well and then we have to exigo terraform plan and then terraform apply okay so click on the plus so let me look for terraform and then let's start adding a task so let me use terraform C like command okay and for validation this is actually good enough terraform validate okay I think that should take care of it and then let me also go ahead and then add terraform plan so let me copy this and then I'm going to say plan okay so we just need to select our Azure subscription there you go I think this is the correct one wonderful okay so before we go too far let me click on Save and Q wow see here guys so this is telling us that this is going to create three resources so one of them is obviously Resource Group and then app service plan and then this will also go ahead and then create web app perfect right so so far so good everything is working fine all we have to do is go ahead and then add a task for terraform apply so let me again look for terraform I and select this particular task and then go to the drop down and then make changes here apply and then I'm going to select apply from the drop down wonderful and then select the Azure subscription so there you go right so this should take care of applying uh so one more thing right so let's say you're performing terraform plan if you want to write the output to a plan file you can also do that over here as well but I'm not doing it but this is just a basic demo so let me click on Save and queue click on save and run okay so plan has been already executed so right now in the process of you know performing apply there you go so it is in the process of creating a resource Group so it is going to create Resource Group with this name and you can see that over here right if I take you guys to my repo so you can see there over here right so it's going to create this Resource Group name and then of course going to create these three resources all right so you can see that over here it was able to create three resources successfully okay of course there are some warnings and all but I think don't worry about it you can you can take a look at the code and then you can fix it or you can perform Google search if you don't know the fix awesome so let's verify that right let's go to Azure portal let me click on refresh okay so go to Resource groups there you go guys so this was the resource Group which got created using terraform and then there you go so we were able to create a service plan and then app service so click on the app service so if you want to make sure that you know this app service is working fine um of course this is Java base so you can click on the URL wow there you go guys you see it so this confirms that our app service is working fine why don't we quickly verify our terraform State information as well so we have already verified that all the resources got created but why don't we verify uh whether our terraform State information got updated or not so let me click on the storage account and then click on containers and then this is the container name click on it there you go so this is the terraform.tf state file and then you can see here that the file got modified and and you can see that 439 like couple of minutes ago so click on the terraform State file there you go and then of course you can click on edit just don't try to mess up the terraform State file the last thing you want to do is you know modify this particular file okay so you should not modify but you can see right now the terraform.tf statefill got updated and you can see here under resources section you can see app service you can also see let's see right you would be seeing app service plan there you go upstairs plan right and then of course the resource Group right so yeah so everything is there okay so let's do one thing right let's go back to our pipeline why don't we try to destroy okay so so you have two options right you can modify the current task or you can add a new task if you wanted to right so let me disable this one for now uh let me add a new task let me look for terraform and add this one okay and then I'm gonna say terraform destroy and then in the drop down I'm going to go ahead and select destroy okay and then select the subscription so that's it right that's it so apply got to disable so now if I go ahead and then click on Save and Q so right now uh the pipeline should be able to uh you know you can go terraform task and then we should be able to destroy all the three resources what we have created out of terraform okay so as you can see here it is in the process of performing destroy there you go guys see here currently it is in the process of destroying app service and then finally of course the resource Group wow so this has completed so let's verify that in Azure portal uh of course before we come back here right let's just make sure that all the resources got deleted so there you go I don't see that Resource Group out here right everything got deleted and then of course if you wanted to see the storage container and then click on container if you want to just check the terraform.tf state file there you go click on edit wow see you guys so you don't see much in the terraform.tfstate file all right so yeah so that's it guys you know that stuff you can automate uh infrastructure setup in Azure Cloud using terraform and Azure devops pipeline thank you for watching this video
Info
Channel: DevOps Coach
Views: 2,443
Rating: undefined out of 5
Keywords: Automate infrastructure setup using Terraform and Azure DevOps, IAC using Terraform and Azure DevOps
Id: kP9UV4vJGro
Channel Id: undefined
Length: 24min 7sec (1447 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.