Resource Exports & Dependencies | Terraform Tutorial | #5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back in this video we're going to create a V PC a subnet and a instance so in the previous videos we've just created a AWS instance and by default that just attaches to our default V PC but what we want to do is we want to create our own a new V PC and subnet and attach this instance to that V PC and subnet and what we'll do is we're gonna show you how to you know use output from one one resource into another so let's get started we are going to keep we'll keep our regular instable us and since here so the first thing what we want to do is we want to create a database V PC so what do we need to do to figure out how terraform handles the V PC well what I usually do is I just Google you know terraform AWS V PC easily the top hit is what I need and so I kind of go I go here and I kind of see what what what data do I need to pass this this resource so I can see it's it's called AWS underscore V PC and it accepts a setter block and here's so here's a basic usage so we'll create it and we're just gonna supply it a setter block so what we want to do is we need a WS underscore V PC so again resource and then AWS V PC and then the name can be anything you want all right so we've got a resource here a resource name in our attribute alright now that actually a better resource but what we call a name here and again this main here does not do anything to the actual infrastructure at all it's just only for terraforming purposes all right so let's add a setter block and let's see it's just a string so let's add 10/16 so let's add that so good so that's that's basically all we need for our basic VPC and we could add a tag but we won't do that I might as well SSI attack it's good practice to name everything so let's add tag do tuts VPC perfect we got that there I'm just changing the indentation it doesn't matter too much alright perfect so we got that so that's all we need for our VPC so next thing we want to do let's create a subnet so how do we figure out what this identifier is here let's see let's go to let's let's Google terraform AWS subnet and usually it's always the first first one so you can see it's AWS underscore subnet perfect so what do we need so here's basic usage so we can we just mainly need the VPC ID and the site or block that we want all right so the VPC ID what that is is that's gonna be the VP the ID of of this resource so how do we get that no so what we have to do is like we have to get the ID from this resource but this resource isn't crazy yet what happens is all the resources from terraform they output a bunch of different attributes so if we go back to our VPC resource here if you go down to the bottom of the page you see attribute reference you can see the all of the all of this data is exported from that resource so we have access to a bunch of different different data so what this is we need the VP CID for the subnet what that is referring to is this ID of the resource that we created all right so so the way to do that so let's let's let's start creating our subnet so resource AWS subnet we can name it you know let's just name it web web or something it doesn't matter you can name it whatever you want so far we got that so then we need the VPC ID so how do we access the VPC ID all right so the way we're able to access the VPC ID from here is we can take we can get it by calling AWS VPC which is the resource so AWS VPC which is this dot the resource name so AWS VPC dot main so main is the resource name and then the attribute itself which is ID so here we're getting the AWS resource domain the resource name and then the exported attribute from this which is ID which comes back to this so the ID so we could do if we if we needed like the cipher block instead of ID we could do CIDR block you know we could do a RN if we want if we needed to a RN but we need the ID so that's basically that's how you get access data from a different resource per so we have so we have this created perfect and then what else do we need on the subnet and we just need the site or block as well so let's let's add that and it's gonna be a string so let's just do 10 v dot 0 dot 0 16 it could be anything we'll do that and then we'll just we'll add tags again name of you know we'll just name it web subnet I'm not sure why the linters giving me an error here but that's that's correct don't worry about that I don't know why it's giving me I went there that's not a big deal I just started using a different extension so ignore that that's it's fine does he know that error for that warning alright so I got this we got name so so we got our VP CID which reference is this so perfect so now what we want to do is we want to attach our AWS instance to this this this V PC well the way we do that is we can attach the subnet to the instance so let's go to terraform AWS instance and if we look there should be ace I believe there's a subnet ID submit' ID the VP see something a diesel engine so we're gonna launch this instance into the subnet and again we need the output of this subnet here so what we can do is something that ID and now we can take AWS subnet dot the resource name so this is gonna be the resource and now the resource name here web and then dot it's gonna be I'm assuming it to be ID so if you look on you know just how we X how we had exported things from AWS we can look at the exported attributes from the subnet so the subnet only has four so it has ID AR n owner ID and ipv6 Ida block Association so that's what we have for that so perfect so so now we have we basically have everything that we need to create our aid abuse incidents inside of a subnet and AV PC so let's just give it another scan we've got everything here so everything looks good so let's let's see what happens if we do terraform apply and we get everything so now I should just nothing's created yet so it's just gonna create everything so let's just do yes and this should just take a minute to create alright so that's added these three added so let's just go to our interface and make sure everything did get added so let's refresh so now we have an instance let's go to our V PC and now we have a VPC of Tut's Tut's PPC which is this and let's go to a subnet which we should have web subnet so perfect so I've got that so so now we've learned how to basically take data from a existing resource or a resource that's already been there and it's pretty simple you know so another thing you might notice is we we went in order so we first in the code so we first specified our V PC then our subnet and then our instance well one awesome thing with terraform is essentially to be able to use this V PC ID in the subnet this resource here the V PC has to be created first so that could get confusing and complicated but terraform takes all of that confusion out of the way so when when terraform before it runs it actually parses the file and it can determine that the it looks for for things like this it looks to where you call other resources and it determines what needs to be ran first so terraform has parsed this file this whole file and it sees this it sees the call to the V PC ID so it knows that this V PC has to be created first before it can create this resource and then it also sees here it sees the use of the subnet block here so it knows that this subnet has to be created before this instance which is cool so so you can actually like define so we have the resource subnet here after V PC we can actually we can specify in the code this anywhere so now we're calling the subnet before the V PC and that's that's perfectly fine it doesn't matter it doesn't matter where you call it because when terraform basically parses this file it knows or automatically what what to run first so the order that you specify things in isn't it doesn't matter to terraform at least so they can be in any order I would suggest though just for readability purposes to kind of specify them in the order that you need them or that it makes sense so it kind of it you know it just makes sense to create a V PC first and then a subnet and then the instance you know but it might not always work out that way but for the most part when you design the code you know do it for what you would naturally create in the interface first so if you're gonna create the V PC first in the interface you know put that on top of your code and then the subnet and then your insist but terraform knows all about the dependencies of everything so essentially the subnet is a dependency of the V PC and then the instance is a subnet or a dependency of the subnet all right so that's everything that I have for for this video make sure you hit the subscribe button below and I will see you in the next video
Info
Channel: Will Brock
Views: 11,369
Rating: undefined out of 5
Keywords: Terraform, AWS, Azure, Cloud, CloudComputing, Infrastructure, Linux, Server
Id: TOnfkKxvOyw
Channel Id: undefined
Length: 13min 36sec (816 seconds)
Published: Wed Jun 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.