Hi and invis, listen, you're going to learn about data sources, data sources are a way TerraForm
can query NWS and return a result.
Essentially, what data sources do is you make an API request to get information.
That's all they do.
They don't actually set anything up.
There are lots of uses for these.
Might be getting the latest.
They are my maybe querying your infrastructure.
So we're going to set up a quick demo to show you how they work.
First things first we're going to need to do is go back into our dependency folder and redeploy our
easy two instances.
Now, when she would come on, it's very useful.
TerraForm, apply, dash, auto dash.
The proof could be a bit careful using this because whereas before we had to type in, yes.
This will automatically go ahead and deploy it because we know what we've set up.
It's fine to go ahead.
Awesome.
But took a while in my case, but now we've got both instances started so we can do is go to our code
and set up a data source to use data.
It's just like a resource.
So we're gonna type in data and the resource type.
In this case is eight W.S. underscored instance.
And what this is going to do is make an API request to Amazon to find out some details.
I'm going to include a link to the documentation because there are so many different options you can
use.
It's incredible.
I've for this example, we could do something really basic.
What we're can to do is query A.W. s for any instance, has the tag of DB server incredibly basic.
But like I said, there are so many different options you could use.
You could use it for Ádám eyes, backups, users.
I mean, it's just endless what you could use it for.
And in fact is we seen you can plug in details.
You might want to, for example, find out a subnet I.D. and then plug that in.
You could use data to get those details.
So now let's jump back into the example.
So we're gonna set a name.
And again, this is only for terraform reference.
So we'll call it DBI search.
And now let's start.
So we want to do is filter.
Now, what we're going to type in is name.
This is going to be tag kolon.
Name the name.
This one on the left in the context here is like a key value.
So the key we're searching for is a tag with the word name in it.
Next, we're going to put it is values because of risk and return more than one.
And that goes in a format of the list.
In this case, your name is going to be TV server.
Now, if we imagine there a hundred DB servers with this, what we can do is return results for any
server that matches this criteria.
Now, if we had 100 and we wanted to get the instance I.D., that's possible using data sources.
Now, this is one of these weird things were terrible that you'd think name would be key and values.
Well, that makes sense because it's a key value.
I digress to get some output to show how this is working.
What I'm gonna use is an output.
And I'm going to call this DV service.
Now, we don't expect to return one, but again, imagine if we had 100.
So we're going to use is data dot NWS underscore, for instance, DOT, DBI search.
And then we have a list of attributes that we can return if we have a quick look on the documentation.
These are the attributes we can return.
Am I?
The availability zone a much, much more.
And again, I'll include a link to her documentation, because as you can see here on the left, these
are the datasource options available for all the A.W. s resources.
I did like the idea of doing a name we I.D., but I think availability saying sounds quite interesting.
Now, what I am going to do is go back to the documentation, because as what's happened in the past,
my spelling sometimes isn't the greatest.
What we do is go back to the documentation.
And I would have copied this word availability zone just to make sure that it is 100 percent spelt correctly.
Now, what we can do is go back to our integrated terminal.
And what's going to happen when we run terraformed plan?
This is going to query a state file.
And if it's not available in their state vault is we're going to query eight of us now to get our output.
We are going to have to run, terraform, apply Simonas or cut out the middleman and run.
Terraformed, apply.
Dash, quarto.
Dash, approve.
How awesome is that?
Is queried A.W. s return, a result which now tells us what availability zone a DP server is in.
And again, that's the power of data sources.
They effectively allow us to query A.W. s to return information and as we mentioned, is good for auditing
purposes, but it's also great for setting things up.
If we need to get latest M I's all we need to get details about VPC.
It's an option we can use.
Now let's go ahead and tear down our environment.
So run, terraform, destroy.
And again, what we can also do is bash also Dasch approved.
Be very careful with his command.
We're running it because you can tear down absolutely everything.
In this case, we've only set up two easy two instances.
So that's fine.
Let's go ahead and run it.
Awesome.
That's now completed.
And that also wraps up this lesson on data sources.
I look forward to seeing you in the next lesson.