Bird
Raised Fist0
Terraformcloud~20 mins

Comments in HCL in Terraform - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
HCL Comments Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:00remaining
Identify the valid single-line comment syntax in HCL
Which of the following lines is a valid single-line comment in HashiCorp Configuration Language (HCL)?
A# This is a comment
B// This is a comment
C/* This is a comment */
D<!-- This is a comment -->
Attempts:
2 left
💡 Hint
HCL supports single-line comments starting with # or //, and multi-line comments with /* */.
❓ Configuration
intermediate
1:30remaining
Effect of comments on resource count
Given the following Terraform configuration snippet, how many resources will be created?
Terraform
resource "aws_instance" "example1" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
}

# resource "aws_instance" "example2" {
#   ami           = "ami-654321"
#   instance_type = "t2.micro"
# }

// resource "aws_instance" "example3" {
//   ami           = "ami-abcdef"
//   instance_type = "t2.micro"
// }
A3
B2
C0
D1
Attempts:
2 left
💡 Hint
Lines starting with # or // are ignored by Terraform.
❓ service_behavior
advanced
1:30remaining
Multi-line comment effect in HCL
What will happen if you use the following multi-line comment syntax in a Terraform configuration file?
Terraform
/*
resource "aws_s3_bucket" "bucket" {
  bucket = "my-bucket"
  acl    = "private"
}
*/

resource "aws_s3_bucket" "bucket2" {
  bucket = "my-bucket-2"
  acl    = "private"
}
AOnly aws_s3_bucket.bucket2 will be created; the first resource is ignored.
BBoth aws_s3_bucket.bucket and aws_s3_bucket.bucket2 will be created.
CTerraform will raise a syntax error due to incorrect comment syntax.
DNo resources will be created because all are commented out.
Attempts:
2 left
💡 Hint
Multi-line comments in HCL start with /* and end with */.
❓ security
advanced
1:30remaining
Security risk of commented sensitive data
Why is it a bad practice to leave sensitive information like passwords or API keys in commented lines in Terraform files?
ACommented lines are encrypted and safe from exposure.
BTerraform automatically removes comments before applying configurations.
CCommented sensitive data can still be exposed in version control or logs.
DComments are ignored by all tools and cannot be accessed.
Attempts:
2 left
💡 Hint
Think about where your code files are stored and who can see them.
❓ Architecture
expert
2:00remaining
Impact of comments on Terraform plan output
Consider a Terraform file where a resource block is commented out using single-line comments. What will be the impact on the output of the `terraform plan` command?
Terraform
resource "aws_security_group" "sg1" {
  name        = "sg1"
  description = "Allow SSH"
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

// resource "aws_security_group" "sg2" {
//   name        = "sg2"
//   description = "Allow HTTP"
//   ingress {
//     from_port   = 80
//     to_port     = 80
//     protocol    = "tcp"
//     cidr_blocks = ["0.0.0.0/0"]
//   }
// }
ATerraform plan will show changes only for aws_security_group.sg1; sg2 is ignored.
BTerraform plan will show no changes because comments disable all resources.
CTerraform plan will fail with a syntax error due to commented lines.
DTerraform plan will show changes for both sg1 and sg2 resources.
Attempts:
2 left
💡 Hint
Comments disable code from being processed by Terraform.

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