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 5 years have passed since last update.

【Terraform】template_fileにてshellで変数を使う方法

Last updated at Posted at 2019-09-15

【Terraform】template_fileのshellで変数を使う方法

地味に探しにくかったのでメモ

最初

下記のようにshellを書くと${var}がterraformのvarsとして認識されてしまう。
そのため単純に変数を使うことができなかった。

main.tf

data "template_file" "shell" {
  template=<<EOF
    #!/bin/bash

    var=12345

    echo ${var}
  EOF
}

terraform plan実行結果

$ terraform plan

Error: data.template_file.shell: 1 error(s) occurred:

* data.template_file.shell: invalid variable syntax: "var". Did you mean 'var.var'? If this is part of inline `template` parameter
then you must escape the interpolation with two dollar signs. For
example: ${a} becomes $${a}

修正

varsに書くまではわかったが$を重ねないといけないというのがなかなか出てこなかった

main.tf

data "template_file" "shell" {
  vars = {
      var="$${var}" # $を重ねないといけない
  }
  template=<<EOF
    #!/bin/bash

    var=12345

    echo $${var}
  EOF
}
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?