How to create terraform modules? - Part 9

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there this is rahul and in today's session we are gonna talk about terraform modules the today's session has been divided into two part in the first part we are going to see what are the basics behind terraform modules and how we can create a terraform module in the second part we are going to perform a demo where we are going to create couple of modules and we are going to deploy those two modules onto aws to start with our first part lets take an example to understand the terraform module because if we will go with the official documentation and official definition of a terraform modules then it will be really hard to understand what the terraform modules are so here in this example you can see our developer is working on this terraform script which will be responsible for installing apache http server there is one more developer in the same team who is also working on the terraform script but that terraform script is responsible for managing the e-commerce application so that terraform script is completely different and that is used for provisioning the infrastructure which is needed for an e-commerce application with the time there is a need for installing apache http server alongside with the e-commerce application so here developer 2 has its own terraform script but into that terraform script he has the infrastructure setup only for e-commerce application there is no instruction which is provided for installing apache http server but on the other hand developer one has a terraform script which is responsible for only installing apache http server so is there a way a developer too can borrow the data from script from developer one so that he can install apache http server so there comes the terraform module so what we can do over here is we can uh create a module in which we can define how to install http server and we can we can import that module into the developer to terraform script so what will happen with it is we can reuse the functionality of installing apache http server and we can use the terraform script inside our developer to terraform script so with that what will happen is you don't need to rewrite the whole function or the whole terraform script for installing apache 2 http server and we can just reuse this complete terraform script as a module for developer 2. all right now we have seen an example and now we got an idea like how terraform module works let's head over to demo and in the demo what we are going to do we are going to create a couple of modules and we are going to name them as a module 1 and module 2 and we are going to reference both the module inside our main.tf that is going to be our main terraform file so once we execute or once we apply the using the terraform apply command then both the module is going to be executed and whatever code or whatever terraform infrastructure code written into that module is going to be applied so for this case we are going to apply this on aws all right here you can see this is my terraform module projects and once i open this terraform module project then you will notice there are a couple of module which i have created so here this is module 1 and this is module 2. along with that you will also notice there is a main.tf that is going to be your parent tf or parent terraform file so here first of all you should notice over here is that we are referencing module 1 as well as module 2. so how to reference a module or how to call a module so first of all what you need to do is you need to create this main.terraform file and into that file you need to first define the provider since we are going to work on aws that's why i have defined the provider as aws apart from that you need to provide the reason access key and the secret key if you don't know how to fetch this reason access key and secret key then i would recommend you to follow the part one where i have shown like how you can create an ec2 instance and how you can fetch this information like a reason access key and secret key so i would highly recommend to follow that part one all right now we have defined the provider the second thing what you need to do is you need to call a module so here the first keyword which you need to put over here is module apart from that you need to define the name of the module so this model name can be user defined so you can put any name or any type of name of your choice for the module so here i have put in the name as j hook web server one because both them in both the module we are going to install apache http server moving further this is my second module and here you can see again i have used the keyword as module and i have kept the name as jhook web server 2 and apart from that we have defined as a source so here this is the source and here we need to define the path of the module from where it is going to pick the module so if you look carefully over here so this is my main.tf and just parallel into the same hierarchy you can find the module 1 as well as the module 2. that's why the path is relative over here followed by i think it's a forward slash so these are the forward slash we are going to use to reference the module one as well as the module two along with the today's session i'm just gonna share this guide this is my blog post where i have taken the same example which i have just explained to you so here you will find all the description along with the terraform script which i have used into this demo so i'll put the link of this guide into the description section of this video so feel free to use this guide and you will find this terraform script over here so this is the terraform script for module one which you can copy from here and this is the terraform script for module two so this is also available here so if you need uh to make some kind of a changes then please feel free to make and make it to your own use now we have seen the complete structure of our terraform module project along with the module one and module two and with our parent main.tf file let's head over to module one and see what's what are the content inside our module one so if you open this module one directory then you will find a main.pf output.tf and variables.tf so you can easily resemble this structure with a similar terraform project so in a typical terraform project you will have a main.tf output.tf and variables.pf so module is just similar to a separate workspace or a separate terraform project where it has its own main.tf file so this is going to be our first main.tf file of our module 1. so let's open the main.tf this main.tf file contains the code for installing apache 2 server onto our ec2 instance so if you look carefully this is the bash script which is needed for installing apache 2 server onto our ec2 instance but let's start from the first block so this is going to be the first block which we need to define and we need to define the required version which is needed for this setup so we will be using the version 0.12 secondly we need to create an easy to install so we need to define it as a resource and resource name is aws instance and here uh the name of uh aws instance so it can be anything of your choice just put a suitable name over here then secondly we need to put the machine id and here i have taken the variable that is ami underscore id and the variable value i have defined inside my variable.tf so this is the ami id which i will be using for my ec2 instance after that we need to define the instance type and that also i have defined into the variable so if you go into the variable.tf then you will find the instance type which i am using is t2.micro so this is just for an example purpose that's why i am using the t2.micro but in case if you are working on a production environment then just choose some higher cpu maybe t2.large or t2 dot extra large that would suit your need so third thing is we need to define the key name and this is just an optional thing uh but if you are interested in setting up your ssh keys then just create one key pair and here assign the public key name this key i have defined over here in the bottom so this is the resource which we need to create for key pair so the name should be aws key pair and the key name should be aws underscore key and this is my public key over here which i have generated for this setup i'll upload a separate video on how to create an ssh key pair and use it with a terraform but in case if you want to know like how to set up your ssh keys on aws not with terraform then i have already i will put a link of that video into the description section i have already uploaded that video on to my channel so just go back and check that video i'll put somewhere into the description or either into the tooltip section okay coming back to uh the modules the next thing which we need to do is we need to create a security group and here you can see this is my security group which i have already defined over here so here you will find the aws security group and the name of the security group that is main apart from that you need to put the name and description uh which you can put it by yourself with uh with your suitable name secondly uh since we will be installing the apache http 2 server so we need to enable the port 80 that is we need to open it otherwise we will not be able to access our home page or the landing page of our apache 2 server second these ports are needed if you want to ssh or if you want to login into your ec2 instance after you start your ec2 instance using terraform so this is the port which is needed and this is the default port which i just kept it and with the cider block so it this port is just optional if you want to keep it that's okay if you don't want to keep it that's also fine okay so now we are done with our oh no we are left with one more thing the installation script so this is the installation script which is needed for installing apache 2 server and here you can see this is the bash script actually and if we will go one by one so whenever you need to define a long bash script so this is the way you need to define inside your ec2 instance resource so that is going to be aws underscored instance so this is the resource tag inside you need to define the bash script okay so now we have gone through ami instance type key name security group and now this is the bash script which we need to go through so we'll start with first the first command is we will go and update our package manager second we are going to install apache 2 using this command sudo apt install y apache 2. third we are going to check the status of our apache 2 fourth we are going to start the apache 2 service fifth we are going to change the permission so as you know once you install apache 2 then you will get a directory created like a slashware www html so this is the directory location and in that directory you will find the html files like the home page or the landing page of apache 2. so once you access the apache tool so that that the default page will be available on to that directory so we need to change the permission of that directory so that user can access it the current user can access it actually so this is the command for that and the last one is we are going to create an html page with some hello message and the message is going to be hello this is module 1 and with some instance id of the ec2 instance which we are going to start so this is our complete uh terraform file or the main.terraform file of module one where we are going to install apache http server uh with some custom message like hello this is module one so this is going to be my module one exactly the same thing which i am doing into the module two also and here if you open the module two then you will not notice any much of a difference so here everything is same and you can see uh this is the bash script which i am using and it is also going to install the same apache 2 and the only thing which i have changed over here is the module 2 message and here you can see this is the message which i have changed so this is also going to install the apache 2 server onto onto aws now we have gone through all the modules and the parent.main.tf now it's time to run our terraform script so probably i'll reduce the font over here and i'll open the terminal and i will reduce the path over here so that yeah that's looks much better now what we need to do is we need to go into the directory where our terraform module project is so i will go into terraform modules and here if you do ls then you will find this is our main.tf that is going to be our parent df and this is these are the model module 2 and module 1. so what we need to do is the first command which we need to run is teraform in it okay that's been done the next command which i'm going to run over here is the terraform plan okay now here you can see there are five resources which is going to be added so now i have finished my terraform plant command and the next command which i'm gonna run is terraform apply i'll clear the screen and i'm just gonna type data form apply there seems to be some problem okay so i think there are form yeah that's the correct command type yes all right so here you can see five resources has been added and applied to aws let's head over to our aws dashboard and see if we are able to if we are we have created those resources or not okay this is my dashboard and here this is my ec2 instance which i need to click and as you can see uh we have couple of modules module one and module two and each module we have started an easy to install so here you can see there are two ec2 instance which is of type t2.micro which are up and running so here you can see this is my instance id1 which i can click and open and i'm just going to open the second one also so this is the first instance id or the first instance which i have provisioned using either my module one or module two and this is the second one so both the ec2 instance are up and running that means we are able to execute our terraform module project successfully now after verifying that our ec2 instance is running the next thing which we need to verify is we need to verify that we have installed the apache http server successfully or not so for that what you need to do you need to copy this public ip address from here go to the next tab paste that ipa address and always remember do not put https because we haven't installed any uh ssl certificate so we just simply need to type in the ip address along with http without any s and just hit enter so here you can see this is the message hello this is module 1 and instance id is this this so this was the message which we have put when we were installing the module one and which we you can verify from here also uh i will reduce the screen size and this is the main.tf of module one as you can see and this was the message hello this is module one and the instance id and insta in the instance id we are putting just the instance id of my aws instance easy to instance okay similarly if you go back to the other instance and here just copy this ip address go to next tab paste it over here and simply type the http without us and that's it and this is my module 2. so now you can see uh we are able to execute both the module and we are able to install the apache http server using module one and module two and this is my main dot tf which were internally calling module one and module two so this main dot tf was doing nothing it was just simply calling the module one and module two so now you know like how to create terraform modules and what are the concepts behind the terraform module and the today's demo just like a proof of concept you can customize the whole project with your own software packages which you want to install along with the other terraform module so use this example just as a reference and i'll share the guide along with the code which you can find into the description section of this video so feel free to use it and if you have any question related to the today's session then please put down in the comment section and i'll try to get back to you and if you are interested into the similar content like kubernetes docker devops helm chart then please do follow this channel where i keep on uploading the similar content on weekly basis so till then see you into the next session bye bye
Info
Channel: Rahul Wagh
Views: 55,975
Rating: undefined out of 5
Keywords: terraform, terraform modules, aws, devops
Id: 9w3ZFBvdVNc
Channel Id: undefined
Length: 18min 11sec (1091 seconds)
Published: Mon Aug 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.