How to Create a VPC Using Terraform

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to another lecture in this lecture we are going to create our vpc for this project to create our vpc we are going to use this reference architecture we are going to create our vpc with public and private subnets in two different availability zones we will use all the information in this reference architecture to create our vpc we are going to use the same cider block for the vpc and the subnets and we're also going to use the name you see here to tag our subnets we'll create a route table we'll call that route table public route table and we are going to associate the public subnet 1 and public subnet 2 to that route table for our private subnets for now they are going to be associated with the main route table and we also create an internet gateway and attach the internet gateway to our vpc we are going to use all the information in this reference architecture to create our vpc to start let's open visual studio code i'm going to minimize this in visual studio code open your project folder i'm going to select file i'll select open folder my folder is on the desktop i'll select the terraform project and in that folder i'm going to select my e-commerce website folder and i'm going to click select folder and that should open your project folder i'm just going to close this in the e-commerce website folder you can see all the files we created in the last lecture if i select the provider.tf file and you can see it here the next thing we are going to do is create a file for our vpc so in this space here right click and select new file and name that file vpc.tf once you have created your vpc file the next thing we will do is enter our syntax before you can create any resource in aws using terraform i recommend that you know how to create that resource using the management console and the reason why i recommend that you know how to create that resource in the management console is that for example the same step we are going to use to create vpc in the management console is the same step that we are going to use to write our syntax for our terraform file to help you understand the steps that we will use to create our vpc i have created a reference template for you please download the reference template in the description and open it to follow along i'm going to browse to the location where i saved it on my computer and the reference template is called vpcreference.tf i'm going to double click it to open it in this reference template i have include all the resource type and arguments that we need to create our vpc another thing i did is i added notes for you and the notes will explain the type of resource that each resource block is creating for example this resource block is creating a vpc and the second notes that i have up here is the keyword you can use to search for this resource type in google so all you have to do is copy this information or copy it and i'm going to open google and in the google search box i'm going to paste it in there and press enter under your search results if you select the first one it is going to take you to the exact location in the terraform documentation that shows you how to enter the syntax for the resource you are trying to create for example i'm trying to create my vpc force and this is the terraform syntax to create a vpc there are also other arguments down here that we can add to the configuration of our vpc i have added an extra argument down here so any information you see on my reference template comes from the terraform documentation and like i said to know where that information comes from in the terraform documentation all you have to do is copy this second note in my terraform reference file if you copy it come to google and i'll open another search box again just to show you i'll paste it in there press enter under the search results select the first option and it is going to take you to the exact location in the documentation so to make things fast that is why i have created this reference template for you plus in most cases we are going to add extra arguments that are not in the default documentation syntax these are the steps to create our vpc first we are going to create our vpc then we'll create an internet gateway and attach that internet giveaway to the vpc we'll create our public subnet one will create our public subnet tool will create a route table and add a public route we will associate public subnet 1 to the route table we created and we will also associate public subnet 2 to the route table then the next thing we are going to do is create our privacy subnet one two three and four so that is pretty much all the steps we need to create our vpc to create our vpc the first thing we are going to do is create the vpc so what you can do is you can copy this resource block to create a vpc here i'll copy come back to your vpc template and paste it in there once you have pasted we are going to enter the value for our arguments the first argument is our cider block and we can enter our cider block after this equal sign according to our reference architecture decider block for our vpc is 10.0.0.0.16 and we can enter that value in here but one thing i want to teach you is there are certain values that we are not going to add code into our file so what i mean by that is we want to add some flexibility here where we can change this cider block if we have to create another vpc that uses a different cider block to add that flexibility into our file what we are going to do is we are going to create a variable file so to create a variable file come in this space again right click in there and select new file we are going to call this file variable.tf and once you type it click out and there you go we have created our variable.tf file you can see it here i'm going to select it and in this variable.tf file you can see i'm in the variable.tf file this is where we are going to enter all of our variables the first variable we are going to enter is the variable for our vpc slider to create your variable you will type it like this first you will start by typing variable and you can use your autocomplete to select the variable here i'll select this and it should create something like this for you the next thing we are going to do is on that name we are going to give our variable a name i'll call it vpc cider and once you've entered the name for your variable you can come down here and the first argument we are going to add is default so type default and once you type default press the spacebar and press the equal sign and space again and we are going to enter the value for our vpc in double quotes so i type double quotes and i'm going to type tender 0 0 0 16 and the next thing we are going to do is we can press enter again right on that default we can add another argument and we will call that argument description and what i did here is when you start typing it and you see that autocomplete suggestion press enter and it is going to complete this for you and all you have to do is enter the value for the value we are going to enter vpc cider block and once you've entered it press enter again and right under that description the next argument we are going to enter is type and you see when i'm typing type the auto completion is showing here i'll press enter to accept it and for type it is going to be a string so enter string in here this is how you create a variable in terraform we created a variable we call it vpc sider and we give it a default value of 10.0.0.0.16. so now in our vpc template when we come to this template on that cider block we want to reference this variable we just created here to reference a variable in your terraform file i'll come back to that vpc again and in here i'm going to enter it like this you'll start with double quotes then a dollar sign then you're going to add those two brackets and in that bracket you are going to type var dot the name of the variable we called it vpc sider so you can see vpc sider if i go back to that variable that is what we called it vpc sider so that is pretty much how you reference a variable the next argument we are going to enter is the instance tenancy and for that we are going to enter a value of default so again double quotes and in the double quote type default and for any of this argument if you don't know what it means all you have to do is come back to the documentation and in the documentation for the vpc resource type if you scroll down under instance tenancy you can read about it here and some of the values it takes is default dedicated or host for our example we are using the default so i'm going to minimize this the next argument we are going to enter is enable dns hostnames and for this we can enter a value of true by making the value true any instance we create in our vpc will have a dns name the next thing we need to enter is the tags we're going to enter a tag name and we are going to enter a value here i'll call my value test vpc and that is all we need to do to create our vpc resource the next step is to create our internet gateway again i'll come back to my reference template and this is the syntax to create an internet gateway i'll copy it and i'll come back to my vpc template i'll press enter and i'll paste it here and for our internet gateway resource i have created a local name just like for the vpc i created a local name and all we need to enter is the value of the vpc id to reference the vpc id which is this vpc up here we are going to enter it like this all you have to do is take this two value copy them you can copy this to value and under the argument for the vpc id we are going to paste it in here and all you can do is remove those quotes add a period between those two and come to the end of the vpc add a period and enter id and that is how you reference your vpc id to reference the vpc id we are going to enter our vpc resource type period our vpc local name and the attribute which is the id the next argument we are going to enter for our internet gateway is the tag again we are going to give it a name and the value we are going to call it test igw so type double quote and type test igw or give it any tag name you want the next resource we are going to create is our public subnet one again i'll come back to my reference file and i will copy this resource block for public subnet 1. copy it come back to your vpc file press enter press enter again and paste it down there for the public subnet one resource again you can see the resource type and i have given it a local name we are going to use this argument to create our public subnet one and we can enter the values for those arguments here the first argument is our vpc id it is pretty much the same as this up here so you can copy this copy this and paste it down here the next argument we are going to enter is our cider block for our public subnet one just like we did for the vpc we are not going to hardcode that cider block in here according to our reference architecture our public subnet one will have a cider block of 10.0.0.0.24 so what we are going to do is we are going to create a variable for it so i'll come to my variable file and i'm going to select here press enter press enter again and all you can do is copy this variable copy and come down here paste it here and all you have to do is modify this first we are going to change the variable name and we can call it public subnet one sider once we enter our variable name we are going to change our default value to 10.0.0.0.24 so this should be 24 and we are going to change our description to public subnet one cider block so here i'll just type public so next one and that is all we need to do to modify this remember change your variable name and change the default value for your sider block and change your description once you've create your variable come back to your vpc file and i'm going to scroll down and for our public subnet one slider block we are going to reference that variable we just created so again to do that you'll start with double quotes dollar sign then the bracket and type var dot the name of the variable we called it public subnet one sider or you can come back to your variable copy it here so that way we don't misspell it come back to your vpc and paste it there and that is all you need to do to reference it the next argument we are going to enter the value for is our availability zone and for our availability zone for now we are going to hard code it in here and later on in the lecture we will look at the different ways we can make it dynamic so for now let's just hard code it in here according to my reference architecture i want to put my public subnet one in the first availability zone and in the region that i'm currently in since i'm operating in the uss1 region my first availability zone in that region would be uss1a so in here i'm going to type uss1a and the next argument we are going to enter the value for is map public ip on launch and we are going to enter a value of true so what this means is that anytime we launch an ec2 instance into this subnet that ec2 instance will be assigned a public ipv4 address the next argument we are going to enter is the tag for the tag we are going to enter a name and for the value we are going to enter public subnet one the next resource we are going to create is our public subnet too again i'll come back to my vpc reference and i'll scroll down i'm going to copy the resource block for the public subnet too copy it come back to your vpc file press enter press enter and paste it under again we are going to enter the value the vpc id we can copy this for our public subnet to cider block according to our reference architecture it is 10.0.1.0.24 so what we are going to do is create a variable for it come to your variable file all we have to do is copy this press enter press enter again paste it there we are going to change our variable name to public subnet to cider and we are going to change the default value to 10.0.1.0.24 and the description will be public subnet to cider block that's all we need to do here then come back to your vpc file for the slider block all we can do is we can copy this paste it down here and just change this to two and again we'll enter it with the quote dollar sign var the name of our variable if you look we called our variable public subnet two sider and that is what we have in here for the availability zone argument we want to put our public subnet to in the second availability zone according to our reference architecture you can see the public subnet 2 in the second availability zone so i'll minimize this and for that one we can copy this paste it here and the second availability zone will be uss1b so for this availability zone you can select any availability zone you want as your first and second but just make sure you are consistent as we build the rest of the subnets so if your first availability zone was uss1c up here and your next availability zone was uss1b you have to be consistent for where you put the subnet in the first availability zone and in the second availability zone so i'm going to minimize this the next thing we are going to enter is map public ip on launch because this is a public subnet it is going to be true so this means that any ec2 instance will launch in this subnet will be assigned a public ipv4 address the next argument we are going to enter is our tags we gave it a name and the value is going to be public subnet too let's look at our reference architecture the next resource we are going to create is our route table and we are going to add a public route to it so i'm going to minimize this come to your reference file and select this resource block to create the route table and add public route i'll copy come back to your vpc template press enter press enter again paste it down there i'm just going to scroll up the first argument we are going to enter is our vpc id so can copy it up here and paste it there so i am copying and pasting it there for consistency the next argument you are going to enter is your route and again this will make more sense if you already know how to create a vpc in the management console and if you don't know what any of these arguments means like i said this second note if you copy it and open google in google paste it in the search box press enter and under your search results select the first option it is going to take you to the exact location of that syntax in the documentation for the route table this is the exact same text we are using but we excluded the ipv6 routes because we don't need that we are only routing our traffic on ipv4 so i'm going to minimize this under the route the cider block is going to be anywhere and this is how you type it you type double quotes and anywhere is going to be 0.000 and for the internet gateway id remember we created our internet gateway up here this is our internet gateway again to get the id of any resource all you have to do is copy the resource type and the local name you give it copy it i'll come down here under my internet gateway id paste it there and all we have to do is modify this i'm going to remove those quotes and add a period between them and come all the way to the end again add another period and enter the id attributes and that is how you specify the internet gateway id it is the resource type name dot the local name of that resource dot the id attribute the next argument we are going to enter is the tag we'll give it a name and we'll give it a value of public route table now that we have created our route table the next resource we are going to create is we are going to associate this public route table to our public subnet 1 and public subnet 2. according to our reference architecture you can see that our public route table is associated with our public subnet 1 and public subnet 2. let's go back to our terraform file and come back to your vpc reference file in my reference file i'm going to copy the next resource copy come back to your vpc file press enter press enter again and paste it in there for every resource that i've created a reference for you please remember that it has a resource type and i also give it a local name for this local name i call the public subnet one route table association so for every resource you create you will have a resource type which is defined by terraform and the local name which is defined by you for this resource type the first argument that we have to enter is our subnet id the first subnet we want to associate with this route table is the public subnet one so what we are going to do is under public subnet one again copy the resource type and the local name copy and we are going to come down here paste it here and remove the quotes between them add a period and come all the way to the end and add a period again and the attribute we want to reference is the id so enter id and again for everything we are entering here you can find them in the terraform documentation for example the id is an attribute if i copy this notes here i'm going to copy and open google and i'm going to paste it in here press enter under your search results select the first option i'm going to scroll down you can see on that attribute reference the id so that is what we are referencing we have arguments and these are the different arguments you can use under the resource block of your resource type you are creating and you also have attributes in which you can reference so that is what we are doing here we have the arguments and for the value of the subnet id it wants the attribute of our public subnet one which is the id for our route table id we are going to do the same thing our table is up here we are going to copy the resource type and the local name come down here paste it here then we are going to remove the quotes between them and add a period then come all the way to the end again add another period and enter id so we are referencing the id of our route table that is what we need to do to associate public subnet 1 to our route table the next resource we need to create is to associate our public subnet to to the route table again come back to the vpc reference file and you can copy the second one i'll copy this associate public subnet2 to public route table copy come back to your vpc file press enter press enter again and i'm going to paste it here the first argument we are going to enter is our subnet id this time we want to associate public subnet 2 to our route table so we are going to enter the id of our public subnet 2. so scroll up to public subnet to it is right here copy the resource type and the local name copy come back down here paste it there remove the quotation between them and add a period and come to the end and add another period then add id that's all you need to do to reference it for the route table id argument we can just copy this up here because it is the same route table copy and paste it down here and that is all we need to do to associate our public subnet 1 and public subnet 2 to our route table the next thing we need to do is create our private subnets according to our reference architecture we have four private subnets and we want to associate those private subnet with the main route table i'm going to minimize this and come to the vpc reference file copy the resource block for private subnet one i'm going to copy come back to your vpc file press enter press enter again and paste it there for the vpc id we already know what that looks like so we are going to come up here we can copy it here and paste it there beside that blog we are going to create a variable for it so go back to your variable file we can copy this variable press enter press enter again and paste it down here and all we have to do is modify it for the variable name we are going to call it private subnet one so change this to private and it is going to be subnet one for the default value according to our reference architecture our private subnet one has a cider block of 10.0.2.0 so in here we are going to change this to 2 so there is 10.0.2.0.24 and for our description we can call it private subnet one and that is all we need to do to create our variable then come back to your vpc file or decide that block let's reference that variable we just created to reference your variable remember you start with a double quote and a dollar sign and the brackets and you type var period and the name of that variable so the variable we called a private subnet one so let's copy it come back here and paste it in there that is all we need to do to reference our variable and for our availability zone argument we are going to put our private subnet 1 in the first availability zone so if i look at our reference architecture our private subnet one is in the same availability zone as the public subnet one so all you have to do is go to your public subnet one which is here copy that availability zone value and come down here and paste it there so the reason why i am copy pasting is just to make sure that everything is consistent and i'm not typing things wrong so in areas where you can copy paste make sure you are doing that and for map ip this is going to be false and the reason why it is false is because this is a private subnet and we don't want to attach any public ipv4 address to any instance in a private subnet so that's why this is false the next argument we are going to enter is our tags again we'll give it a name and a value we'll call it private subnet one apt and that is all we need to do to create our resource for our private subnet one let's create the next resource which is our private subnet too come back to the reference file and i'm going to copy that resource block for private subnet 2. come to your vpc file press enter press enter again paste it down there and for our vpc id we can copy this for our private subnet to cider block again we are going to create a variable for it and according to our reference architecture our private subnet 2 has a cider block of 10.0.3.0.24 so all you have to do is copy this variable come down and paste it there and all we have to do is modify it this is going to be private subnet 2 and the cider block is going to be 10.0.3.0.24 and for the description we are going to call it private subnet to cider block that's all we need to do to create our variable come back to your vpc file we are going to reference that variable we create so to reference it i can just copy this i'll copy this and paste it here and i'm going to change this to two so remember the variable name is private subnet two if i go back to that variable you can see it here private subnet too the next argument we are going to enter is our availability zone and according to our reference architecture our private subnet 2 is in the same availability zone as our public subnet 2. so in your file all we have to do is let's go to public subnet 2. under the public subnet 2 resource let's copy that availability zone and we are going to paste it down here the next argument we are going to enter is map public ip on launch again it is going to be false because this is the private subnet the next argument we are going to enter is our tag we've given it a name and the value is going to be private subnet 2 up tier to create our private subnet 3 again i'll come back to the vpc reference file i'm going to copy that resource block for private subnet 3. come back to the vpc file press enter press enter again paste it in there the vpc id we are going to copy this and paste it here for decider block argument we are going to create a variable for it so let's copy this variable i'm going to press enter press enter again paste it there for the variable name it is going to be private subnet 3 cider block and the default value for our private subnet 3 is going to be 10.0.4.0.24 so here we are going to change that to 4 so it is 10.0.4.0.24 and for our description we are going to call this private subnet three-sided block and that is all we need to do to create our variable let's come back to our vpc file on that slider block again we can just use this to reference it copy this paste it down here and modify this to three for availability zone let's look at our reference architecture our private subnet 3 is in the same availability zone as private subnet 1 and public subnet 1. so all we need to do is for our public subnet 1 or private subnet 1 resource we are going to copy that availability zone copy and come down and paste it here and for the argument map public ip on launch it is going to be false and the next argument is our tags we have given it a name and we are going to give it a value of private subnet 3 database tier the last resource we are going to create is our private subnet for come to the vpc reference file copy your private subnet for resource come back to your vpc file press enter press enter again and paste it down there for our vpc id we are going to copy this and paste it there for decider block we are going to create a variable for it so select your variable file and copy this variable press enter press enter again paste it there then i'm going to modify the variable name to private subnet 4 slider but the default value for our private subnet 4 slider according to our reference architecture it is 10.0.5.0.24 we are going to change this value to 10.0.5.0.24 and for the description it is going to be private subnet for so we have privacy subnet for cider block that's all we need to do for our variable come back to your vpc file for your cider block argument copy this and paste it here remember to change it to private subnet for citer because that is the name we gave the variable the next argument we are going to enter is our availability zone and again according to our reference architecture our private subnet 4 is in the same availability zone as our private subnet 2 and public subnet 2. so you can go to your privacy subnet 2 which is right here copy that availability zone and we are going to paste it here for the map public ip on launch it is going to be false and the next argument we are going to enter is our tags we have given it a name and the value is going to be private subnet for database chair this is all the resource we need to create our vpc in this reference architecture so once you've created all your resource all you have to do is come to file and select save all another thing i want to mention is make sure that your vpc reference file that i gave you is not listed here this is just a reference file for us to follow along the files you should have in here should be for now the provider.tf variable.tf and vpc.tf and also the folder and the files for your plugin so this vpc dash reference file should not be in this folder we are done with this vpc reference file now so we can just close it our file is ready and we can create this vpc in our aws account the next thing i want you to do is open your aws account i've logged in my management console select services and come down to networking and content delivery and select vpc you can see under my vpc dashboard that i currently have one vpc and if i look in it it is my default vpc now let's go back to our visual studio code the next thing we are going to do is open the terminal so select terminal and open a new terminal in the terminal the next command we are going to run is called terraform plan so type terraform plan here and press enter and there you go once you type terraform plan and press enter the terraform plan will show you the list of things that terraform will create in your account so down here you can see the plan and it is telling you that it is going to add 11 new resources it is not going to change any resource because the number is zero and it is not going to remove any resource because that number is zero and if you want to know the resource that terraform will add you can scroll up here you see all this resource with green the aws vpc it is going to create a vpc if you scroll up it is going to create a subnet and this is the public subnet 2. it is going to create the public subnet 1 and so on so you can review this plan before you actually create it in your environment this plan is useful for you to know what is going to happen before you actually implement it so i'm going to go back down the next terraform command we are going to run is called terraform apply so type terraform apply here and once you type terraform apply and you press enter it is going to create our vpc in our account so i'm going to press enter and again it is showing me the plan and letting me know that it is going to create a level resource in my account and it is asking me do i want to perform these actions and i'm going to type yes in here type yes and press enter and terraform will start creating the resource in my aws environment so you can see now it is creating my vpc and there you go it is creating all the resource in my aws account again if i go back to the management console and in the management console if i refresh and you can see that i have two vpc the second vpc was created by terraform because in our telephone file we gave it a tag of test vpc i'm going to go back to visual studio code and wait for terraform to finish creating all the resources in my account then we are going to come here and review all those resources and there you go terraform has finished creating all the resources we listed in this file in my aws account and you can see it created 11 resources and it didn't change anything or destroy anything by destroy they mean it didn't remove anything so let's go to our aws account again to review our vpc to make sure we set up everything correctly i'm going to go back to my aws account in our management console you can see we have two vpcs the test vpc is the vpc that was created by terraform the next thing we can review is if you select the vpc you can see that our dns host name is enabled because that is what we specify in our terraform file and you can also see that our cider block is 10.0.0.0.16. the next thing we can review is our subnets i'm going to select subnets and i'm going to filter this by my test vpc so select in here and select your vpc now i can only see the subnet in that vpc i'm going to expand this you can see that i have private subnet 1 private subnet 2 private subnet 3 privacy subnet 4 public subnet 1 and public subnet 2. let's review public subnet 1 select that you can see my public subnet 1 is in the uss1a and let me review the public subnet 2. public subnet 2 is in the uss1b so make sure you review all your subnet to make sure they are in the right availability zone the next thing we can review is our route table i'm going to select route tables and again i'm going to filter this by my test vpc and you should only have two route tables in here the first route table is your main route table and this route table was created when the vpc is created the second route table which we tagged public route table is the route table we created in terraform and this route table we have succeeded two subnets to it so if i select our route table first let's review the route i'm going to move this up and you can see we have a destination of 0.0.0.0 through our internet gateway and let's check our subnet associations select the subnet associations you can see that our public subnet 1 and public subnet 2 are the only two subnets associated with this route table and according to our reference architecture we want our private subnet to be associated with the main route table so if we select this first route table which is the main route table and we come to subnet associations and i'm going to scroll down you can see that four subnets are associated with this mineral table and what a main route table does is anytime you have subnets in your account that are not explicitly associated with any route table those subnets will be associated with the main route table so everything looks good what about our internet gateway and again let's filter it by our test vpc and you can see our internet gateway that we call test igw is assigned to our test vpc so we've configured everything correctly in terraform and we can verify those configuration in our management console let's go back to visual studio code and now we want to remove all those resources from our account to remove all the resources terraform just created in our aws account in the terminal we are going to type terraform destroy once you type terraform destroy press enter again it is going to give you a plan and it is going to let you know what it is going to do you can see it is going to destroy our vpc meaning that it is going to remove it it is going to remove the public subnet too it is going to remove the public subnet 1 and so on so once you've reviewed the plan you can come back here and it is letting you know the plan here it is not going to add any resource or change any resource but terraform is going to remove 11 resource and it is asking you do you want to proceed we are going to type yes and press enter and terraform will remove all the resources from our ews account let's give it some time and there you go terraform has successfully removed the 11 resources it created in our aws account this is how to create the vpc for this project using terraform in the next lecture we are going to be creating our terraform file for the night gateway thank you and i'll see you in the next lecture bye
Info
Channel: AOS Notes
Views: 4,373
Rating: undefined out of 5
Keywords: awsnotes, How to Create a VPC Using Terraform, terraform, vpc, aws, terraform aws, devops, terraform tutorial, infrastructure as code, create aws vpc with terraform, terraform aws tutorial, terraform tutorial for beginners, terraform for beginners, automation, ec2, hashicorp, terraform aws vpc with public and private subnets, terraform modules, terraform aws provider, how to use terraform, subnet, tutorial, docker, terraform state, iac, terraform aws example, aws tutorial, aws vpc tutorial
Id: _CdTR0LpSLw
Channel Id: undefined
Length: 54min 5sec (3245 seconds)
Published: Tue May 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.