Terrafrom函數解析

  • 2020 年 3 月 18 日
  • 筆記

Terraform支援很多內建的函數可以用來處理字元串、數值計算、加密等。調用方式如下:

<FUNCTION NAME>(<ARGUMENT 1>, <ARGUMENT 2>)

本文主要對Terraform支援的函數進行總結,簡要說明其用處及用法。

數值計算函數

函數名

描述

使用示例

示例結果

abs

計算絕對值

abs(-123.4)

123.4

ceil

向上取整

ceil(5.1)

6

floor

向下取整

floor(4.9)

4

log

計算對數

log(16, 2)

4

max

取最大值

max(12, 54, 3)

54

min

取最小值

min(12, 54, 3…)

3

parseint

將字元串轉換為整數

parseint("FF", 16)

255

pow

計算x的y次冪

pow(3, 2)

9

signum

計算數字的正負,正數返回1,負數返回-1,0返回0

signum(-13)

-1

字元串處理函數

函數名

描述

使用示例

示例結果

chomp

移除字元串末尾的換行符

chomp("hellorn")

hello

format

格式化字元串

format("Hello, %s!", "world")

Hello, world!

formatlist

格式化字元串,返回字元串列表

format("Hello, %s", "world", "tencent")

"Hello, world", "Hello, tencent"

indent

在多行字元串的每一行前面添加指定數量的空格(統一縮進)

indent(2, "testnnew")

testn new

join

把數組中的所有元素放入一個字元串,元素通過指定的分隔符分隔

join (", ", "foo", "bar", "baz")

foo, bar, baz

lower

將字元串中的字母轉換為小寫

lower("HELLO")

hello

uper

將字元串中的字元轉換為大寫

upper("hello")

HELLO

regex

正則匹配函數

regex("a-z+", "53453453.345345aaabbbccc23454")

aaabbbccc

regexall

正則匹配函數,返回所有的匹配的字串列表

regexall("a-z+", "1234abcd5678efgh9")

"abcd","efgh"

replace

字元替換函數,支援正則匹配

replace("hello world", "/w.*d/", "everybody")

hello everybody

split

分割字元串

split(",", "foo,bar,baz")

"foo","bar","baz"

strrev

翻轉字元串

strrev("hello")

olleh

substr

在字元串中抽取從start下標開始的指定數目的字元

substr("hello world", 1, 4)

ello

title

將給定字元串的每一行首字母大寫

title("hello world")

Hello World

trim

移除字元串開頭和結尾的指定字元

trim("?!hello?!", "!?")

hello

trimprefix

移除字元串開頭的指定字元

trimprefix("helloworld", "hello")

world

trimsuffix

移除字元串結尾的指定字元

trimsuffix("helloworld", "world")

hello

trimspace

移除字元串開頭和結尾的空白字元

trimspace(" hellonn")

hello

集合函數

函數名

描述

使用示例

示例結果

chunklist

將單個list分割為多個固定大小的list

chunklist(list, chunk_size)

coalesce

返回第一個非空的字元串

coalesce("", "b")

b

coalescelist

返回第一個非空的list

coalescelist([], "c", "d")

"c","d"

compact

移除list中的空字元串

compact("a", "", "b", "c")

"a","b","c"

concat

將多個list合併為一個list

concat("a", "", "b", "c")

"a", "", "b", "c"

contains

判斷list中是否包含給定的值

contains("a", "b", "c", "a")

true

distinct

移除list中的重複元素

distinct("a", "b", "a", "c", "d", "b")

"a", "b", "c", "d"

element

返回list指定位置的元素

element("a", "b", "c", 1)

b

flatten

將所有list中的元素合併到一個list

flatten(["a", "b", [], "c"])

"a", "b", "c"

index

找到給定值在list中的位置

index("a", "b", "c", "b")

1

keys

返回map中的所有key

keys({a=1, c=2, d=3})

"a","c","d"

length

返回list, map或string的長度

length("hello")

5

list

將所有參數組成list

list("a", "b", "c")

"a","b","c"

lookup

查找map中的值,如果不存在,返回default

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

ay

map

將所有參數組成map

map("a", "b", "c", "d")

{"a" = "b", "c" = "d"}

merge

將多個map合併為一個map,並移除重複的key

merge({"a"="b", "c"="d"}, {"e"="f", "c"="z"})

{"a" = "b", "c" = "z", "e" = "f"}

range

根據start, limit, step生成一個list

range(start, limit, step)

reverse

翻轉list

reverse(1, 2, 3)

3,2,1

setintersection

返回多個集合中都有的元素集合

setintersection("a", "b", "b", "c", "b", "d")

"b"

setproduct

返回集合的笛卡爾積

setproduct("a", "b")

["a","b"]

setsubtract

返回第一個集合中有的但第二個集合中沒有的元素集合

setsubtract("a", "b", "c", "a", "c")

"b"

setunion

返回多個集合的並集

setunion("a", "b", "b", "c", "d")

"d","b","c","a"

slice

從list中取連續的子list

slice("a", "b", "c", "d", 1, 3)

"b","c"

sort

對list按升序排序

sort("e", "d", "a", "x")

"a","d","e","x"

transpose

交換map的key和value

transpose({"a" = "1", "2", "b" = "2", "3"})

{"1" = "a","2" = "a","b","3" = "b"}}

values

返回map的所有value

values({a=3, c=2, d=1})

3,2,1

zipmap

將兩個list組合成map

zipmap("a", "b", 1, 2)

{"a" = 1,"b" = 2}

編碼函數

