【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
}