3
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のtfファイルをJSON(やYAML)で管理する

Last updated at Posted at 2020-06-11

個人的な感想としては、JSONファイルは、人間が編集して使用するには向いていない。terraform の tfファイル(HCL)は、やや人に優しくなっているが、YAMLほどではない。
つまり、何が言いたいかというと、terraformの管理をYAMLでやりたい。

terraformのtfファイルはJSONでも書ける

JSONとYAMLの変換は容易

JSONで書ければ、yqユーティリティーなどにより、YAMLへの変換やJSONへの戻しなどが容易に可能となる。

つまり、YAMLでterraformを管理できる

こんな感じ

# yq -y . main.tf.json > main.tf.yml  # JSONファイルをYAML化 (初回のみ)

# vi main.tf.yml                      # YAMLファイルを編集

# yq . main.tf.yml  > main.tf.json    # YAMLファイルをJSON化

注意点

  • JSONの場合、拡張子は .tf.json とする必要がある
  • JSONやYAMLでは、同じ属性をまとめないといけない(単一ファイル内では)
    • 例えば、トップレベルの"resource": { が複数箇所にあってはいけない。
    • (tfでは、マージされるので離れた箇所にあっても良い)
    • ファイルを別ければ、それぞれのファイルに1つなので少し緩和可能
  • 既存のtfファイルをJSON化するオフィシャルなツールはない
  • 変数などは${ }で囲う必要がある(tfでは囲う必要はなくなっている)
    • ${ }の中では、他の変数などに関して、再度${ }を使用する必要はない
3
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
3
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?