Using Terraform in Azure DevOps Pipelines PART 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Seems relevent, we found this which was nice : https://www.runatlantis.io/

👍︎︎ 3 👤︎︎ u/gcoeverything 📅︎︎ Jun 11 2020 🗫︎ replies

After doing the turorial, i doubt you're using the service principal you created with the az ad sp.

When creating a service connection it automatically creates a service principal and grants it the contributor role on the RG. The pipeline works with this SP.

That's why the SP didn't have permissions to create new RGs. I modified the TF code a little to source the RG ID that already exists (terraform-rg)

👍︎︎ 3 👤︎︎ u/TelefonTelAviv 📅︎︎ Jun 11 2020 🗫︎ replies

Been hearing so much about terraform lately. Time to check it out.

👍︎︎ 3 👤︎︎ u/xinhuj 📅︎︎ Jun 11 2020 🗫︎ replies

Great video! You should make ASMR videos about cloud stuff

👍︎︎ 2 👤︎︎ u/cenariusofficial 📅︎︎ Jun 11 2020 🗫︎ replies

Good Stuff!!

👍︎︎ 2 👤︎︎ u/TelefonTelAviv 📅︎︎ Jun 11 2020 🗫︎ replies
Captions
hey what's up everyone in this video we're gonna go over how to create an azure DevOps pipeline that leverages terraform to bring up resources in Azure cloud alright let's go ahead and jump on this right here before we get started we need to create a resource group okay I'm gonna go ahead and copy this here this ID and I'm gonna use it on the next step now we're gonna create a service principle and we're gonna use the service principle to connect Azure DevOps to our Asha cloud account and to this specific service principle that we're creating right now all right so that ID that we got from the when we created the research group we're gonna use it here so we're just gonna paste it and that should be good all right now that that's done let's go back to our Azure DevOps and now we're gonna create a service connection so we go to project settings service connections new service connection as your resource manager service principle give it a second here select your service principle and then now click on the resource group drop-down menu and you're gonna have to authenticate all right once the authenticate you click on the resource group drop-down menu again and now click on the resource group you created give your service connection a name it's safe alright so the next thing we need to do is we need to create a blob storage so that we can store their terraform States file and we'll start off by creating a storage account you have to use the resource group we created in the previous step and give it a name alright next give it the SKU and encryption services this blob ok looks like that's done alright so the next thing we need to do is grab our key okay so you're gonna grab the value for that first key and now we're gonna use that to create our container within the blob all right so now we create our container and I'll just paste that key that I got earlier okay looks like our blob storage containers has been created all right the next thing we need to do is create a repo so go to repos click on the repo drop-down menu click on new repository initialize and in here we're gonna create a simple terraform file because we're gonna need that in order to create the pipeline so let's go ahead and click new file let me call this terraform TF you can call it whatever you want but I'm just calling it that ok we'll start this off by giving it the provider and that would be Azure RM the version I'm gonna use the latest as of this video and features I'll leave that empty and in order for you to be able to store your your terraform state file you need to create a back-end so we'll do that by doing typing this here and now we give it the backend the resource group name TAF state t-state the storage account name and that's the account we created earlier and then the container name that's the container that's inside your blob storage and here's where you get to specify the terraform state file as I called my entire form it's gonna be called terraform here TF state now I have to give it a data resource and for this terraform I'm just gonna create a simple app service and that of course starts with creating a resource group so now we're gonna give it a resource and I'm gonna give it just the simple name of a resource group here and I'm gonna give it the name that it's going to be used and then the location the next thing we need to do is create a service plan give it a name and then of course you're gonna reference the location to your research group next you can give it the resource group name and now we got to specify the SKU tier in size and lastly we to create the actual app service you got to give it a name the location now I'm just gonna copy these over here and what these are is again there are references to this resource group that way you don't have to keep typing the resource group name and the location and lastly we need to give it the app service plan ID and this just refers back to the service plan above okay so that should do it let's click commit all right so now we have our terraform file all right before we get started with our pipeline we need to install the terraform extension to do that go to the marketplace browse marketplace search for terraform and it's right here Microsoft dev labs click on that click on get it free and then make sure your organization is selected click install proceed go back to your repo and now let's set up a build create a starter pipeline erase this and for this pipeline we're gonna use stages we're gonna have a state for to validate the terraform and then we'll have a stage to deploy it okay now we're gonna add the jobs it's only one but you have to add it anyway and then job oops I'm also gonna call it validate and I'm gonna set continued error false okay now we defined a step section and we can use the assistant for this type in terraform and the first thing we're going to do is tell it to install the terraform tool and we want version 0.12.10 TV station click on add we're gonna add another task and that's the one that just says terraform we're gonna say as your RM in it the working directory we'll leave it we'll leave it at default subscription you create we created earlier so as your SPN is the one I created the resource group the storage account the container we create it and then for key we're gonna type in terraform dot TF state all right let's click on add task alright that looks good to me let me give it the display name because otherwise it's just gonna give it the name terraform task ok we're gonna add another task this time we're gonna use the validate we'll leave everything else default click on add where you give this a display name as well and that should take care of the validate section okay now let's move on to the deploy section this also requires a jobs this one is different this was going to be deployment and we'll call it deploy terraform continue on arrow oops teen you on error false that way errors out environment will call this dev and now the strategy run once deploy steps and we have to tell it to check out this repo otherwise they won't see the terraform file and then here we go back to our tasks over here click on the terraform tool Installer we again 1 version 2 6 so 0 about 12.2 6 click on add I'll give this a display name I don't think I gave the other one to display date so let me go back up here okay we're gonna go back over here to our tasks search we're terraform we're also doing it in it here select your service principal your resource group storage account storage blob container and then the key which again is terraformed dot TF state you're gonna add that let's go back to tasks add another one this is gonna be terraformed plan and then you're gonna specify your service connection we're gonna add that then go back over here and give this a display name and we're gonna add another terraform and this was the apply so validate and apply specify your subscription add that give this a display name quick check on my work here everything seems to validate as valid uh yeah mo I think we should be good let's try it out save and run and you can leave the default commit message yes I'm committing directly to master I'm the only one working on this so ya don't care okay so now when you run the pipeline you'll see that there's the two stages validate and deploy but I want to see it running so I'm gonna click on it I clicked on the validate stage that's validation let's look at that install the tool you did a terraforming it ran validation it says it looks good and let's see what happens here oh okay this is a resource group okay and it's the scope is invalid that's kind of weird alright guys so check it out I mean a mistake it looks like I had to go to my subscription and give the contributor road to my service principal once I did that I reran this pipeline and I'm no longer seeing the error so it looks like we're good okay so now we can go to Azure cloud and we can go to the resource group we created SK terraform and yes I renamed it from what I created in the terraform I thought that was a naming problem but it wasn't so what I typed in earlier is it's a different name now but either way you can give it whatever name you want but now if we go to the resource group we can see that we have an app service and we have an app service plan using an azure devops pipeline that runs terraform so some of you might be asking yourself why are you using terraform to create things in Azure cloud if it's meant more for bringing up infrastructure well my answer to that is you can use terraform for many things it just depends on what your needs are and actually now that I have this pipeline I can copy that pipeline or I can convert it into a template and I can use it for any of my terraform needs to bring up resources in Azure so I can use the same generic pipeline to do that if I create a template obviously the terraform state the blob storage container would be different I can see how somebody can take this and then build upon it and make something that's more complex and you can do much more with it anyway so I'm just I just wanted to show you how I'm using terraform azure devops pipelines to bring up resources in Azure cloud I hope you learned something please make sure to LIKE and subscribe thank you [Music]
Info
Channel: skunklabz
Views: 15,269
Rating: 4.9113922 out of 5
Keywords: SkunkLabz, Episode 12, azure, azure devops, terraform, state file, pipelines, devops, yaml, azure blob storage, azure app service, azure app service plan
Id: kqwzqWcSCYM
Channel Id: undefined
Length: 18min 29sec (1109 seconds)
Published: Wed Jun 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.