Beginners Tutorial to Terraform with Azure

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey folks it's chris wahl today i'm going to show you how to get started using terraform with azure the idea is if you're a beginner and you just want to get up and going using azure and terraform this video is for you and with that let's get coding we're going to need a couple things to get started first we need to have terraform installed so i'm just going to do terraform version and we can see i'm running version.13.2 as a little helpful reminder it's letting me know that i need to upgrade okay i'll do that in the future but as long as we're on version.13. anything we should be fine so terraform is handled the other thing we're going to need is the azure cli and that's just a z and i can do a version there as well you can see we're running version 2.12 the point here is just making sure you're running at least version 2 and i'm running 2.12 so i should be fine as a little bonus tip if you need to upgrade your version you can do an az upgrade and it'll go ahead and pull in whatever is new but 2.12.1 is the latest at this time this is a good time to remind you that if you like this video leave it a like it really helps me out with the algorithm and if you want to catch me live i stream on twitch right here twitch.tv slash wall network twice a week happy to answer questions and just hang out let's talk tech the next thing we need to do is log into azure so that the tool has all the information it needs so we'll do a z login and we're provided a login prompt once i've signed in and provide my credentials i'm returned back to the console and we can see a bunch of metadata about my account and my subscriptions in this json payload as long as you see you have logged in now let's find all the subscriptions blah blah that means it's successful and we can actually check that you can find all of the configuration files under your user account slash dot azure importantly you'll see the azureprofile.json and the accesstokens.json these are filled with all the credentials and important bits around your subscription and your user information and your tenant id that are necessary for the az command or azcli to interact with azure without having to type in more credentials it's also used by terraform so that it can interact with the azure cloud when you build out a configuration i've gone ahead and cleared the screen and let's start writing some terraform code and begin i'm just going to make a new file called main.tf directly in the root of this directory and we'll navigate to it as well i'm going to go to wall network if you have examples or azure there we go and we can see that i've already made a new branch called like subscribe that we'll be using to develop this code we begin by telling terraform what provider to use and i've already got a block pre-written that we can go through it's fairly simple we're just saying the required providers are azure rm or the azure resource manager and it can be found at hashicorp azure rm and i want to use at least version 2.31.1 but not version 3 dot anything else that's what the tilde and the greater than sign equals that's a pessimistic operator that ensures that we're not going to change major versions pessimistic operators ensure that we're not going to use a different major version but as long as we're at or exceeding the version that we described within that major version we're okay i'll leave a link to a blog post right here that i definitely recommend you check out this is where azure gets a little bit unique so we've already told terraform what provider to use and now we need to also provide some information about the provider and you'll see we have provider azure rm and then an empty block called features this is a little bit specific to the azure provider you have to leave at least a blank features block or actually put some features into it and those are feature flags we don't need any features but if you don't put the block in there you get an error the last thing we'll need is some sort of resource to build and normally i build a budget but there is no command to build a budget sadly enough so we'll build a resource group instead and i've gone ahead and pre-populated one here this just says build me a resource that resource type is azure rm resource group so we're building a resource group and i've contextually called it rg to mean resource group the rest of the data defines the name the location which is the region and any tags that i want to apply to the resource and in this case it's a development resource group made by terraform now that the code's in place i've gone ahead and put it out of the way for a moment because we're going to do all the terraform steps now the first step is to initialize so i'll do a tf init and then i'll go ahead and make sure we have the local back end defined grab any missing provider plugins and just be ready to run this terraform config it's kind of nice how it's green and happy and it says you may now begin working with terraform because you can so let's do that next let's do a terraform format and that cleans up the code and you can see it returned to main.tf meaning it cleaned up the main.tf and made everything spaced properly essentially we'll enter the file finally we're going to do a terraform validate to make sure we have good code i mentioned earlier the features block even though it's blank is still required and we can see that if i go ahead and delete the features block and just leave it empty we'll re-run the validate and it'll come back kind of angry like hey i need a features field i don't have any value there even though nothing's coming back let's go ahead and put that back and rerun the validate which should come back just fine and then we can proceed ahead now that the config is initialized we have formatted it we validate it it's time to plan and apply we'll start with the plan and this is going to look at the current state of things which doesn't exist so that's easy and compare them to the desired state which is just building a resource group so it should come back pretty simply here we can see the execution plan has said that it's going to create something which makes sense and that thing it's going to create is a resource group named like subscribe with two tags an environment tag called dev and a source tag called terraform not too hard right we're going to go ahead and apply these changes and then make some tweaks to it but hopefully you can see now it doesn't take that much effort to get started with terraform and azure i'll do a tf apply to go ahead and apply these changes and we'll see that basically the same report comes back all the execution plan results that we saw in the plan phase is echoed with the apply phase they're kind of the same thing except now we have the ability to execute it in reality so we can say yes i want these changes build me this resource group there we go we have a resource group and you can see kind of how azure treats objects within its cloud everything's in this kind of hierarchical folder structure where you have subscriptions and then resource groups and then things live under that hierarchical path as they're created one piece of advice i'd give you is that the variability between how quickly azure response to api requests is highly dependent on the region you choose and the time that you choose so by default don't just blame your computer your network it may just be the apis are busy or taking a little bit longer to respond than you may be used to if you've worked with amazon or google clouds now as you might expect when we go into the portal and look at the resource group it mirrors the reality that we set forth in terraform there's the name in the top left and then kind of in the middle we have the two tags that were defined the terraform is not just a make it and forget it sort of thing it's expected that you're going to hand over control of the creation maintenance and retirement of this resource wholly within terraform to that end let's change some of the tags in terraform and then reflect that change into azure let's add a new tag i'm going to say that we have one named owner and we'll set that equal to chris just just me we'll do that we'll assume that everybody's first name basis in this cloud now i'm going to run the terraform format one more time just to clean it up you can see the spacing wasn't quite right and that's it we can now do another apply i'm going to make this larger the apply has yielded that there's an update required and this is specifically an update in place there's lots of different ways to change resources it could be an update that requires a destroy first or an update that's done in place where the resource is just modified without actually deleting it making a new one in this case the only thing that's modified is defined with the tilde that's the tags and the modification is that we're going to create a new tag called owner with the value of chris i'm fine with that so we'll say yes and reflect that change into azure so that it meets what we've built out in terraform that change is pretty simple and we can tab over to the azure portal and see that change and there we go i'm now the owner of this resource group based on this particular tag if someone that i'm working with had an issue with it they at least know to hit me up and ask a question now i get it you may be playing with terraform for the first time and you're like i don't want that resource group to persist how do i get rid of it this is where destroy comes in handy i'll start by running a terraform destroy command and then we'll talk about what it's going to do let's look at the destroy plan that was presented here now you'll notice it's still an execution plan it's just instead of creating or updating in place we're going to destroy those resources this one's pretty easy we're only going to destroy that resource group because it's the only resource defined in this terraform configuration terraform makes sure that things are deleted in the right order now this only has one resource in there that resource group so it's pretty easy to delete it you just delete the resource right but if there are dependent objects terraform will delete them in the right order so the dependencies are maintained i can clearly see that it's going to delete the correct resource group i'm okay with that i'm going to type yes and there we go now my cloud is back to its original state and i didn't have to go in there and muck about deleting this resource group so there you go that should be enough to get you started so that you have terraform and azure talking to one another and you can start that journey with building things in the cloud again if you found this helpful a like and a subscription help me out in a super big way it helps other people find my videos and shares it to your network so i'd appreciate that and as always you can catch me live on my stream every tuesday and thursday on twitch tv slash wall network i'd love to hear from you until then happy terraforming you
Info
Channel: Wahl Network
Views: 13,951
Rating: undefined out of 5
Keywords: tutorials, how to, guide, technology, tech
Id: gyZdCzdkSY4
Channel Id: undefined
Length: 9min 41sec (581 seconds)
Published: Tue Oct 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.