- Now let's learn about resources and data sources.
So this is where we start
to really manage our resources in the cloud.
So up to this point,
we've kinda learned about how Terraform works,
a little bit about its inner workings,
but this is what we actually want Terraform to do.
So just sort of two categories here,
resources and data sources.
There's a subtle difference between these.
So a resource is just simply something
that you want Terraform to manage in your cloud environment.
So examples are VM instances,
networking objects like VPC storage buckets,
things like that.
Data sources are things that you want to refer to
in your Terraform code,
but that are not managed by Terraform.
So some good examples of that are regions
and availability zones,
you know, AWS accounts, things like that.
So for example,
if you're creating your networking infrastructure
and, you know, you need to specify
which availability zones those are in,
you don't manage and create availability zones.
And so you need a way to reference those,
and Terraform does that with data sources.
And the configuration for those is similar to the resources,
and we saw a little bit of resource creation,
but I wanna go over it in some more detail here.
This is one of the main things that you're gonna be doing
when you're developing Terraform code.
So I'm just gonna switch to my text editor here.
And we've got some example Terraform code here
that kind of explains a little bit about what we're doing.
So again, I'm using an S3 bucket as an example,
just because it's a very, very simple resource.
So this is an S3 bucket defined here on line six.
So we're telling Terraform that we want this bucket
to exist in our state and in our cloud.
This is a resource with no configuration again,
but you can see if we go down a little bit
that we have a bucket further down here
where we are giving it a little bit of configuration, right?
So here, we're setting the bucket parameter in AWS,
that's the name of the bucket.
They just call it bucket
for whatever reason rather than the name.
So we could apply this and create this bucket.
These resources could be,
you know, any kind of object in AWS
and, you know, there are thousands of different services
and resources that the Terraform provider supports.
Now, similar to that, we have this data source, right?
So here, it's indicated by the keyword data.
The resource has the keyword resource for it.
Data source data tells AWS
that we wanna fetch some value from the cloud
and use it in our configuration.
So we do that by using this data syntax.
And in this case, we're gonna get the caller identity.
And that simply reflects back the, you know, the ID of,
you know, whoever's calling this Terraform code,
so it'll give you the user ID
and it'll give you the account ID and stuff like that.
And then similarly, you can see here,
we've got availability zones here.
So we're gonna fetch a list of availability zones
so that we can use those in our configuration.