- So one really useful thing that Terrafrom
has implemented are functions.
Functions allow you to call
out externally to retrieve values
or calculate values outside your config.
Now unfortunately, Terraform does not have the ability
for you to implement your own functions directly
so we'll talk about a little bit more
in some later lessons
if you do have that need
but they have well over 100 built-in functions defined
and they can do just all kinds of different things.
So let's just take a look
at some of the more common ones
that I find myself using
and that you may use as well.
Now, the syntax for calling a function
is simply to embed the name
of the function in your code
and use the parentheses for any parameters.
You have to know the names of these functions.
They're all documented here.
You can just follow this link
and it will take you there.
It'll tell you all the functions.
It has 'em all broken out
by function type like date/time functions,
string functions, mathematical functions, et cetera
and then you can assign these to locals
just like you can assign any other expression.
You can use them interpolations.
You can use a function within another function
so I'll just show you some of the syntax for that.
It's pretty simple to understand.
But it's a really powerful feature.
So let's start off with just some simple date
and time functions.
These are used quite frequently.
This is the current timestamp.
This is used all the time
if you need to make a unique name for an S3 buckets
as our examples were.
This is probably what Terraform uses internally
when they make their default names.
This'll just give you a timestamp
and then you can, formatdate will give you the name
or the month of the year.
And this takes a standard date format string
and you can see we're referencing something
that we already assigned above
so you can reference within this same local block in here
so this takes two arguments.
It takes a format string
and it takes a timestamp.
And then if we wanted to increment the day by one,
we can call formatdate
with telling it that we just want to date to output
and we can call the timeadd function
so with timeadd, we're adding 24 hours
to the timestamp so that's gonna be tomorrow
and this is gonna tell us the day of the month.
There's lots of numeric functions in there.
These aren't used quite as often in my experience.
Here we're using the minimum function.
As I mentioned before in the last section,
we had a bug here
because we don't actually want
to check whether or not this is zero.
What we wanna do is check
whether it's above the minimum.
So the way to do that
is to use this min function.
This is gonna return the lesser of the number
of buckets or the bucket count.
Actually I think we actually want the max there
so we have a bug on top of a bug but you get the idea.
It's a simple numeric function
that compares two numbers
and you can return the min with the minimum function
and the min or the mac.
String functions are used very frequently.
In fact, we used lower earlier in this lesson.
So lower, upper, trimspace will remove any white space
from the beginning or end of a string.
Format, it's the same as the C string formatting functions.
So you give it a format string
and you give it the string to put it in there.
This one's not so useful because the interpolation
in Terraform is actually much more robust than this
but it does have a list form
so this will create hello
and it'll take a list
and it'll create four strings all with that
if that's useful.
That can be useful.
There're other interpolation methods now in Terraform
that may be more appropriate than this for most cases
and we'll talk about those later.
So that's kinda how functions work.
They're really simple to get started in using
by they can be really, really powerful.
You can do things like generate UUIDs
if you need a unique identifier.
That's pretty powerful ability.
You can look up values in a map,
things like that are all implemented in functions.