0
0
Terraformcloud~10 mins

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

Choose your learning style9 modes available
Process Flow - Comments in HCL
Start reading HCL file
Encounter # or //?
YesIgnore rest of line
No
Encounter /*?
YesIgnore 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.