From 6aae3b1a1aea1f426de1bc335734bd8666abd1bd Mon Sep 17 00:00:00 2001 From: NISHIMURA Yoshitaka Date: Mon, 26 Oct 2026 22:18:27 +0900 Subject: [PATCH] feat(terraform_jp): 2026-10-26 'fix: #432 tfstylus' --- README.md | 1 + articles/2026-10-26_tfstylus.md | 81 +++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 articles/2026-10-26_tfstylus.md diff --git a/README.md b/README.md index 5c2da2d..06b4305 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,7 @@
2026 年の記事 +- 2026-10-26 [ [:memo:](articles/2026-10-26_tfstylus.md) ] [terraform のコードを少し整える tfstylus を作ってみた](https://zenn.dev/terraform_jp/articles/2026-10-26_tfstylus) - 2026-07-07 [ [:memo:](articles/2026-07-07_terraform_graph_format_mermaid.md) ] [terraform graph -format mermaid が来るっぽいので試してみた](https://zenn.dev/terraform_jp/articles/2026-07-07_terraform_graph_format_mermaid) - 2026-06-26 [ [:memo:](articles/2026-06-26_terraform.md) ] [tflint と terraform-docs のバッティングを回避](https://zenn.dev/terraform_jp/articles/2026-06-26_terraform) - 2026-06-23 [ [:memo:](articles/2026-06-23_terraform_floci.md) ] [Azure のローカル開発に floci を使ってみた。が](https://zenn.dev/terraform_jp/articles/2026-06-23_terraform_floci) diff --git a/articles/2026-10-26_tfstylus.md b/articles/2026-10-26_tfstylus.md new file mode 100644 index 0000000..d2fdd71 --- /dev/null +++ b/articles/2026-10-26_tfstylus.md @@ -0,0 +1,81 @@ +--- +title: "terraform のコードを少し整える tfstylus を作ってみた" +emoji: "🐣" +type: "tech" +topics: ["terraform", "AI", "tfstylus"] +published: true +publication_name: "terraform_jp" +--- + +# tl;dr + +@[card](https://github.com/officel/tfstylus) + +- 歴史のある terraform のコードを整えるツールを作ったので紹介 + +# tfstylus is + +- terraform を使い出すとディレクトリやファイルがたくさんになる +- コードを書く人やツールによって、同じリソースブロックでも並び順が異なるなど +- アウトカムはないに等しいけど、整えられるのに整えないのは気持ち悪いって同士のためにツールを作ってみた +- 公式記載の大雑把な並び順に加えて、個人的な経験に基づいて読みやすい順になるようにしている +- 全機能をオフにして何もしない、からスタートできる +- `plan` と `apply` で terraform に揃えたので覚えやすい(はず) + +# demo + +ここに variable を書いたコードがあるじゃろ + +```hcl:variable.tf +variable "test" { + default = "test001" + type = "string" + description = "テスト" +} +``` + +これをこうして(`tfstylus plan`) + +```patch +variable.tf: rename to variables.tf [file-name/variables] variable.tf contains a variable block and variables.tf does not exist +variable.tf:1:1: variable_order: variable elements should be ordered description, deprecated, sensitive, nullable,ephemeral, const, type, default, validation +diff --git a/variable.tf b/variables.tf +similarity index 66% +rename from variable.tf +rename to variables.tf +--- a/variable.tf ++++ b/variables.tf +@@ -1,6 +1,6 @@ + variable "test" { ++ description = "テスト" ++ type = "string" + default = "test001" +- type = "string" +- description = "テスト" + } +``` + +こうじゃ(`tfstylus apply`) + +```hcl:variables.tf +variable "test" { + description = "テスト" + type = "string" + default = "test001" +} +``` + +漏れがなければ terraform の公式リソースブロックは全部網羅してるはずなので、ぜひお手元で試してみて欲しいなって。 + +# 連載にしようと思う + +- 今のところ満足する形で動いている +- AI 頼りで2週間くらいかかってる +- 自己振り返りまでいったん終わったので、内容を整理して何回かにわけて公開しようかなって +- べ、べつにしばらく terraform の記事書いてないからってわけじゃないんだからねっ(懐かしのツンデレ + +# まとめ + +- terraform のコードを少しだけ整える `tfstylus` というツールを作った(ので紹介) +- ぜひお手元のコードで試してみて欲しい +- 次回から、何回かにわけてツールを作った振り返りを公開していこうと思います