【Terraform】JSONファイルに変数を埋め込んでリソースから値を渡す方法

Terraform
スポンサーリンク

はじめに

この記事では TerraformでJSONやYAMLファイルに変数を埋め込んで、実行時に値を渡す方法をまとめます。

templatefile関数を使う

templatefile - Functions - Configuration Language | Terraform | HashiCorp Developer
The templatefile function reads the file at the given path and renders its content as a template.

JSONに変数を埋め込んで値を渡すには、リソース側で templatefile関数 を利用します。

第一引数に用意しておいたJSONファイルのパスを指定し、第二引数で変数のマップを指定します。

スポンサーリンク

ファイルに変数を埋め込む

JSONファイルに変数を埋め込むには、templatefile関数で設定した変数名を “${ … }” で囲みます。

これでファイルに埋め込んだ変数に対して、リソース実行時に値を渡すことができます。