Want to save hours of confusion and fix mistakes before they happen? Comments are your secret weapon!
Why Comments in HCL in Terraform? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you are writing a long list of instructions to build your cloud setup by hand, without any notes or explanations.
Later, when you or someone else looks at it, it's hard to understand why certain choices were made.
Without comments, it's easy to forget the purpose of each part.
This leads to confusion, mistakes, and wasted time trying to figure out what the code does.
Comments in HCL let you add simple notes inside your code.
These notes don't affect how the code runs but help anyone reading it understand the why behind each step.
resource "aws_instance" "web" { ami = "ami-123456" instance_type = "t2.micro" }
# This is the web server instance resource "aws_instance" "web" { ami = "ami-123456" # Use latest stable AMI instance_type = "t2.micro" # Small instance for testing }
Clear, easy-to-understand infrastructure code that anyone can maintain and improve.
A team managing cloud servers can quickly see why a specific server type was chosen or why a setting is set a certain way, avoiding costly mistakes.
Comments explain your code's purpose.
They make teamwork smoother and faster.
They prevent confusion and errors.
Practice
Solution
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.Step 2: Identify the correct purpose
Since comments do not execute or affect resources, their purpose is purely explanatory.Final Answer:
To explain the code without affecting its execution -> Option AQuick Check:
Comments explain code [OK]
- Thinking comments run code
- Assuming comments speed up deployment
- Believing comments delete resources
Solution
Step 1: Recall single-line comment syntax in HCL
Terraform HCL supports#and//for single-line comments.Step 2: Match options with valid syntax
# This is a commentuses#, which is valid./* This is a comment */is multi-line comment syntax. Options C and D are invalid in HCL.Final Answer:
# This is a comment-> Option BQuick Check:
Single-line comment = # or // [OK]
- Using -- or %% which are not valid in HCL
- Confusing multi-line comment syntax for single-line
- Omitting comment symbols
resource "aws_instance" "example" {
ami = "ami-123456"
instance_type = "t2.micro" # This is a small instance
// This line is ignored
/* multi-line
comment here */
}Solution
Step 1: Understand how comments affect Terraform code
Comments are ignored by Terraform during execution and do not affect resource creation.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.Final Answer:
Terraform will create an AWS instance ignoring all comments -> Option AQuick Check:
Comments ignored, resource created [OK]
- Thinking comments cause syntax errors
- Assuming comments disable resource properties
- Believing comments skip resource blocks
resource "aws_s3_bucket" "bucket" {
bucket = "my-bucket"
/* This is a multi-line comment
missing the closing tag
Solution
Step 1: Check multi-line comment syntax
Multi-line comments start with /* and must end with */. The snippet lacks the closing */.Step 2: Confirm other parts are correct
Resource type and bucket name are valid. No single-line comment issues.Final Answer:
Missing closing */ for multi-line comment -> Option DQuick Check:
Unclosed multi-line comment [OK]
- Forgetting to close multi-line comments
- Confusing resource type errors with comment errors
- Assuming bucket names must be numeric
Solution
Step 1: Review comment styles for multiple lines
Single-line comments (# or //) require prefixing each line, which is tedious for many lines.Step 2: Identify the best multi-line comment method
Using/* */wraps multiple lines easily, disabling the entire block at once.Step 3: Exclude invalid options
XML style comments are not valid in HCL.Final Answer:
Use/* */to wrap the entire block -> Option CQuick Check:
Multi-line comments use /* */ [OK]
- Using # or // for many lines instead of block comment
- Trying XML style comments which are invalid
- Not closing multi-line comment properly
