Introduction:
The Terraform language includes a number of built-in functions that you can call from within expressions to transform and combine values. The general syntax for function calls is a function name followed by comma-separated arguments in parentheses.
>> String Functions
>Lower Function
lower converts all cased letters in the given string to lowercase.
Examples
> lower("HELLO")
hello
This function uses Unicode's definition of letters and of upper- and lowercase.
> Replace Function
replace searches a given string for another given substring and replaces each occurrence with a given replacement string.
replace(string, substring, replacement)
Examples
> replace("1 + 2 + 3", "+", "-")
1 - 2 - 3
> replace("hello world", "/w.*d/", "everybody")
hello everybody
> Substr Function
substr extracts a substring from a given string by offset and (maximum) length.
substr(string, offset, length)
Examples
> substr("hello world", 1, 4)
ello
> Split Function
split produces a list by dividing a given string at all occurrences of a given separator.
split(separator, string)
Examples
> split(",", "foo,bar,baz")
[
"foo",
"bar",
"baz",
]
> split(",", "foo")
[
"foo",
]
> split(",", "")
[
"",
]
>> Collection Functions
> Merge Function
merge takes an arbitrary number of maps or objects, and returns a single map or object that contains a merged set of elements from all arguments.
If more than one given map or object defines the same key or attribute, then the one that is later in the argument sequence takes precedence. If the argument types do not match, the resulting type will be an object matching the type structure of the attributes after the merging rules have been applied.
Examples
> merge({a="b", c="d"}, {e="f", c="z"})
{
"a" = "b"
"c" = "z"
"e" = "f"
}
> merge({a="b"}, {a=[1,2], c="z"}, {d=3})
{
"a" = [
1,
2,
]
"c" = "z"
"d" = 3
}
> Lookup Function
lookup retrieves the value of a single element from a map, given its key. if the given key does not exist, the given default value is returned instead.
lookup(map, key, default)
Examples
> lookup({a="ay", b="bee"}, "a", "what?")
ay
> lookup({a="ay", b="bee"}, "c", "what?")
what?
Reference
>> Connect With Me
If you enjoyed this post or want to follow my #30DaysOfAWSTerraformChallenge journey, feel free to connect with me here:
πΌ LinkedIn: Amit Kushwaha
π GitHub: Amit Kushwaha
π Hashnode / Amit Kushwaha
π¦ Twitter/X: Amit Kushwaha

Top comments (0)