Terraform 内置函数(built-in functions)

Built-in Functions

Terraform包含了很多内置函数,内置函数可以在terraform控制端使用,比如:

> max(5, 12, 9)
12 

数字类的函数

> abs(23)
23
> abs(0)
0
> abs(-12.4)
12.4
> ceil(5)
5
> ceil(5.1)
6
> floor(5)
5
> floor(4.9)
4
> log(16, 2)
4
> min(12, 54, 3)
3
> parseint("100", 10)
100

> parseint("FF", 16)
255

> parseint("-10", 16)
-16

> parseint("1011111011101111", 2)
48879
> pow(3, 2)
9
> signum(-13)
-1
> signum(0)
0
> signum(344)
1	

字符串函数

> chomp("hello\n\n")
hello
> format("Hello, %s!", var.name)
Hello, Valentina!
> formatlist("%s, %s!", "Salutations", ["Valentina", "Ander", "Olivia", "Sam"])
[
  "Salutations, Valentina!",
  "Salutations, Ander!",
  "Salutations, Olivia!",
  "Salutations, Sam!",
]
> "  items: %{indent(2, "[\n  foo,\n  bar,\n]\n")}"
  items: [
    foo,
    bar,
  ]
> join(", ", ["foo", "bar", "baz"])
foo, bar, baz
> split("+", "1+2+3")
[1, 2, 3,]
> regex("[a-z]+", "53453453.345345aaabbbccc23454")
aaabbbccc
> regexall("[a-z]+", "1234abcd5678efgh9")
[
  "abcd",
  "efgh",
]
> replace("1 + 2 + 3", "+", "-")
1 - 2 - 3
> strrev("hello")
olleh
> substr("hello world", 1, 4)
ello
> title("hello world")
Hello World
> trim("?!hello?!", "!?")
hello
> trimprefix("helloworld", "hello")
world
> trimsuffix("helloworld", "world")
hello
> trimspace("  hello\n\n")
hello

集合类函数

字符串编码函数

文件系统函数

时间函数

加密函数

网络相关函数

类型转换函数

> local.foo
{
  "bar" = "baz"
}
> can(local.foo.bar)
true
> can(local.foo.boop)
false
> can(local.nonexist)

Error: Reference to undeclared local value

A local value with the name "nonexist" has not been declared.
> tolist(["a", "b", 3])
[
  "a",
  "b",
  "3",
]	
> tomap({"a" = "foo", "b" = true})
{
  "a" = "foo"
  "b" = "true"
}
> tonumber(1)
1
> tonumber("1")
1
> tonumber("no")
Error: Invalid function argument
> toset(["c", "b", "b", 3])
[
  "b",
  "c",
  "3",
]
> tostring("hello")
hello
> tostring(1)
1
> tostring(true)
true
> tostring([])
Error: Invalid function argument
> local.foo
{
  "bar" = "baz"
}
> try(local.foo.bar, "fallback")
baz
> try(local.foo.boop, "fallback")
fallback

以上

Table of Contents