Creating a module in Terraform - Getting started with Terraform Modules (part 1)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back uh today we're going to be taking a look at how to create a module in terraform and specifically we're going to kick it off by showing you how to use a publicly available vpc module that allows you to create a vpc with just a couple of lines of terraform which is super useful because building out of epc by hand takes quite a lot of time um i used to do this back in the day before there were public modules available so we'll we're going to start by doing that and then we're going to work through creating our own module now today is going to be the first of a two-part episode because uh sharing your module and publishing it and adding versions to it is quite a bit of work and also quite a lot of detail so today we're just focusing on how to create a module locally so let's jump into that okay so we are exactly where we were the previous time um the only change is that i have moved the dev production and testing environment variable files into a folder called variables over here um so what i want to do now is to show you why you would want to use a module first because if you don't feel this value you probably won't spend the time to learn modules so let's quickly go back to our example file over here so let's say i wanted to go to create my own vpc now avpc typically consists of the actual vpc then you have got subnets and if you want high availability you would likely need at least two of these and probably you want to have public as well as private ones so now when we look at this that equals another four resources cool now to be able to communicate with the internet public subnets need an internet gateway so we add one of those and to be able to actually root that we also need at least one root table that root table needs a an entry to connect to know how to do to our internet gateway so root for ig and that just covers the public subnets now private subnets we need at least one nat gateway and ditto we need at least a root table for that etc so you start to get the point here where i'm not even continuing here because this isn't all of the components you need for that so we are already talking um five six seven eight nine ten ten components you can imagine each of these being a resource block referencing all of them so let me show you how to do it much easier well for that we go to the public repository of modules so we're going to go terraform module so module vpc uh there's a public oh that's big repo that has a whole bunch of modules in it and if you look at the url over here you can see that this is the terraform registry modules terraform aws modules vpc now each of these modules are maintained by different people so you can always have a look here in terms of who maintains it now in this specific uh case sorry let me just make it a little bit bigger here we go um anton babenko is actually a very avid terraform user so the modules that he's built tend to be of very very high quality so i can highly recommend using the modules that he's built so in this case you can see the default one is this vpc module so let's quickly take a look we'll come back to the actual documentation at some point but i just want to show you how easy it is to get a vpc up and running so you scroll down grab the example over here copy it and let's go back to our sorry where is mine there we go module so what you can see over here is that in this case and let me just rip out the tags to make it a little bit shorter what we have is a vpc where i specify the cider and it is across three availability zones it got it has three private subnets it's got three public subnets as well and we are using nat gateways and we are also enabling a vpn gateway now you can in theory take this out so this is 11 lines of code to actually get it so there is the value so how do we go about building our module well that is uh fairly simple in terraform module is just a grouping of code so what you do is you create a directory to start off with and let's call it our um let's say new module for now and call it um creating the files in there now the convention is let me just go in there quickly is to create a file called variables main and outputs so i'm going to quickly create them tf and you can call them whatever you want because remember similar to your normal terraform directory terraform will just concatenate all of these files together but it's easier to split them this way so you can find the different variables and outputs and specific components that you are creating so right main.tf and touch output stopped here cool so what we can do now is if i go back to microsoft my variables over here i'm just going to grab these as is because we're just going to dump this code in there effectively we can say that cool similar to normal variables you can have defaults and you pass them in and then also for our main so let's go to our example quickly over here we've got our aws instance that we want to use and we put that inside the main file now this is where you would specify all the different resources that you want to um create now what you see over here is that we are referencing the data source that we had to look up the mi in our case what we want to do is we're going to call that let's say easy to mi id now we have to go create a variable for this so let's grab that and add it to our variables file quickly and like that boom type equals uh sorry string like that cool so now we've got most of this now if we were to use this module at it as is that'll create the infrastructure but we won't be able to reference the infrastructure so for to be able to reference anything we need to create an output so let's say we wanted to get the instance id there's no way for us to access this instance id inside or from inside the module unless we define an output so what we would do over here is that we grab the just the sample um like that and we go to the other folder i'm going to find an output output we call it ec2 instance id like that and we say values is equal to and we can just um output some values over here and in our case what we'll do is we'll say aws instance and as a sample one and it is the id like that now what happens is this is our module so let's quickly go and add this to our example um file instead of having the specific section over here okay so we're going to start and just create a module so that's the keyword you have to provide it here and we are going to give it a name um my ec2 instance cool and then we go ahead and now the first thing we have to specify here is where is the module source so we're going to say sorry source and we are going to say this is relative to the current directory there's a directory called new module now i will have a follow-up episode um the second one in the series uh all mini-series that shows you how to actually host this module on git and also how to do versioning for it because that becomes very important later on but for now we just want to get it going quickly now that i've got this module reference what i need to do is to pass in the variables so if i take a look at the variables over here i have to pass in the instance type so i'm just going to paste this over here to make it easier to get into the main project or the main module over there paste this is an easy way to just ensure that you set all the variables for your module so i cut that and now i'm going back to my example over here we paste that in and what i can see actually let me do this and show you another kind of nice command the instance type what i do pass in is the var ec2 instance type this is equal to the sorry var uh dot uh in ec2 instance type uh this is equal to var dots number of instances and this one is equal to uh data dots uh what do we call that up here uh oh cloud quebec come on copy cloud globus dots um id remember correctly just quickly double check uh ec2 terraform aws so we just need to double check what that value is from the data source that we grabbed um cool it's oh i need to add the type that's what i forgot something was bugging me dipped up we need to grab that and specify the type of data source we're dealing with cool there we go okay so now what i have over here is um this should allow us to actually create that instance um and what i want to do is um remove this sample instance and now we're going to run firstly actually before we do that let me show you this nice come on so if you go to terraform format this is super useful because this actually goes and cleans up your files oh i'm in the wrong directory let's go one up um and it actually has a defined formatting for your file so it's always good to build this format command into your pipeline for your code in terms of when you do your deployment so now for the moment of truth let's go ahead and say terraform plan and see what happens i made a mistake i didn't install the model install okay we need to install it first so terraform in it uh oh here we go this is broken let me take a look quickly at what's wrong okay so the mistake that i had there is that it is value and not values for output so that was a mistake on my site um so now that i've run the internet successfully let's quickly see if i can do a terraform plan let's go terraform plan boom and ah we have to enter the value because we're not passing the variable file so let's quickly fix that because i don't want to enter all of these so it is var file equals variables slash div dot tf vars cool now the plan should be able to run and in theory it should delete the old instance and create this new module for us so let's see what it says aha awesome plans to add one plans to destroy one so we are good to go now the interesting part now is that if we want to for example reference that output what we can do is have an output inside our main terraform project as well so i just want to show you how we can chain these together to make sure that we actually show the output so let's say we say output uh instance id and then we just say value and equal is module dot my ec2 instance dot what do we call the output we called it ec2 instance id so i can copy it from there and we paste that back in again and now what i can do is i'm going to run plan again and what we will see at the end once we apply is that it'll actually changes um or tell us what that output is so let's go ahead and say apply and i'll just say yes immediately and then get that to run so i'm quickly going to pause and just wait for that to finish the run cool that was surprisingly quick what you can see over here is that it actually took a full 13 seconds was the slowest resource here to destroy the instance and i've got my new instance busy up and running and also importantly here you've got the outputs over here now the other reason why outputs like this are important on a terraform project level is that there is a mechanism that you can reference these outputs between different terraform states so let's say i've got one project that is the let's say base infrastructure for an environment so um let's say my dev environment i've got a project where i create the load balancer the api gateway um some specific am roles uh s3 buckets that are used by services etc those are common to all the services and other infrastructure in that environment that's one repository then i create a secondary positive for my first set a service a third repository for my third service etc what i can do then is from the repository for my servers i can reference the remote state and actually read these values out of that state but i'll do that in a future session so that is it for today for covering um the start of how to do modules the next session will focus on how to do modules and also commit them somewhere so you can reference them as from somewhere other than your local folders so that is how you create a module locally um remember just keep an eye on the channel for the next part in this uh two-part series well i don't know if you got a series or mini-series but there will be a second part coming soon about how to publish your modules some way that you can reference it other than just your local machine as always if you enjoyed this please hit the like button that helps other people discover this episode and then also please hit the subscribe button that also helps it easier for me to you know let you know about these new episodes and then lastly um i was looking at a couple of youtube videos and apparently you need to do this please hit the comments down below um i don't know um but seriously uh use the comments ask me questions about this content or uh other content you want me to cover or general aws content um i'm planning to do quite a few uh different types of videos in the near future i'm just busy lining them up and getting a couple into the pipeline first but we hope to see you then for the next one bye you
Info
Channel: Cobus Bernard
Views: 12,756
Rating: undefined out of 5
Keywords: aws, aws ec2, terraform explained, terraform architecture, amazon vpc, infrastructure as code, vpc, automation, terraform modules, what is terraform, beginners guide, getting started, ec2, amazon ec2, devops terraform, getting started on aws, Hashicorp, beginners guide to terraform, devops, IaC, Infrastructure as code, cloud services, tutorial, guide, how terraform works, terraform, cloud architecture
Id: OeL2AlsdNaQ
Channel Id: undefined
Length: 13min 52sec (832 seconds)
Published: Thu Aug 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.