Data Sources | Terraform Tutorial | #10

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to be going over data sources and terraform so what are data sources basically data sources allow you to retrieve data from different resources essentially so basically all we've been doing so far is is sending data to to our like cloud provider like AWS and Azure or things like that but maybe we just want to retrieve the data so with the data sources it's kind of like an API so you're kind of like fetching fetching the data and return in it and then using that in your configuration so the easiest way to show that is to just just go go right in so previously we've been using let's let's take this as an example we have our V PC a subnet in an instance well we've been using the V PC as make like we've been creating a V PC every time but maybe we already have our V PC created and we don't want to actually like create a new V PC but we need that V PC data to be able to pass into our subnet to get that so what we can use is a data source and go and grab that data so let's do that now so you have the data keyword here that we always start with see the data and then you're gonna have your resource so basically every resource that we've been using like the resource name like AWS V PC AWS subnet those all have a data source as well so just as it has a resource it has just the data as well to just export that data so what we want to do is we want to get the ID of the V PC here so what we can do AWS V PC because that's the resource we want to grab and then we just need a name that we wanted to do which should just be fubar it doesn't have to reference this like so let's just get rid of this you know it could just be fubar it could be you know let's just do Tut's to talk like tutorial so we have that so we have the the resource name but we have a prefix with data here and then just the name here which is internal to terraform and then we have our block statement again inside of curly braces so there's a few different ways to to grab what what like VPC you want or what whatever resource you want and each resource is slightly different in each way but what one thing had a lot of resources habits you can filter it out by like say tags so let's say we want to grab I've got a V PC created in my account here that we can go look for so I'm V PC of name of Tut's here so let's let's try to grab this this V PC based on the name of Tut's so what we can do here is well let's let's show you what the data sources have so let's do terraform V PC data let's see it gives you different options that you can use so let's let's come here so here you can actually specify the ID if you know the ID so this V PC ID if you know that ID you can pass that directly to the data source here if you want so that's one way of retrieving the data you know but maybe you don't know maybe you don't know the ID maybe just know the name so let's there's different option options here so you can do it on the site or block default would be like the default V PC so if you set default equal to true so if we set default equal to true here this would be the default V PC in AWS which would be I believe in mine is gonna be this one for now so it would grab this one and let's actually let me just run this just to show you I'm gonna comment out this these resources down here but just to show you I want to own a log output this data here oh and the way to use this the output from a data source is it o is similar to how you do with output of a resource so you see how we have you know when we had the resource we did AWS VPC main ID well we do something similar with data data sources we just do data dot the resource data AWS VPC dot tuts and then that will give us all of the the data outside of the V PC if we wanted the ID we could do touch ID let's let's run this just to show you it does work terraform apply Auto probe perfect so it did work so now what we had to see the tags of will - V PC so we got will V PS v pc because this is the default V PC on my account right now so that's why it approved that's why I returned that and not that one so that's one way of grabbing the data so let's let's grab so what we want to do is I want to create an instance I want to use this this tuts V PC and use that V PC as our our subnet which then uses our instance so what we can do is let me leave that that output there so let's let's get this by the tag so we can do filter which is here and what we can do with the filter is [Music] grab it by but it by a name so we're gonna do like meme and then since it's a tag we can do tag colon and then the the tag itself so our in our case the tag for this is gonna be name so we can do name and then we have Vout of values key which is an array which could be multiple values but we want what we want is we want Tut's so that's that's one way of doing it so let's log this and make sure we get so right now you can see we have we'll VPC now when we run this we should see it should be the Tut's VPC if we did this correctly and each each resource is a little bit different so you can kind of look and see what they which resource what each resource does for the argument reference and the in the data block to see all the ways that you can actually search for the resource itself but you can see here this returned and now we have Tut's so now we are returning our our Tut's VPC perfect so let's so awesome so that's kind of how data sources work you know that's it very simple it's just kind of like an API call let's let's work through a couple more examples to kind of give you some more places where you might use this so we could use this on our our subnet now if we wanted to so now instead of it being AWS VPC main ID to set it we can do data data AWS be PC and then tuts dot ID so now when we create this configuration here we are we're not creating a PPC anymore we're just using an existing V PC and then we're creating a subnet and in an instance we would actually have to you subnet if you wanted to do you know we do subnet ID equals the AWS subnet here but went out when I can actually create this but you get the point on I know you can you know fetch the data another thing you might use you know day resource for it or another good example is for like the ami maybe so maybe you have a custom image that you you've built or your company bill or that you have for different you know different things and you want to use that that ami so maybe you eat that could you know you're a my could be passed in through the command line variable or maybe you want to fetch it so instead of hard-coding this here let's let's fetch an existing ami so let's get rid of this so I have I have an existing am I in my account here let's go to let's go to it so I just have one called Tatsu boo - so it's just a very basic who boots your images I have but let's let's go and fetch that and create a and use it to create the instance here actually let me I'm gonna I'm gonna keep this V PC here cuz well actually use that cuz I want to show you actually this working so let me actually do add that subnet subnet ID here hopefully all this works alright perfect so now let's instead of using this ami let's let's grab the ami that I have here so we can do again data and then you know what I usually do is I always just do a Google search you know terraform AWS ami data and usually it's the first result and we've got the data source of ami and you can see that this has more more options and our VPC to do different things i said you liked the argument here the domain the required argument is owners that we need in owners I'm gonna use self because it's gonna be the current account that we're using but if you needed to pass in your something else you can do AWS account ID and so forth but one thing you can do is you can do most recent so what most recent will do is it will just grab just your most recent ami so you can do that so let's let's do that let's grab the most recent for now let's do a double yes Am I because AWS am I here and then like let's just do main am I and then our block statement and let's do most recent trip and now that should give us that most recent am I so let's let's use this let's use this below so what we can do is instead of this we can do we can access this by data AWS ami which is the resource name and then whatever we called it main ami main am i dot ID and if you come again if you look at the argument o the attribute reference this is what is exported so we can use the ID here that's set and then there's a bunch more things that are exported so so now what we've got is we've got a configuration where we're using an ami that's already created and we're using a V PC that's already created and then we're creating the data we're using that data to create a subnet and our AWS instance so let's see what happens if we create this and everything should just work I probably have an error somewhere of course I do owner's okay yeah so owners like I said owners is required so I have to do on the AWS ami it's not required on AWS V PC so to each data source is a little bit different if I can spell ah self so that's that's our current AWS account now let's see what happens there's probably gonna be another error somewhere I pressure them name I prove - if I just done apply or plan but essentially you can see you know how we've used data sources and then there's there's there's other ways you can read through the data based on what resource what each resource says in their argument reference yeah so that's that's basically how data sources work they're very powerful you'll use them quite often depend on what your your infrastructure setup is but I think that's all we have for now don't forget to hit the subscribe button below and I will see you in the next video
Info
Channel: Will Brock
Views: 21,513
Rating: undefined out of 5
Keywords: Terraform, AWS, Azure, Linux, Cloud, Cloud Provider, Infrastructure as Code, Servers, CloudFormation, ARM, VM, Virtual Machine, Networking, Data Sources
Id: 9cDDZzl7zow
Channel Id: undefined
Length: 14min 21sec (861 seconds)
Published: Wed Jun 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.