AdSense

網頁

2021/11/13

Terraform 資料型態範例 value types example

Terraform 資料型態範例。


在「Terraform local_file hello world」範例的hello.tf中利用了local_file resouce產生一個內容為Hello world!的文字檔hello.txt。下面則把local_file resource的content的值以local values的方式傳入各種型態值組成的內容。


修改hello.tf內容如下。Local values定義各型態值:

  • name - string,字串,實字使用雙引號
  • age - number,數值,可為整數或小數。
  • manager - bool,布林,可用於判斷式。
  • ext - list(or tuple),多個值組成的序列。
  • dept - map(or object),物件,由多個帶名稱的值所組成。

hello.tf

resource "local_file" "hello" {
    filename = "${path.module}/hello.txt"
    content  = local.content
}

locals {
    name = "John"
    age = 33
    manager = true
    ext = [331, 332]
    dept = {
        name = "IT"
        employees = 5
    }

    content = <<-EOT
            Hello ${local.name}, ${local.age} years old.
            ${local.manager ? "A manager" : "An employee"} of ${local.dept.name} department with ${local.dept.employees} employees.
            Extensions are ${join(", ", local.ext)}.
            EOT
}

<<-EOT ... EOTIndented Heredocs表示的多行字串。


hello.tf所在目錄以命令列輸入terraform apply執行結果如下。

hello.txt

Hello John, 33 years old.
A manager of IT department with 5 employees.
Extensions are 331, 332.

參考github


沒有留言:

AdSense