Why You See `this` in Terraform Modules — And Why You Should Use It Too
If you’ve explored the Azure Verified Modules (AVM), you may have noticed a consistent pattern in how resources are named: the use of this
. Whether it’s a virtual network, a private DNS zone, or a key vault, many AVM modules name their primary resources simply as this.
At first glance, this might seem odd. Terraform lets you name your resources whatever you want — so why not something more descriptive?
As it turns out, this isn’t just a naming shortcut — it’s a thoughtful convention borrowed from mainstream programming languages, and it offers some practical benefits: improved readability, predictability, and reusability across Terraform modules.
Why Is this
Used in Programming?
If you've written code in Python, JavaScript, Java, or C#, you're already familiar with this
— or something like it.
- In Python,
self
is used to refer to the current instance of a class. - In JavaScript,
this
refers to the context in which a function was invoked. - In Java/C#,
this
is used to refer to the current object, often to distinguish between instance variables and function parameters.
In all these languages, this
provides a clear and self-referential way to talk about the object in context. It's about scoping — avoiding confusion over which object or variable you're referring to.
The Terraform Parallel
When writing Terraform modules, naming resources and data sources this
is a simple but powerful pattern. Here's why:
-
It's Clear and Predictable
Inside a module, naming a resourcethis
communicates that it's the main or only instance of its kind in that module. There's no need to name it something likeprimary
,main_storage_account
, ormy_static_app
. -
It Makes Modules More Reusable
When you name every resource descriptively within a module, you're tying that resource to a specific use case. But a module should be generic. By naming a resourcethis
, you're signaling that it could be anything — and you're keeping it flexible for reuse. -
It Reduces Noise
When someone is consuming a module and sees outputs likedata.azurerm_storage_account.this.id
, they immediately know: this is the one that module defines.
What Problem Does this
Solve?
Terraform doesn't require you to name things a certain way. But conventions like this
solve the problem of consistency and readability across a codebase. Just like how developers instinctively know what self
or this
means in other languages, Terraform users can quickly recognize what this
refers to.
It helps answer the question:
Which resource is this referring to?
And the answer is:
The one and only one defined in this module.
When Not to Use this
Like all conventions, this
works best when used appropriately. You should not use this
when:
- You're managing multiple resources of the same type in the same module.
- You need to distinguish between two similar resources (e.g.,
frontend_storage
vsbackend_storage
). - It would make debugging or reading the code less intuitive for new developers.
What do others think?
I thought I'd ask ChatGPT to find me links to similar opinions. Here's some of the research that ChatGPT did that supports the use of this
in Terraform modules:
-
Anton Babenko's Terraform Best Practices — A leading community guide that explicitly recommends using
this
as the label for a module’s main or only resource when no other descriptive name applies. -
Reddit: Usage of "this" in terraform — In this thread, HashiCorp engineer Martin Atkins (aka apparentlymart) explains that
this
is widely adopted as a convention for singleton resources, especially in reusable modules. He highlights its clarity and the lack of meaningful alternatives. -
Medium: DevOps Mojo - Terraform Best Practices — A practitioner’s post reiterating that
this
provides clean naming and supports reusable modules without making assumptions about the resource’s purpose.
Across these sources, there's a consistent message: this
helps authors write cleaner, more modular, and predictable infrastructure code. It mimics a convention that developers from other programming languages will already understand.
Final Thoughts
Using this
in Terraform modules is a small convention that pays off in clarity, simplicity, and consistency — especially when writing reusable modules. It's a nod to practices in object-oriented programming, and it makes modules feel more like black boxes with clean interfaces.
By adopting this
where appropriate, you're not just writing Terraform — you're writing maintainable infrastructure code.
Let’s Connect
If this post sparked any ideas, gave you food for thought, or you just want to chat more about Terraform best practices and how it looks like in practice in a large enterprise, I’d love to hear from you. Drop me a message on LinkedIn.
Thanks for reading!