DEV Community

Cover image for ->> Day-11 AWS Terraform Functions - Part 1
Amit Kushwaha
Amit Kushwaha

Posted on

->> Day-11 AWS Terraform Functions - Part 1

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Examples

> replace("1 + 2 + 3", "+", "-")
1 - 2 - 3

> replace("hello world", "/w.*d/", "everybody")
hello everybody
Enter fullscreen mode Exit fullscreen mode

> Substr Function

substr extracts a substring from a given string by offset and (maximum) length.

substr(string, offset, length)
Enter fullscreen mode Exit fullscreen mode

Examples

> substr("hello world", 1, 4)
ello
Enter fullscreen mode Exit fullscreen mode

> Split Function

split produces a list by dividing a given string at all occurrences of a given separator.

split(separator, string)
Enter fullscreen mode Exit fullscreen mode

Examples

> split(",", "foo,bar,baz")
[
  "foo",
  "bar",
  "baz",
]

> split(",", "foo")
[
  "foo",
]

> split(",", "")
[
  "",
]
Enter fullscreen mode Exit fullscreen mode

>> 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
}
Enter fullscreen mode Exit fullscreen mode

> 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)
Enter fullscreen mode Exit fullscreen mode

Examples

> lookup({a="ay", b="bee"}, "a", "what?")
ay

> lookup({a="ay", b="bee"}, "c", "what?")
what?
Enter fullscreen mode Exit fullscreen mode

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)