2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Terraformのコメントの書き方について

Last updated at Posted at 2025-01-20

はじめに

どうも、@to-fmakです。最近、弊社におけるTerraformのコーディングルールをアップデートしました。その際に決定したコメントの書き方について、この記事で共有したいと思います。

Terraformのバージョン

現在、Terraformのバージョンは1.9系を使用しています。

Terraformにおけるコメントの書き方

Terraformでは以下の3種類のコメントスタイルがサポートされています。

  • #
  • //
  • /* */
公式ドキュメントより抜粋
The Terraform language supports three different syntaxes for comments:

    # begins a single-line comment, ending at the end of the line.
    // also begins a single-line comment, as an alternative to #.
    /* and */ are start and end delimiters for a comment that might span over multiple lines.

The # single-line comment style is the default comment style and should be used in most cases. Automatic configuration formatting tools may automatically transform // comments into # comments, since the double-slash style is not idiomatic.

公式ドキュメントに記載されている通り、#が最も一般的なスタイルです。

弊社でのコーディングルール

弊社では、以下のルールでコメントの書き方を使い分けています。

  • /* */ (複数ブロックの注釈)
    • 複数のコードブロックを1つの塊として扱う場合、最初のコードブロックの上に/* */を使用してコメントを記述する
  • #(複数行の注釈)
    • 複数行のコードに対する注釈を付ける場合、該当コードの上に#を使用する。この際、対象コードの前後に改行を入れ、可読性を確保する
  • #(単一行の注釈)
    • 単一行のコードに対する注釈の場合、該当コードの右側に#を使用する。ただし、コードが長い場合はコードの上に記述する
  • //(一時的なコメントアウト)
    • 一時的に無効化するコードには、//を使用してコメントアウトする
/*
 * ネットワーク設定
 */
resource "aws_vpc" "example" {
  cidr_block = "172.16.0.0/16"

  tags = {
    Name = "tf-example"
  }
}

resource "aws_subnet" "example" {
  vpc_id            = aws_vpc.example.id

  # ciderとazの設定
  cidr_block        = "172.16.10.0/24"
  availability_zone = "us-east-2a"

  tags = {
    Name = "tf-example" # nameタグ
    // TmpName = "sample" # 一時的な設定
  }
}

/*
 * XXX設定
 */
 ...
 ...

コーディングルールを決めた理由

  • 一般的なスタイルに準拠
    • #が最も一般的であるため、基本的にコードに注釈を付ける際は# を使用する
  • 可読性の向上
    • 複数のコードブロックを1つの塊として扱う際は/* */を使うことで、#と区別しやすくなり、全体の可読性が向上する
  • 一時的なコメントアウトの明確化
    • //を使用することで、通常のコメントと区別がつきやすくなる。コメントアウトは基本的に一時的に無効化するコードに使用するため、長期的に残さない前提である

おわりに

今回は、Terraformにおけるコメントの書き方と、弊社のコーディングルールについて紹介しました。
コーディングルールは、チーム内で合意することが重要であり、必ずしも正解が一つだけというわけではありません。この記事が、皆さんのルール策定の参考になれば幸いです。

参考URL

エンジニア募集

Gakken LEAP では教育をアップデートしていきたいエンジニアを絶賛大募集しています!!
ぜひお気軽にカジュアル面談へお越しください!!

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?