1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

TerraformでJsonに変数を埋める

Posted at

関数を使う方法とdata sourceを使う方法があります

まず、Jsonの中では ${hoge} の形式で指定しておきます
例えば

{
  "region": "${region}"
}

関数を使う方法

参考: templatefile function

templatefile(path, vars)

Jsonを読み取るときに file の代わりに templatefile を使用します

= templatefile("path/to/foo.json", { hoge = var.fuga })

data source を使う方法

参考: template_file

data "template_file" "bar" {
  template = file("path/to/foo.json")
  vars = {
    hoge = var.fuga
  }
}

その上で、Jsonを指定する箇所で

= data.template_file.bar.rendered

と指定します

どっちを使うべき?

In Terraform 0.12 and later, the templatefile function offers a built-in mechanism for rendering a template from a file. Use that function instead, unless you are using Terraform 0.11 or earlier.

とあるので、一つ目の関数を使いましょう

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?