How to add comments to your Terraform code

Updated: February 3, 2024 By: Guest Contributor Post a comment

Introduction

Effective code documentation is an invaluable aspect of development, especially when working with complex infrastructure as code (IaC) tools like Terraform. Adding comments to your Terraform code not only makes it more understandable for others but also serves as a crucial reminder for yourself about the purpose or function of a specific piece of code. This tutorial will guide you through the basics to more advanced methods of commenting in Terraform, reinforcing best practices to maintain clean, readable code.

Why Comment Your Terraform Code

Before delving into the how, let’s focus on the why. Commenting your code is essential for several reasons:

  • Documentation: Comments serve as documentation for others who may work on your code in the future.
  • Code Readability: Well-commented code is easier to read and understand.
  • Future You Will Thank You: When you revisit your own code in the future, comments can quickly remind you of your thought process at the time of writing.

Single-Line Comments

The most basic form of comment in Terraform is the single-line comment. You can add a single-line comment by starting the line with a # or //. Here’s how to do it:

# This is a single-line comment

resource "aws_instance" "my_instance" {
  // This is another single-line comment
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

Multi-Line Comments

For longer explanations or to comment out blocks of code, Terraform supports multi-line comments that start with /* and end with */. This can be especially useful when testing or debugging specific parts of your code without deleting them:

/*
This is a multi-line comment
covering several lines of code
*/

resource "aws_instance" "my_instance" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

Best Practices for Commenting

While adding comments is beneficial, there’s an art to doing it right. Here are some best practices to follow:

  • Keep Comments Relevant and Up to Date: Comments should be timely and reflect the current state of the code. Outdated comments can be more harmful than no comments at all.
  • Don’t State the Obvious: Avoid commenting on code that is self-explanatory. Use comments to explain the why behind a code decision, not the what.
  • Use Comments to Explain Complex Logic: When your code does something non-obvious or tricky, that’s a great place for a comment.
  • Maintain Consistency: Establish a commenting style for your project and stick with it. Consistency makes reading and maintaining the code easier.

Advanced Commenting Techniques

As you grow more comfortable with Terraform, you may encounter situations where advanced commenting techniques can be useful:

  • Structured Comments for Automation: Some teams use structured comments to automate documentation generation. This often involves adhering to a specific format that can be parsed by tools to generate external documentation.
  • Commenting for Conditional Logic: Use comments to explain the purpose behind conditional logic within your Terraform code to make it clear why certain conditions are being checked.

Commenting Out Code

Sometimes, you may want to temporarily disable a portion of your Terraform code. Commenting out code is a common practice, but it should be used sparingly. Large blocks of commented-out code can make your files cluttered and hard to read. If you’re using version control (and you should be!), consider removing unused code instead and rely on your version history to retrieve it if needed.

Conclusion

Adding comments to your Terraform code is an essential practice for maintaining clarity, readability, and documentation. Whether you’re jotting down a quick single-line comment or explaining complex logic with multiple lines, remember to keep your comments relevant, concise, and consistent. Doing so will make your codebase more accessible and maintainable for both you and your collaborators.