Hosting your Terraform module on Github - Getting started with Terraform Modules (part 2)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back uh so in the last episode we took a look at how to create a module in terraform which helped us wrap our infrastructure into a reusable block now that's great except we probably want to share this with someone else um having it on a local computer only and not being able to enable others to use it is kind of pointless so what we're going to do today is take a look at how do we take that module and host it some way now that somewhere for today will be a github public reaper so what i've done is i've gone ahead and created a repo that we will be using in the demo today and the repo is currently empty and i'll show you now when we jump to the code but before we get to the code quick apology for the previous video or two i'd realized a bit late that i was bouncing up and down on the wooden floor so it was a little bit of a bumpy ride literally um and i made some people seasick so apologies for that and hopefully this one will be a bit better cool let's jump into the console okay so now we are in the console and um this is exactly where we left off last time so this is where we had our local module and what i've created so long is a github repository over here and you can see that this is um it's been nine minutes since i cleaned it up and it's brand new and empty and i have cloned that into a directory here for us called tf module example now the first thing we want to do is we'll move the code into that repository so let's go ahead and select our module that we used previously and we drop it inside this module folder so this is great now we can share our code but for others to be able to use our code we probably need to give them some kind of you know hint or instruction what to do so let's go ahead and add a readme file because that's the expectation that people have in terms of where they can find documentation so let's quickly create one and we call it readme.md as you would normally do and we open up the file quickly so let's go ahead and just give some instructions about what this module is so my awesome module sorry terrified module um and then just what does this do this solves all your infrastructure needs and just disclaimer because some people take things literally just kidding this is just an example cool now we've got that right now this tells someone what this module does it doesn't tell them how to actually use it so let's add a little section for usage and to do that we go ahead and say usage like this and we're going to be providing some code examples so let's use the triple tooled to actually create a coding block inside our markdown folder so now how do we go about using it well if i go look at our code over here what we can see is this references the local relative path using this dot slash module so that's the part that we need to change to be able to use a module from github so to do that what you do is you grab the url so let me just go ahead and paste that in and you want it without the prefix so if you just specified in this format which is the normal http type checkout format terraform will actually check it out as is using the normal public https checkout mechanism if you do i ever want to use uh the get um mechanism with a get at syntax or which is over ssh or make use of a private repo um to store your modules you can do so just put it in that format um like you would normally get off the github repo so you can just click over here git oh sorry code and then you can see this is the format in the syntax to use for those private modules but we're just going to use the public one and this is actually on my github repo so feel free to go poke around and have a look so now we've got our module and this is where it's been is going to pull it from but guess what our source is not there yet but before we publish it there we need to do a little bit more cleanup um because we want to give people instructions in terms of how to use it so let's go back to our readme over here and we paste this in and over here someone might not be familiar with what an ec2 instance is or what the instance types are so what we can say over here is let's give them a useful example so for example let's say t3.micro as a usable one the instance name is let's just call it my um instance and close the bracket ah sorry the quotes and number of instances we probably want one of these and in terms of the ami now this is something where you can either try and hard code something or add in a value but there are a couple of different ways where this can break for people so what i prefer to do is to make sure that this will give them an error so let's go ahead and say um your your mid id to use to launch the instance because this when they just you know copy and paste this into the code they'll realize that hey listen we made a mistake and we need to go fix it there'll be a prompt of some kind instead of just launching something that they didn't necessarily intend cool so now documentation here is done so let's go and clean up our variables and outputs a little bit because while this is useful to show what the um i'm entering here to get the module going it doesn't really explain what these variables are so let's add some descriptions to them so all you do over here is you say description like this and you give them a description so this is the aws ec2 instance type size to use and what we do now is we add similar descriptions for the rest [Music] cool now we have got all of our uh descriptions in here um and uh it's actually nicely lined up and formatted but let's say i didn't have a mistake um and this is all not a mistake just a miss format misformatted document because previously i actually came along and ran this command but terraform has got this fmt command that stands for format built in so i can just go so let me just get to that directory uh terraform format and this will actually go ahead and format the code in um the way that they define and think your code should look like terraform is built in go and goes has got an opinionated way to format your code and my recommendation here is just stick to it you don't want to go into formatting wars and fight with people just say this is it and build it into your build system speaking of building it into your build system there will be a video in the next um week or two on how to wrap all of this multi-environment terraform usage inside a make file so stick around for that and hit the subscribe button if you want to know when that video is coming out so now what we have is we have cleaned up our variables now we can do the same for our outputs just let people know what it is so similarly we just say a description like that description and give it a value once again um the aws id of the ec2 sorry instance we created cool that should help once again i'll just quickly run format so i don't have to worry about my code being ugly cool now we've got everything so let's go ahead and say get status to see what we have so we've got our readme we've got our files that we updated so let's go ahead and say git add git commit um new module and we go ahead and say git push this code let me just quickly add my passphrase so now what we can see in the github reaper over here in a second is that we now have got our readme over here i can see how to use the modules let's go ahead and copy this and then i can go back to my code over here which is uh examples over here so let's go ahead and paste this in now i'm expecting a couple of things to go wrong here so let's quickly work through that first one is we want to run that terraform um sorry let's see there's plan we don't want to run terraform plan now for those that are watching there's one mistake already which is um there is well sorry wrong directory um there is something wrong with this ami id and if we look at it oh yeah we forgot to actually add it in here so how we do that is um we can quickly go up here and just go ahead and grab the one that we previously used which is data.awsm because remember this is how you use data sources to actually do lookups for you so let's go ahead and grab that cut it out and then we paste it in here so when i look at my module things should be working now right uh no we're gonna run into another issue so let's go ahead and just run the plan come on again and i'll show you what that is and that issue is that oh it doesn't know about this module how do we go about fixing that well terraform in it so terraform init will go ahead and go pull the modules you could also use terraform get and we are actually going to use terraform get in a second to actually make use of it so now we've got all of this configured over here so let's go ahead and say plan now everything should work right uh not so fast we're gonna have an issue once again and ah it was actually happy ah the reason it's happy is that it's we actually provided it with all the variables now the thing that we see here if we once again look at it closely is that we specify the number of instances and if we go to our actual module code that's not being used here so clearly that's a mistake that we left over from last time so you know what we're good voice cards we are going to go clean up the code and leave it better than we found it so let's do that and to do that we simply need to move that variable so let's go ahead into our module we can see over here a number of instances that's definitely the one we want to remove so save it let's go back to our repo we say get add uh get status just to double check yep it's that one git commit cleaning up removing uh instance count variable right and we push this and we go ahead and pause phrase again and now that's been pushed cool so our module is updated right and if we go look at our actual source code over here in examples now we just remove this and we should be good to go right well not so quickly again let's let's take a look at what's going to happen uh sorry plan when we run plan now it's going to give us an issue which is it wants that um variable now this is kind of weird because then we just fix our module and we did except terraform keeps a cached version of the module locally now to be able to force terraform to grab the latest we need to go terraform get and then update like that and what this will do is it'll tell terraform to go grab the latest copy of all the modules that it knows about so now if i go and say terraform plan guess what everything will work and it'll be able to tell us what infrastructure wants to create like we did last time and now we can go ahead and actually use it now once again this is super useful and this pushed us in the right direction but there's still this issue of what happens when i change my version or change the code inside my module do people get notified about it how do they pick what version do they want from history etc so what i'll do is in the next episode i'm going to take you through how to add versioning to this and also show you how you can actually publish your module on telephone cloud which is a service by hashicorp that allows you to publicly share modules as well as you can see the tab over here i've set that up already and i will be publishing that in the next video so like i said we're going to be showing you in the next video how to use version numbers inside your modules how to pin those versions specifically so you don't accidentally upgrade your infrastructure without checking um what the changes are and speaking of changes i'm also going to show you how to add a change log markdown file to a document exactly what the changes are perversion and i'll be basing this on the documentation that you get when you look at the terraform providers and in general terraform modules they are super useful and very detailed and it's one of the things that really drew me to terraform years ago is the level of detail inside these documents you can scan them and immediately know is this a bug is this a hot some kind of fix or is this a new feature um so we'll be doing that and if you want to get notified about that hit the subscribe button down below and also hit that little bell icon that's i believe what lets you know when the actual new video comes out and then lastly if you could hit the like button that helps other people find this so thanks for that okay with that we'll see you in the next episode [Music] bye [Music] you
Info
Channel: Cobus Bernard
Views: 3,037
Rating: undefined out of 5
Keywords: aws, getting started on aws, getting started, tutorial, guide, devops, terraform, getting started with terraform modules, beginner's guide to terraform modules, infrastructure as code, automation, cloud infrastructure, github, github terraform, github terraform module
Id: POM73N3Vgw0
Channel Id: undefined
Length: 12min 44sec (764 seconds)
Published: Tue Sep 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.