函數名

描述

使用示例

示例結果

base64decode

base64解碼函數

base64decode("SGVsbG8gV29ybGQ=")

Hello World

base64encode

base64編碼函數

base64encode("Hello World")

SGVsbG8gV29ybGQ=

base64gzip

gzip壓縮字元串然後base64編碼

base64gzip("AAAAAAAAAAAAAAAAAAAAAAAA")

H4sIAGUdX14AA3N0xA64ADb7j4sZAAAA

csvdecode

解碼csv格式的字元串數據

csvdecode("a,b,cn1,2,3")

{"a" = "1","b" = "2","c" = "3"}

jsondecode

json解碼函數

jsondecode("{"hello": "world"}")

{"hello" = "world"}

jsonencode

json編碼函數

jsonencode({"hello"="world"})

{"hello":"world"}

urlencode

url編碼函數

urlencode("Hello World")

Hello%20World

yamldecode

yaml解碼函數

yamldecode("{"hello": "world"}")

{"hello" = "world"}

yamlencode

yaml編碼函數

yamlencode({"a":"b"})

"a": "b"

文件操作函數

函數名

描述

使用示例

示例結果

abspath

返回絕對路徑

abspath(path.root)

/home/user/some/terraform/root

dirname

返迴文件路徑,移除最後的文件名

dirname("foo/bar/baz.txt")

foo/bar

pathexpand

展開用戶的HOME目錄

pathexpand("~/.ssh/id_rsa")

/home/steve/.ssh/id_rsa

basename

返迴路徑中的文件名

basename("foo/bar/baz.txt")

baz.txt

file

讀取文件並返迴文件內容

file("hello.txt")

Hello World

fileexists

判斷給定的路徑是否存在

fileexists("hello.txt")

true

fileset

正則匹配目錄下的文件

fileset(path.module, "files/*.txt")

"files/hello.txt","files/world.txt"

filebase64

讀取文件並返迴文件內容的base64編碼

filebase64("hello.txt")

SGVsbG8gV29ybGQ=

templatefile

讀取模板文件並渲染

templatefile(path, vars)

日期時間函數

函數名

描述

使用示例

示例結果

formatdate

格式化日期時間格式,輸入日期為RFC 3339格式

formatdate("MMM DD, YYYY", "2018-01-02T23:12:01Z")

Jan 02, 2018

timeadd

加一段時間到日期上,返回一個新日期

timeadd("2019-11-22T00:00:00Z", "10m")

2019-11-22T00:10:00Z

timestamp

返回當前時間

timestamp()

2019-05-13T07:44:12Z

哈希和加密函數

函數名

描述

使用示例

示例結果

base64sha256

計算字元串的SHA256哈希值,然後base64編碼

base64sha256("hello world")

uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=

base64sha512

計算字元串的SHA512哈希值,然後base64編碼

base64sha512("hello world")

MJ7MSJwS1utMxA9QyQLytNDtd+5RGnx6m808qG1M2G+Y…

bcrypt

計算Blowfish演算法加密值

bcrypt("hello world")

$2a$10$D5grTTzcsqyvAeIAnY/mYOIqliCoG7eAMX0/oFcuD.iErkksEbcAa

filebase64sha256

計算文件的SHA256哈希值,然後base64編碼

filebase64sha256(filepath)

filebase64sha512

計算文件的SHA512哈希值,然後base64編碼

filebase64sha512(filepath)

filemd5

計算文件的md5值

filemd5(filepath)

filesha1

計算文件的sha1值

filesha1(filepath)

filesha256

計算文件的sha256值

filesha256(filepath)

filesha512

計算文件的sha512值

filesha512(filepath)

md5

計算字元串的md5值

md5("hello world")

5eb63bbbe01eeed093cb22bb8f5acdc3

rsadecrypt

解密RSA加密的密文

rsadecrypt(ciphertext, privatekey)

sha1

計算字元串的sha1值

sha1("hello world")

2aae6c35c94fcfb415dbe95f408b9ce91ee846ed

sha256

計算字元串的sha256值

sha256("hello world")

b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

sha512

計算字元串的sha512值

sha512("hello world")

309ecc489c12d6eb4cc40f50c902f2b4d0e…

uuid

隨機生成唯一ID

uuid()

b5ee72a3-54dd-c4b8-551c-4bdc0204cedb

uuidv5

根據名稱生成唯一ID

uuidv5("url", "https://www.terraform.io/")

9db6f67c-dd95-5ea0-aa5b-e70e5c5f7cf5

IP相關函數

函數名

描述

使用示例

示例結果

cidrhost

根據IP地址前綴和編號計算IP地址

cidrhost("10.12.127.0/20", 16)

10.12.112.16

cidrnetmask

計運算元網掩碼

cidrnetmask("172.16.0.0/12")

255.240.0.0

cidrsubnet

計運算元網地址(具體規則看官方文檔)

cidrsubnet("172.16.0.0/12", 4, 2)

172.18.0.0/16

類型轉換函數

函數名

描述

使用示例

示例結果

can

表明表達式是否能正確計算出結果

can(local.foo.bar)

false

tobool

轉換為布爾值

tobool("true")

true

tolist

轉換為list

tolist("a", "b", 3)

"a","b","3"

tomap

轉換為map

tomap({"a" = "foo", "b" = true})

{"a" = "foo", "b" = "true"}

tonumber

轉換為數字

tonumber("1")

1

toset

轉換為set

toset("a", "b", 3)

"a","b","3"

tostring

轉換為字元串

tostring(1)

1