Bird
Raised Fist0
Terraformcloud~10 mins

Comments in HCL in Terraform - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Comments in HCL
Start reading HCL file
↓
Encounter # or //?
Yes→Ignore rest of line
No↓
Encounter /*?
Yes→Ignore until */ found
No↓
Process code normally
↓
End of file
Terraform reads the file line by line, ignoring text after # or // on a line, or between /* and */ for block comments, then processes the rest as code.
Execution Sample
Terraform
# This is a comment
resource "aws_s3_bucket" "b" {
  // Another comment
  bucket = "mybucket"
  /* Block comment
     spanning lines */
  acl = "private"
}
This HCL snippet shows single-line comments with # and //, and a multi-line block comment with /* */ inside a resource block.
Process Table
StepLine ReadComment DetectedActionCode Processed
1# This is a commentYes (#)Ignore entire line
2resource "aws_s3_bucket" "b" {NoProcess line as coderesource "aws_s3_bucket" "b" {
3 // Another commentYes (//)Ignore entire line
4 bucket = "mybucket"NoProcess line as codebucket = "mybucket"
5 /* Block commentYes (/*)Start ignoring until */
6 spanning lines */Inside block commentEnd ignoring at */
7 acl = "private"NoProcess line as codeacl = "private"
8}NoProcess line as code}
9End of fileNoStop processing
💡 Reached end of file, all comments ignored properly, code lines processed.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5-6After Step 7After Step 8Final
Processed Code Lines[][]["resource \"aws_s3_bucket\" \"b\" {"]["resource \"aws_s3_bucket\" \"b\" {"]["resource \"aws_s3_bucket\" \"b\" {", "bucket = \"mybucket\""]["resource \"aws_s3_bucket\" \"b\" {", "bucket = \"mybucket\""]["resource \"aws_s3_bucket\" \"b\" {", "bucket = \"mybucket\"", "acl = \"private\""]["resource \"aws_s3_bucket\" \"b\" {", "bucket = \"mybucket\"", "acl = \"private\"", "}"]["resource \"aws_s3_bucket\" \"b\" {", "bucket = \"mybucket\"", "acl = \"private\"", "}"]
Key Moments - 3 Insights
Why does the line starting with # get ignored completely?
Because lines starting with # are single-line comments, Terraform ignores everything after # on that line as shown in execution_table step 1.
How does Terraform handle multi-line comments with /* and */?
Terraform ignores all text starting from /* until it finds */, skipping all lines in between, as shown in steps 5 and 6 in the execution_table.
What happens to lines with // comments?
Lines starting with // are treated as single-line comments and ignored entirely, like in step 3 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What code line is processed?
Abucket = "mybucket"
B// Another comment
C# This is a comment
Dacl = "private"
💡 Hint
Check the 'Code Processed' column at step 4 in the execution_table.
At which steps does Terraform ignore lines due to block comments?
ASteps 1 and 3
BSteps 2 and 4
CSteps 5 and 6
DSteps 7 and 8
💡 Hint
Look for 'Start ignoring until */' and 'Inside block comment' in the 'Comment Detected' column.
If the line with // comment was changed to a code line, how would the 'Processed Code Lines' change after step 3?
AIt would still ignore the line
BIt would include the new code line after step 3
CIt would cause an error
DIt would be treated as a block comment
💡 Hint
Refer to variable_tracker 'Processed Code Lines' after step 3 and how comments affect processing.
Concept Snapshot
Comments in HCL:
- Use # or // for single-line comments
- Use /* ... */ for multi-line block comments
- Terraform ignores comments during parsing
- Comments can be placed anywhere outside strings
- Properly ignoring comments keeps code clean and readable
Full Transcript
Terraform configuration files use comments to add notes or explanations without affecting the code. Single-line comments start with # or // and ignore the rest of that line. Multi-line comments start with /* and end with */ and can span multiple lines. When Terraform reads the file, it skips these comments and processes only the actual code lines. This helps keep configurations clear and maintainable.

Practice

(1/5)
1. What is the purpose of comments in Terraform's HCL language?
easy
A. To explain the code without affecting its execution
B. To execute additional commands
C. To speed up the Terraform apply process
D. To delete unused resources automatically

