- Okay, so now I wanna talk about
making HCL a little bit more useful.
Obviously,
choosing random names for your resources
like buckets and so forth
isn't gonna be terribly useful in the real world.
That's not generally what we want.
So,
and now, I'm using S3 buckets as an example
for a couple of reasons.
One, because they take no configuration
to get started, there's no required attributes.
And two, they're very simple and they're,
they don't cost anything if,
if you don't store any data in them,
if you just create them.
So they make a good example for configuring Terraform
and doing demonstrations.
But you should keep in mind that
these techniques apply to any resource in Terraform, okay?
So what I'm about to show you is called interpolation.
This is simply
evaluating string values and
creating custom resource identifiers,
and I'll show you the syntax for doing that.
Okay.
So,
you remember our previous example
where we didn't have any attributes,
and then we put in bucket and we called it bucket one,
and it wouldn't work because something was
already named that.
So,
let's say we decide on a naming convention
that we're gonna prefix our bucket names
with the account ID that we saw
in the caller identity before.
This is how we would do that, okay?
So, we're gonna call this one bucket two.
And remember, this is just a label internal to Terraform.
And then we're gonna create a quoted string, okay?
And then within a quoted string,
the syntax for this is dollar sign, curly brace,
and then the attribute that you wanna place in that string
and then the closing curly brace.
Now, when Terraform runs, it's gonna interpolate that,
and it's gonna substitute in the value
into this part, okay?
So this is gonna resolve to being the account ID string.
And it's gonna create your bucket name
that way.
Prior to Terraform 12, interpolation was
essentially the only way to reference between
objects in Terraform.
Since Terraform 12,
this syntax, you see how this isn't quoted,
it doesn't have the dollar sign,
it doesn't have the curly braces, it's just a raw value.
So this was introduced in Terraform 12.
Prior to Terraform 12, to do this,
you would have to
quote it and
put the brackets, even though there really is no
interpolation here, it would just interpolate that value.
Now,
this
is valid, and this is the recommended way
to reference variables within HCL.
But if you need to,
if you need to concatenate it into a string,
this is a good shortcut for doing that,
because it doesn't require you to use
a format,
a function or anything like that.
So it's a real simple way.
It's a pretty self-explanatory code
most of the time, so you can
see what we get there, the plan should tell us
what the name of the bucket is going to be
before we apply it.
So you can see
that it's already done the interpolation within the plan,
so that's already resolved.
Okay.
So we have our account ID, and then a suffix of bucket two,
which
you saw in the code.
So we'll just create that.