Comments

HCL supports single-line comments with # or // and block comments with /* */.

Syntax# single line // single line /* multi line */

Comments document your intent and are ignored by Terraform.

Comment styles

  • # text — the preferred single-line style.
  • // text — an alternative single-line style.
  • /* text */ — a multi-line block comment.

The idiomatic Terraform style, applied by terraform fmt, uses # for single-line comments.

Example

Example · hcl
# Preferred single-line comment
// Also valid, but fmt will convert it to #

/*
  A block comment spanning
  several lines, useful for
  temporarily disabling config.
*/
resource "aws_s3_bucket" "logs" {
  bucket = "soundscode-logs" # inline comment
}

When to use it

  • A team member adds a # TODO comment above a hard-coded AMI ID to flag it for replacement with a data source in the next sprint.
  • An engineer uses a /* */ block comment to temporarily disable an entire resource block during debugging without deleting the configuration.
  • A senior engineer uses // inline comments next to argument values to explain non-obvious choices like specific CIDR ranges or instance sizes.

More examples

Hash single-line comment

Shows the # comment style used both as a full-line comment and as an inline comment after an argument value.

Example · hcl
# This is the primary web server
resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"  # Ubuntu 22.04 LTS
  instance_type = "t3.micro"
}

Double-slash single-line comment

Uses the // comment style, common among developers from C-family language backgrounds, to disable a line.

Example · hcl
resource "aws_s3_bucket" "logs" {
  bucket = "my-app-logs"
  // force_destroy = true  // uncomment only for dev environments
}

Block comment to disable resource

Wraps an entire resource block in a /* */ comment to exclude it from the configuration temporarily.

Example · hcl
resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t3.micro"
}

/*
resource "aws_eip" "web" {
  instance = aws_instance.web.id
  # Disabled: EIP quota exceeded in this account
}
*/

Discussion

  • Be the first to comment on this lesson.