Creating a VPC with Public and Private Subnets in AWS Using Terraform

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we'll be going through AWS and terraform our goal is to deploy VPC in AWS using terraform when with one private subnet and one public one so let's hop into the console and work on [Music] that all right so I'm already logged into to our AWS account and let's firstly navigate to VPC we will check how our current infrastructure looks like so we have one VPC deployed a default one with a cider of 172 and few subnets are public and private ones in different availabity zones so let's hop into Ubuntu Server which we installed before if you weren't aware of this particular hands Zone I will link it down below and the second thing let's go to our terraform which we already instantiated before I will link it also down below so here we have our terraform code so we have our providers. TF which has AWS provider and AWS region set to us East one and we have our M TF which only consists of an S3 bucket which we created in previous videos so right now we would like to go to this main TF I will use Nano and we would like to create a VPC there is quite a lot of code to create VPC subnets s Gateway internet gateway and so on and so on so I already prepared that for you and it will be a part of our today's blog post on briefly. dev so let's just copy all of that and we will go through that that will make more sense than you're waiting for me to type all of that so let's paste it here and let's go up to the beginning and see what we will be creating so first of for we will do a resource AWS VPC we will call it a main VPC and we will take a cider block of 10.0.0.0 remember that the actual cider block which is deployed in this account is 172 31.0 do0 so then in this VPC we will create a subnet we will use resource AWS subnet and we will create a public one VPC ID we will take out of it so we will do uh connection to AWS VPC resource. main VPC so to this one and do ID this is what we are taking and we are giving a CER block of our public subnet to 10.0 1.0 sl24 and map public IP on Lounge this is this is to add IP addresses public IP addresses to this subnet and there is a tag so we will have a name of public subnet which will be shown here another thing private subnet so private subnet looks the same but we will not map the public IPS and The Cider will be 10.0.2 another thing which we need to which we will create create is an internet gateway so for the public subnet which we want to create here to have access to the internet we need to create an internet gateway which will allow for that so we will create a resource of AWS internet gateway and call it main egw another thing we need to do is to create a route table so route table for public one so we will make it exposed and open to the internet fully public one so to all IPS in the internet we want to Route them through internet gateway so it will have connection to outside and from the internet to the Public Public route then to attach it we need to do something called table associ Association so we will take this newly created route table AWS WR table resource. public RT so we are referencing this do ID and we are assigning this route table to the public subnet which we created so AWS subnet resource. public subnet doid and that will create this connection of Route table to subnet ID another thing this will be already a part of uh private Subnet in the future so we want to create something called AWS not Gateway we will use it to give internet access from our private subnets so if we would let's say deploy E2 in a private subnet it would have access to Internet let's say for a connection to some kind of external database or let's say doing even updates or whatever but it will have a private IP so from outside the private subnet you won't be able to access this instance so we are creating AWS nut Gateway calling it main nut and we are making an allocation of elastic IP which we are creating here so we will create ourselves an elastic IP to which we will attach to this nut Gateway so that it has access to the internet and we will make a correlation to the public subnet so it will take the internet from internet gateway attached to this public subnet and here we have a close dep pend zone so this elastic IP needs to be created first because in order of operations we cannot create nut Gateway if we don't have a elastic IP created yet so depends on is used for terraform to make some kind of a chain of operations so this tells terraform that hey you need to First create our ourselves an elastic IP and then you can go to create not Gateway because you depend on this elastic IP being created so then we have another route table which will be attached to our private route so we will also do the 0.0.0.0 sl0 so access to the internet but it will go through not Gateway not the internet gateway and then we are giving it a name so that we can easily check that in the console and the last thing we will do is another route table Association so to private subnet we will attach the route table of private at RT which we are creating right here so let's write all of that let's do terraform fmt to format the code if anything is needs to be formatted like the indents and so on and then let's do terraform apply we only need to wait a moment for the states to check as you can see it's checking this bucket okay let's go let's go from the top so we have our plan here AWS S3 bucket was checked the state is okay so we won't be creating that but here are all of our resources so we are creating elastic IP as we said we are creating not Gateway we are creating IP internet gateway we are creating creating private route table and P public right table we are creating the associations and we are creating private and public subnet and we are creating VPC so we will be totally added adding 10 elements so let's do here yes I want to approve that and let's wait for it to finish if it would take quite some time I will speed up the video all right and we are back it took like two or three minutes to deploy and let's go to AWS and go to our vpcs and let's refresh that and as you can see our main VPC with cider of 10.0.0.0 was deployed let's go to subnets Let's refresh that and we have our new two new subnets which we called public subnet and private subnet let's go to Route tables we have a private route table and public route table we can see here that we are in private right table and all the traffic to the internet is being routed through not Gateway which we created and in our public route table all the traffic is going to internet Gateway and we will go to elastic IPS and this will be this one elastic IP which we created and Associated it with the nut Gateway we created here so that's the basic idea behind deploying VPC with private subnet public subnet internet gateway not Gateway public routes table routes next time we will use all of these data to utilize more of the AWS resources through terraform so we will be able to take ec2 and deploy it in this VPC and this in the particular public or private subnet and then we will check if everything works if we have the connectivity to internet or outside of the uh VPC itself so in public subnet can we access the easy to instance from the internet itself so see you next time there is a lot of more to learn in AWS and terraform see you
Info
Channel: DEVelopment Briefly
Views: 10
Rating: undefined out of 5
Keywords:
Id: dZTf5v50Ess
Channel Id: undefined
Length: 11min 40sec (700 seconds)
Published: Tue Jul 02 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.