Solution

  1. Step 1: Understand the role of comments

    Comments are notes in code that help explain what the code does but do not change how it runs.
  2. Step 2: Identify the correct purpose

    Since comments do not execute or affect resources, their purpose is purely explanatory.
  3. Final Answer:

    To explain the code without affecting its execution -> Option A
  4. Quick Check:

    Comments explain code [OK]
Hint: Comments never change code behavior, only explain it [OK]
Common Mistakes:
  • Thinking comments run code
  • Assuming comments speed up deployment
  • Believing comments delete resources
2. Which of the following is a valid single-line comment syntax in Terraform HCL?
easy
A. /* This is a comment */
B. # This is a comment
C. -- This is a comment
D. %% This is a comment

Solution

  1. Step 1: Recall single-line comment syntax in HCL

    Terraform HCL supports # and // for single-line comments.
  2. Step 2: Match options with valid syntax

    # This is a comment uses #, which is valid. /* This is a comment */ is multi-line comment syntax. Options C and D are invalid in HCL.
  3. Final Answer:

    # This is a comment -> Option B
  4. Quick Check:

    Single-line comment = # or // [OK]
Hint: Single-line comments start with # or // only [OK]
Common Mistakes:
  • Using -- or %% which are not valid in HCL
  • Confusing multi-line comment syntax for single-line
  • Omitting comment symbols
3. What will happen if you run this Terraform code snippet?
resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"  # This is a small instance
  // This line is ignored
  /* multi-line
     comment here */
}
medium
A. Terraform will create an AWS instance ignoring all comments
B. Terraform will throw a syntax error due to comments
C. Terraform will create an instance but ignore the instance_type
D. Terraform will skip the resource block entirely

Solution

  1. Step 1: Understand how comments affect Terraform code

    Comments are ignored by Terraform during execution and do not affect resource creation.
  2. Step 2: Analyze the code snippet

    The resource block is valid with comments in single-line (#, //) and multi-line (/* */) forms. Terraform will create the instance as specified.
  3. Final Answer:

    Terraform will create an AWS instance ignoring all comments -> Option A
  4. Quick Check:

    Comments ignored, resource created [OK]
Hint: Comments do not affect resource creation or syntax [OK]
Common Mistakes:
  • Thinking comments cause syntax errors
  • Assuming comments disable resource properties
  • Believing comments skip resource blocks
4. Identify the error in this Terraform snippet:
resource "aws_s3_bucket" "bucket" {
  bucket = "my-bucket"
  /* This is a multi-line comment
  missing the closing tag
medium
A. Single-line comment used incorrectly
B. Incorrect resource type name
C. Bucket name must be numeric
D. Missing closing */ for multi-line comment

Solution

  1. Step 1: Check multi-line comment syntax

    Multi-line comments start with /* and must end with */. The snippet lacks the closing */.
  2. Step 2: Confirm other parts are correct

    Resource type and bucket name are valid. No single-line comment issues.
  3. Final Answer:

    Missing closing */ for multi-line comment -> Option D
  4. Quick Check:

    Unclosed multi-line comment [OK]
Hint: Always close multi-line comments with */ [OK]
Common Mistakes:
  • Forgetting to close multi-line comments
  • Confusing resource type errors with comment errors
  • Assuming bucket names must be numeric
5. You want to temporarily disable a block of Terraform code without deleting it. Which comment style should you use to comment out multiple lines safely?
hard
A. Use # at the start of each line
B. Use // at the start of each line
C. Use /* */ to wrap the entire block
D. Use XML style <!-- --> comments

Solution

  1. Step 1: Review comment styles for multiple lines

    Single-line comments (# or //) require prefixing each line, which is tedious for many lines.
  2. Step 2: Identify the best multi-line comment method

    Using /* */ wraps multiple lines easily, disabling the entire block at once.
  3. Step 3: Exclude invalid options

    XML style comments are not valid in HCL.
  4. Final Answer:

    Use /* */ to wrap the entire block -> Option C
  5. Quick Check:

    Multi-line comments use /* */ [OK]
Hint: Wrap multiple lines with /* and */ to comment out block [OK]
Common Mistakes:
  • Using # or // for many lines instead of block comment
  • Trying XML style comments which are invalid
  • Not closing multi-line comment properly