Use Terraform Module to Build a 3 Tier AWS Network VPC

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to another lecture in this lecture we will use terraform module to create a 3-tier vpc architecture when you create terraform module it allows you to use the same code to create as many vpcs as you want to start the first thing i want you to do is open visual studio code once you've opened visual studio code the next thing i want you to do is open your project folder to open the project folder i'll select the explorer here i'm going to click open folder browse to the directory where you saved your project folder my project folder is on the desktop i'll select desktop and on my desktop my project folder is the terraform modules i'll select this folder once you select this folder make sure you don't click to go into the folder just select it and once you select it click select folder there you go we have opened our project folder once you open your project folder you should see the name of your project folder here i'm going to close this to create the terraform module for the vpc the next thing i want you to do is create a folder in this directory to create a folder right click in here and select new folder give the folder a name and call it modules once you've given the folder a name press enter we have successfully created the folder that will store all of our terraform module and you can see it here the next thing i want you to do is create another folder in this modules folder and we will call that folder vpc this is the folder that will store our terraform module for the vpc to create a folder in the modules folder first make sure you select the modules folder by clicking it once you select it click this icon to create a new folder and call that folder vpc once you've called the folder vpc press enter there you go we have created our vpc folder in the modules folder the next thing i want you to do is select the vpc folder once you have selected vpc folder we will create three teraform files in that folder the first terraform file we will create will call it main.tf so make sure you click on the vpc folder click on it once you click on it select this icon to create a new file then call the file main.tf once you've called the filemain.tf press enter we will create the second file in the vpc folder select the vpc folder then click new file we will call this file variables.tf once you've given the file a name press enter once you have created the variables file we will create another file in this vpc folder and we will call that file outputs.tf make sure you select the vpc once you select the vpc click new file here name the file output.tf once you've given the file a name press enter these are the three files we will use to create our vpc module the first file is main.tf and this is the file that will contain our code the second file is variables.tf and this is the file that will contain our variable the top file is output.tf and this is the file we will use to export the value we want to reference in another module to start writing our code first i want you to close the output.tx file for now we are just going to leave the main.tf and the variable.tfs file for now the next thing i want you to do is download and open the reference file i created for this project in this video's description this is the reference file we use to create the vpc module and in this reference file i've included the resource type and arguments that we need if you want to find any of this resource type in the terraform documentation all you have to do is select these two right click to copy it and paste it in google it will take you to the exact location of this syntax in the terraform documentation the next thing i want you to do is select everything in this reference file to select everything in this reference file press ctrl a then right click to copy once you've copied close the reference file then come back to your main.tf file make sure you are in the main.tf file and paste it in there once you've pasted the syntax in there scroll all the way up the next thing i want you to do is select the variables.tf file once you select it it is going to open it i want you to click and hold it then drag it somewhere here so that way you have to split screen on this side we have the main.tf and on this side we have the variable.tf i'm going to drag this somewhere here and to create space on your screen you can also click the explorer here to close the explorer to start writing our code the first thing i want you to do is come to the variable file in the variable file we will create two variables called the first variable region type variable once you type variable i'm going to autocomplete then for your variable name call it region this is the variable we will use to specify the region we want to create the vpc in once you've created your variable we won't enter any information in here i want you to press backspace and also come here press backspace here so your code looks like this the next variable we will create will call it project name and this is the variable we will use to give our project a name so what i want you to do is copy this region variable i'll select it and copy it and i'm going to come down here i'll paste it here then once you paste it all you have to do is modify the variable name i'm going to delete this and i'll call this variable project name once you have created both variables next i want you to come to the main.tf file and in here we will start writing our code the first syntax will create a vpc and this is the resource type and reference name i have given it the first argument we will enter is decider block for decider block let's create a variable for it so come to your variables file in this variables file i'm going to copy this and press enter and paste it here once you've pasted we are going to modify the variable name call the variable vpc sider this is the variable we use to specify decider block for our vpc so for decider block argument we are going to reference this variable to reference the variable we are going to type va dot the name of the variable which is vpc sider the next argument is the instance tenancy and we'll type default for the value in double quotes type default the next argument will enable dns hostname in our vpc and for that argument we have entered true for the next argument we will give our vpc a tag name for the tag name i want you to come into this bracket and in this bracket we want to reference our project name so anytime we create a vpc it will be project name vpc so in here i want you to reference your project name variable to reference your project name variable type var dot project name which is what we entered here so you can copy this name here and paste it after the period the next syntax will create an internet gateway and attach the internet gateway to the vpc the first argument is the vpc id we created the vpc up here so what you can do is copy the resource type and the reference name once you copy it under vpc id paste it here then remove the double quotes between them add a period come all the way to the end add another period and type id the next argument we will enter is the tag name and for the tag name we want to reference our project name so that it reads project name dash internet gateway similar to what we did up here so what i want you to do is copy this here and in this bracket you can paste it in there so this is going to be our project name dash internet gateway the next syntax will use data source to get a list of all the availability zones in the region where we are creating our vpc and this is the syntax to do that in the next syntax we will create our public subnet z1 this is the resource type to create a subnet and this is the reference name i've given it the first argument we will enter is the vpc id for the vpc id we referenced it up here so i'm going to copy this and i'll come down here and paste it here the next argument we will enter is the cider block for the cider block let's create a variable for it so i'm going to copy my variable here then i'll come down and paste it in there i'm going to modify the variable name i'll call this variable public subnet easy one-sider once you've given your variable a name select the name and copy it then under decider block argument we are going to reference that variable type var period and paste the variable name next to it the next argument we will enter is the availability zone remember we are using data source to get a list of all the availability zones in the region where we want to create the vpc so to specify a specific availability zone in that region we are going to copy this information here so copy everything here and paste it here then i want you to remove the quote add a period come here remove the quotes add a period then come all the way to the end remove the quote add another period once you add the period type names hello guys so when you get to this point where you are typing the value for your availability zone it should be data dot aws availability zones dot availability zones dot names type the name with an s i made an error here which i fixed later on in the lecture but i want you to correct yours now so when you type the value for your availability zone you should type it like this it is data dot aws availability zones dot availability zones dot names names with an s also for other syntax in this lecture where we referenced our availability zone make sure you type it with an s after the name so it is names not name thank you bracket in that bracket type 0. when you type 0 in here this is called indexing what this means is when we get the list of the availability zones in the region where we want to create our vpc we will select the first availability zone in that region if you change this number to one that means that when you get a list of all the availability zones in this region you want to select the second one for this project we are going to select zero here for our first availability zone the next argument is map public ip on launch and for this argument we will type true the next argument is tags and we'll give it a tag name we'll call it public subnet az1 the next syntax will create the public subnet in the second availability zone this is the resource type to create a subnet and this is the reference name i've given it the first argument is the vpc id remember we referenced our vpc id up here so i'm going to select this to copy and i'm going to paste it here the next argument we will enter is the cider block for our cider block let's create a variable for it i'm going to come to the variable file and i'll copy this once you copy press enter and paste it down here then modify the name and change this to easy to so this variable should read public subnet az2 sider once you have created the variable name select it and copy it once you copy it come back to the main.tf file and we are going to reference that variable to reference the variable we'll type var dot and paste the variable name there the next argument is the availability zone for this subnet we will create it in the second availability zone so what i want you to do is copy this once you copy this availability zone for public subnet easy one paste it here and all you have to do is modify this number so here i'm going to change this number from zero to one so this means that we are creating our public subnet z1 in the first availability zone and we are creating our public subnet az2 in the second availability zone the next argument is map public ip on launch and we will type true the next argument is tags we'll give it a name and we'll call it public subnet ez2 the next syntax will create a route table and add a public route to it this is the resource type to create a route table and this is the reference name i've given it the first argument we will enter is the vpc id we referenced our vpc id up here i'm going to select it and copy it here and i'm going to paste it here the next argument is decider block for decider block the destination is going to be anywhere on the internet so here i want you to type 0.0.0.0.0 in double quotes type your cider block this is how you enter inside that blog that the destination is anywhere on the internet the next argument is the gateway id and this will be the id of our internet gateway remember we created our internet gateway up here or scroll back up this is our internet gateway we will copy the resource type and the reference name then come back down paste it in here remove the double quotes between them add a period come all the way to the end add another period and type id the next argument we will enter is tags for the tag name we will call it public route table the next syntax is going to associate the public subnet sd1 to the public route table we just created this is the resource type to associate a subnet with a route table and this is the reference name i've given it the first argument we are going to enter is the id of the subnet we want to associate with the route table the id of the subnet we want to associate in this syntax is the id of our public subnet is the one so what i want you to do is scroll up this is the resource that is creating the public subnet easy1 select the resource type and the reference name copy it then come back down paste it in here remove the double quotes between them add a period come all the way to the end add another period and type id the next argument we will enter is the id of the route table we want to associate this public subnet is they want to the route table we want to associate the public subnet easy one to is the public route table we created up here so i want you to select the resource type and the reference name copy it come back down paste it in here remove the double quotes between them add a period come all the way to the end add another period and type id this is how we associate the public subnet easy1 to the public route table we created the next syntax is going to associate the public subnet az2 to the public route table this is the resource type and the reference name the first argument is subnet id so let's scroll up this is the syntax that is creating the public subnet easy to copy the resource type and the reference name once you've copied come back down paste it here remove the double quotes add a period come all the way to the end add another period and type id the next argument is the route table id and this is the route table we want to associate the public subnet az2 z22 the route table is the public route table and we referenced it here so just copy this here and paste it here the next syntax is going to create the private app subnet iz1 this is the resource type to create a subnet and this is the reference name i've given it the first argument is vpc id so let's scroll up to get our vpc id we referenced our vpc id here so you can copy this come back down and paste it here the next argument is cider block and this is going to create the cider block for our subnet to create the cider block let's create a variable for it so in here i'm going to copy this variable once you copy the variable i'm going to press enter and paste it down here then modify the variable name the variable name is going to be private app subnet is a one-sider once you have entered the variable name select it to copy it then we are going to reference it here to reference it type var period and paste the variable name next to it the next argument we will enter is the availability zone we want to create this subnet in the first availability zone so let's scroll up when we created our public subnet easy one we created it in the first availability zone so here what we can do is copy this and come back down then paste it here remember the index 0 is the first availability zone index 1 is the second availability zone the next argument is map public ip on launch for this we will type false the next argument is tags for the tag name we'll call it private app subnet az one the next syntax will create the private app subnet in the second availability zone for this syntax this is the resource type to create a subnet and this is the reference name i've given it the first argument is the vpc id we reference the vpc id here i'm going to copy this and paste it here the next argument is decider block for decider block we will create a variable for it so i want you to come to the variable file copy this and press enter paste it down here all we have to do here is modify the name and i'll change this to two so your variable should read private app subnet is the two sider once you've created the variable copy the variable name once you've copied the variable name we will reference it here type var dot and paste the variable name there the next argument is availability zone and we want to create the private app subnet az2 in the second availability zone so let's copy this here and paste it here and all we have to do is modify the index change it from 0 to 1. so what this means is we will create the private app subnet easy2 in the second availability zone the next argument is map public ip on launch because this subnet is a private subnet we don't want any instance with launching it to have a public ip address so here we are going to type false the next argument is tags for the tag name we'll call it private app subnet az2 the next syntax is going to create the private data subnet in the first availability zone this is the resource type to create a subnet and this is the reference name i gave it the first argument is vpc id so let's copy the vpc id up here and paste it here the next argument is the cider block for decider block we will create a variable for it so let's copy this variable in our variable file and we are going to press enter and paste it here once you've pasted the variable we'll modify the variable name and this you read private data subnet is the one the variable name should be private data subnet easy one i'm going to drag this so you can see everything better the variable is private data subnet easy one once you've entered the name copy the variable name and we will reference it here here we are going to type var dot and paste that variable name there the next argument is the availability zone we will create the private data subnet easy one in the first availability zone so let's scroll up this is where we are creating this subnet in the first availability zone so let's copy this and come back down we'll paste it here make sure the index is zero zero is the first availability zone the next argument is map public ip on launch for this we will type false because this is a private subnet the next argument is the tags for the tag name we'll call it private data subnet easy one the last syntax is going to create the private data subnet in the second availability zone this is the resource type to create a subnet and this is the reference name i've given it the first argument is the vpc id and this is the vpc we want to create this subnet in we reference the vpc here so copy this and paste it here the next argument we will enter is decider block for this subnet let's come to the variable file to create a variable for it i'll copy this variable then i'll press enter paste it here all you have to do now once you paste it is modify the name but we change it to private data subnet easy to sider so make sure this reads private data subnet is the two sider once you've created the variable copy the variable name then come back to your main.tf file we will reference that variable we'll type var dot and paste the name of the variable next to it the next argument is the availability zone we will create the private data subnet easy to in the second availability zone so what i want you to do is copy this here and paste it here when you paste it don't forget to change the index to one so this is going to create the private data subnet az2 in the second availability zone the next argument is map public ip on launch and it is going to be false the last argument is tags for the tag name we'll call it private data subnet az2 we are done creating the main.tf file and the variable file for our vpc module the next thing i want you to do is save this file to save the file click file up here and select save all review your work to make sure you've entered everything correctly the next thing we will create for the vpc module is the output the output will allow us to export some values from this vpc and we can reference those values when we create other resources to create our output i want you to select in here then open the explorer by clicking this select outputs.tf and that is going to open your outputs.tf here once you open the outputs.tf you can close the explorer again the first output we will create will export our region to create the output type output here once you type output and press enter we are going to enter the name of our output here for the name of the output enter region once you've entered the name of your output come down here and down here type value equals it is value space equal sign space and the value we want to enter here is our region variable so in here i want you to type var dot region so what this means is whatever value we enter for our region variable we want to export that value here the next value we are going to export is our project name so i want you to copy this output press enter and paste it here we are going to modify it the name of the output is going to be project name the name of the output is project name and the value is going to be our variable project name so here i want you to change this and type project name so just like we did for the region this means that whatever value we enter for the project name we want to export that value in our outputs file the next value we will export is our vpc id i want you to copy this output press enter and paste it here we are going to modify the output name and change it to vpc underscore id we will use this output to export the id of our vpc for the value we are going to reference our vpc id remember we reference our vpc id in the main.tf file here so i'm just going to select this to copy it and paste it in here the next output we want to create will export the public subnet easy ones id so what i want you to do is select this output here copy it press enter twice paste it down here we are going to modify this output and we will change the name for the name of this output call it public subnet is the one id this is the name of the output and this output will export the id of our public subnet az1 for the value of this output let's come to our main.tf file and scroll up we created our public subnet easy1 here so to reference the id of this public subnet is the one we'll copy the resource type and the reference name we'll paste it here we'll modify it remove the double quotes add a period come all the way to the end add another period and type id this is how we reference the id of our public subnet az1 the next output we will reference is the id of our public subnet az2 so i want you to select this output copy it then press enter paste it here we will modify the name it is going to be public subnet az2 id change the ac to 2 and for the value this is where we created our public subnet easy to copy the resource type and the reference name once you copy it paste it in here remove the double quotes between them add a period come all the way to the end add another period and type id the next output will export our private app subnet easy one so what i want you to do is select this output copy it come down paste it here we are going to modify the name and we'll call it private app subnet as a1 id once you have given the output a name for the value we'll come to our main.tf file scroll down this is the resource that is creating our private app subnet is the one copy the resource type and the reference name paste it here remove the double quotes between them add a period come all the way to the end add another period and type id the next output we will create will export the value of our private app subnet az2 so i want you to copy this output here come down paste it here we are going to modify the name of the output this should be private app subnet az2 id change this to two i noticed that i spelled my subnet wrong so i'm going to change this to subnet and i also change this here to subnet the next thing we will do is modify the value this is where we created our private app subnet iz2 so copy the resource type and the reference name and we will paste it here remove the double quotes between them add a period come all the way to the end add another period and type id the next output we will create will export the value of our private data subnet az1 so i want you to copy this output here press enter paste it here modify the name of the output it is going to be private data subnet change this to data subnet az one id and for the value this is the resource that is creating the private data subnet easy one so i want you to copy the resource type and the reference name i'm going to replace this with it remove the double quotes add a period come all the way to the end add another period and type id the next output will export the value of our private data subnet easy to what i want you to do is select this output copy it then come down paste it here we will modify the name of the output make sure the output read private data subnet is z2 id change the az1 to ez2 then for the value this is where we created our private data subnet az2 copy the resource type and the reference name come to the value here and we will paste it here remove the double quotes add a period come all the way to the end add another period and type id the last output we will create will export our internet gateway the reasons why we are creating this output is because we have to reference them when we create other resources so i want you to copy this output and come down paste it here we will modify the name of the output called the name of the output internet gateway once you enter the name of the output but the value will reference the resource that is creating our internet gateway in the main.tf file scroll up this is the resource that is creating the internet gateway i want you to copy the resource type and the reference name copy it and for the value here paste it here remove the double quotes between them and add a period this is all you need to do to reference the internet gateway we are not going to add id at the end we just want to reference the internet gateway these are all the outputs we need to export to complete this project once you've entered your outputs the next thing i want you to do is review your syntax to make sure you have typed everything properly once you finish reviewing your work the next thing i want you to do is save your file click file and select save all this is all we need to do to create the module for the vpc the next thing i want you to do is close this file i'm going to click this x to close everything then select your explorer to open the explorer here we have created our vpc module now that we have created the module for the vpc we can reference the module to reference the module i want you to create a folder here when you are creating the folder make sure you don't create it in your modules folder just click anywhere down here and create the folder to create the folder select new folder here give the folder a name i'll give the folder the name of this project and i'll call it jupyter website ecs once you've given the project a name press enter to make this clear we will use this folder to create a vpc so basically what i'm trying to say is for every project you are trying to create where you want to reference this module you will create a folder for that project and you can reference this module from that projects folder the next thing i want you to do is select this project folder you created and create a new file in it once you select it we will create a new file in it click new file we'll call this post file backend.tf once you've coded backend.tf press enter if you notice the backend.tf file i created is in the jupyter website let me drag this out so you can see if you notice if i select this drop down my backend.tf file is in the jupyter website ecs folder if i click the drop down on this module you can see we have two folders one we called it modules the other one we called it jupyter website ecs so make sure when you are creating your folder structure that you are creating the file in the right folder once you've created the backend.tf file i want you to download and open this reference file in this video's description this is the reference file where we use to create our backend what i want you to do is select everything in this reference file i'll press ctrl a to select everything right click to copy it close it then come back to your backend.tf file paste it in there we will use this backend file to store our state file for the jupyter website ecs project in s3 so what i want you to do is enter your value here the first argument we will enter is our bucket name we created the bucket for our state file in the previous lecture so i want you to go to the management console in the management console type s3 in the search box and select s3 on that services in the s3 console this is the bucket we are using to store our terraform state file select it select the bucket name here and copy it once you've copied the bucket name go back to your terraform file in double quotes paste the bucket name here the next argument we will enter is the key and this is the unique name terraform we use to store our state file for the key i want you to name it jupyter website ecs dot tf state in double quote i will name it the same name of my project you can call this key any name you want but for best practice i always like to name the key the same name of my project because we will be storing multiple state files for different project in the same s3 bucket so it is best to name your state file the same name of the project the next argument is the region the region we are using for this project is us east one so type us east one in here the next argument is profile and this is the profile we configured on our computer in the previous lecture the profile contains the access key id and secret access key that we will use to authenticate with our aws environment for the profile name that i configured on my computer i called it terraform user so i'll type it in here this is all we need to do to create our backend file that will store the state file for our project in s3 the next thing i want you to do is save this file click file and select save all once you've saved this file you can close it next we are going to create another file in our project folder so select your jupyter website ecs project folder click new file give the file a name and call the file variables.tf once you've given the file a name press enter for the variables file you just created in your project folder what you must do is you must open this variables file you created in your vpc module so select this variables file i'm going to click it and drag it here so i can split the screen and all you need to do is copy all these variables the variable you are seeing here this is the variable we created in our vpc module so what you must do is copy all these variables and you will paste it in the variables you created in your project folder so come here and paste it there so basically what we are doing here is we copy all these variables from the vpc module and we paste it in the variables we created in our project folder once you've pasted the variable in there close this and that's all you need to do to create your variable the next thing we are going to do is create another file in our project folder i will call this file main.tf so what i want you to do is select your project folder i got my project folder jupyter website ecs once you select your project folder click new file call the file main.tf once you've given the file the name.tf press enter the main.tf file is what we will use to reference our vpc module so for every project folder you create in the future you will have a main.tf file in it and in that min.tfr that is how you will reference your vpc module once you've created your main.tf file the first syntax we will create in neet is the syntax to authenticate with our aws environment so the first thing i'm going to do is type my notes to explain what the syntax is doing once you type your notes press enter we will configure our provider to authenticate with our aws environment to configure your provider you will type provider once i start to type provider i'll press enter for auto complete then i'll give my provider a name the cloud provider i want to authenticate to is aws so i'm going to type aws in here once you type aws in here come down here the first argument we will enter here is region so enter region once you enter region press space equal space again before we enter the value for our region the next thing i want you to do is take your variables file bring it here to dock it here so we have a split screen i'm going to close the file explorer for now so for the region we want to reference the variable region in our variables file so here we are going to type var dot region once you type your region press enter the next argument we will enter is profile type profile in here for profile press space and equal sign and another space and for our profile this is the profile name we configured on our computer in the previous lecture the profile that i configured on my computer in the previous lecture i called it terraform user so i'll type terraform user in here in double quotes this is all we need to do to create the syntax to authenticate with our aws environment to make your code cleaner press the tab key here i always like to arrange the equal sign under each other so press the tab key here again and this will arrange the equal sign under each other once you have created your provider syntax press enter and press enter again the next syntax will create our vpc and to create our vpc we will reference the vpc module to start here type a note and call it create vpc once you've typed your notes press enter to reference the vpc module the next thing we are going to do here is we are going to reference our vpc module to reference your vpc module you will start by typing module in here once you type module press enter to auto complete it the next thing you will do is give your module a name you can call it any name you want since we are using this module to create vpc i'll call this module vpc once i have given your module a name press the down arrow key the next argument we are going to enter in here is source this is very important so type source in here space equal space again the source will allow us to reference the vpc module if i hover over the source here you can read the definition it says source is where to load your module from so once you enter source for the value we will enter double quotes double period forward slash where our module is located remember our module is in the modules folder so here we'll call it modules the modules i enter in here is the same exact name as the modules folder that my vpc is in so whatever you call the folder where your vpc module is in that is what you enter in here then enter another forward slash in this modules folder my vpc module is in the vpc folder and you are going to enter vpc in here this is how you specify the path to your vpc module it is double period the double period means go up to directory since we are in a folder it is going to come out of this folder and come into the modules folder and in the modules folder it is going to find my vpc module in the vpc folder this is how you specify your source it is very important once you have specified the source press enter the next thing we will do is list all the variables we have in our variables file here to list all this variable i'm going to copy this and paste it here add an equal sign before i enter the value i'm just going to list everything for now to make everything clear all we did is we list all the variables we have under our variable files in here if you look starting from region all the way to the end is the same thing i have here all the way to the end once you have listed all your variables here another thing i like to do is arrange the equal sign under each other so what i'll do here is press the tab key and i will arrange the rest of the equal sign under each other once you have arranged all your equal sign under each other i'm going to close the explorer for now so i have more space i'll drag this here the next thing we need to do is enter the value for this argument here to enter the value all you have to do is type var and the name so for every one of these all you do is type var dot the name for example for the region we will type va dot region for project name we will type va dot project name for vpc cider we will type var.vpc cider for public subnet is the one sider we will type var dot public subnet is a one sider for public subnet az2 sider we will type var dot public subnet is the two sider for private app subnet is the one sider we will type va dot private app subnet is the one slider for private app subnet hd2 sider we will type va dot private app subnet easy two sider for private data subnet is the one slider we will type va dot private data subnet is the one sider for private data subnet easy two sider we will type va dot private data subnet is the two sider this is all we need to do to reference our vpc module the next thing i want you to do is save your file click file and select save all once you've saved your file open the explorer again once you open the explorer before we proceed to the next thing i want you to review your work to make sure you've typed everything correctly once you've reviewed your work and everything looks fine i'm going to close this main.tf file the next thing we will do is create another file that contains the values of our variable we will create this file in our project folder and my project folder i called it jupyter website ecs i'll click on this project folder select new file and call the file terraform dot tf verse make sure you spell the name correctly it is terraform dot tf vars once you've typed terraform dot tf vars press enter in the terraform.tf vars this is where we will enter the values for our variables so what i want you to do is take your variable and drag it here to split the screen the next thing i want you to do is list all the names of your variables from your variables.tf file into the terraform.tf vars so you will start a region project name vpc sider all the way to the end you will list it here to make sure you don't spell anything wrong what i like to do is copy paste so i'll copy my region paste it here and enter my equal sign when you enter your equal sign make sure there isn't a space there just enter your equal sign i'll copy my project name i'll press enter here paste it in here and enter my equal sign press enter paste it here and enter the equal sign i'll copy the public subnet as a one sider and i'll copy the public subnet az2 slider so copy everything from the top to bottom and paste it here and enter the equal sign once you have entered the name of all your variables in the terraform.tf file the next thing we will do is enter the actual value we want to use for region we want to create this resource in usb1 so we will type usb1 in here in double quotes for the project name i'll call it jupiter for the vpc cider block i will enter a cider block of 10.0.0.0.16. for the public subnet is a one-sider i'll enter insider block of 10.0.0.0.24 for the public subnet is the two sider i'll enter insider block of 10.0.1.0.24 for private app subnet is the one sider i'll enter insider block of 10.0.2.0.24 so what i'll do is copy this paste it here and i'll modify this i'll change it to two for the private app subnet easy two sider i'm going to enter a cider block of 10.0.3.0.24 so what i'll do is copy this paste it here and i'm going to modify this and change it to three for the private data subnet is the one sider i'm going to use a cider block of 10.0.4.0.24 so i'm going to copy this paste it here and modify this by changing this to four and for the last variable private data subnet is the two sider i'm going to enter a cider block of 10.0.5.0.24 so i'm going to copy this paste it here modify it and change this to five this is all we need to do to write the syntax that will create our vpc using terraform module once you've created everything again review your work to make sure you've typed everything correctly then i want you to save your file click file and select save all once you select save all i'm going to close my variables.tf file and you can also close your terraforms terraforms.tfvas file if you want you can open your main.tf file under your project folder the next thing we will do is use this module to create the vpc in our aws environment to create the vpc in your aws environment make sure you right click on your project folder so my project folder is jupyter website ecs right click on it and select open in integrated terminal once you right click on your project folder and select open in integrated terminal it will open the terminal to your project folder if you look here my tamina is open to the jupyter website ecs folder this is very important make sure you open your terminal to your project folder once you've opened your terminal to your project folder the first thing we will do is initialize with our aws environment to initialize with your aws environment we are going to type terraform init once you type terraform in it press enter terraform is initializing with my aws environment and i'll give it some time to finish initializing there you go terraform has successfully initialized with my aws environment the first thing you must do is initialize terraform once you have initialized terraform the next command we will type is terraform apply terraform apply will create the vpc in our aws environment once you type terraform apply press enter so here this is one of the things about working with terraform i am getting this arrow unsupported attribute this is also important when you are working with terraform it is rare that you will be able to write your syntax without missing something somewhere anytime you get an error how you can troubleshoot the error is here i can already see in what file i have the error in i have the arrow in my modules vpc main.tf file that is what that is showing here and the lines that i'm having the error is line 114 line 102 line 90 line 78 line 40. if i scroll down terraform is also giving me the hint here for each of these syntax i am using name but i should have used names so all i am missing here is s this is one of the things i like about terraform so anytime you are running your terraform code and you get an error make sure you read the error message to understand what you did wrong i am glad this happened to show you this part of terraform so the next thing we need to do is fix our syntax go into the modules vpc and select the main.tf file so in this main.tf file scroll down and for your availability zones where we have the name here we need to change it to names so i'm just going to scroll down on all of them i enter the name and i'll change it to names also if you look in the error message terraform is telling you the lines that you have that error if you see this is line 78 and terraform is telling me i have that arrow in line 78 this is another way you can troubleshoot your error i'll enter s here scroll down the next one is line 90 which is here i'm going to enter s here scroll down the next one is line 102 you can see 102 i'm going to enter s here scroll down the next one is line 114. if i scroll down here line 114 i'm going to enter s here i believe these are all the places that i have the arrow so once you fix it click file and save your file again i'll select save all then come to your terminal and we are going to run terraform apply again type terraform apply to run the last command you type press up on your arrow key and that is going to bring the last command you run then press enter there you go my code is not working properly anytime you type terraform apply the first entire form will do is show you the plan of the resources it will create in your aws account feel free to review the resources and once you are happy with everything come all the way down terraform is asking you this question do you want to perform these actions we will type yes and press enter there you go telform is now creating the vpc in our aws account and i'll give you some time to finish creating it another thing i also want to mention is when terraform is creating the vpc you can see here that terraform is using the module terraform has successfully created the vpc in my aws account the next thing i'm going to do is go into my aws account to verify that the vpc is there in your aws account type vpc in the search box and select vpc under services on the vpc dashboard we are going to select this drop down to filter by vpc and i'll select the jupyter vpc i just created once you're filtered by your vpc select vpcs here and there you go you can see the vpc i just created the name of my project is jupiter and it is joining it jupy.vpc this is how we enter the tag for this vpc you can see decider block here 10.0.0.0.16. let's review the subnets i'm going to select subnet in this vpc you can see that i have six subnets i'm going to sort this in order you can see i have public subnet easy one public subnet easy to i'm going to drag this out private app subnet easy one private app subnet easy to private data subnet easy one private data subnet easy to these are also decider blocks i specified in my terraform dot tfvas file the next thing we can review is the route tables select route tables you can see the public route table we created and we associate two subnets to that public route table if you select that route table on that route you can see we are routing traffic to the internet through the internet gateway and if we select subnet associations the public subnet is the one and public subnet ez2 is associated with this route table these are all the resources we entered in our terraform file and we have verified that terraform has created those resources in our aws account let's go back to our terraform file if you want to delete the vpc you just created in your aws account in the terminal type terraform destroy and it will delete the vpc you created in your aws account this is how you use telephone module to create a vpc the next thing we will do is push our code to the github repository so push your code to the github repository select source control here and in this box we are going to type a message i'll type created vpc file once you type your message click this check mark to commit it once you've committed your message click sync changes to push it to your github repository i'll click this button and there you go we have successfully pushed our code to the github repository if i go back to my github repository in my github account i'll select this icon to go to the home page and i'm going to select the telephone modules repository there you go in the telephone modules repository you can see all the files we created on our computer we have pushed it into this repository this is how you use terraform module to create a vpc if you have any questions or there's any part of this lecture you don't understand please leave your comments below thank you and i'll see you in the next lecture bye
Info
Channel: AOS Note
Views: 39,483
Rating: undefined out of 5
Keywords: what is terraform modules, terraform, terraform modules, hashicorp terraform, hashicorp, aws, devops, infrastructure as code, terraform modules tutorial, terraform modules examples, terraform modules demo, terraform cross module reference, terraform tutorial for beginners, terraform enterprise, aws ec2, terraform modules aws, terraform modules explained, terraform aws, terraform tutorial, modules, tutorial, create aws vpc with terraform, vpc, how to create a vpc using terraform
Id: ZP_vAbjfFMs
Channel Id: undefined
Length: 73min 36sec (4416 seconds)
Published: Tue Jul 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.