antonbabenko / terraform-best-practices

Terraform Best Practices free ebook translated into 🇬🇧🇦🇪🇧🇦🇧🇷🇫🇷🇬🇪🇩🇪🇬🇷🇮🇱🇮🇳🇮🇩🇮🇹🇰🇷🇵🇱🇷🇴🇨🇳🇪🇸🇹🇷🇺🇦🇵🇰
https://www.terraform-best-practices.com/
Other
2.06k stars 431 forks source link

`terraform.tfvars` only for composition? #28

Open dominikbraun opened 3 years ago

dominikbraun commented 3 years ago

First of all, thanks for providing this great book!

The Getting started with structuring of Terraform configurations section states that terraform.tfvars files should exclusively be used with composition:

terraform.tfvars should not be used anywhere except composition.

In turn, the Composition section defines composition as follows:

Composition is a collection of infrastructure modules, [...]

However, the example for small-size infrastructures doesn't have any module but nevertheless uses a terraform.tfvars file.

I'm wondering who's wrong here - the code structure section or the code example?

djaboxx commented 2 years ago

compostion is a strange word for a workspace. A workspace is composed of many modules; but, there's already a standard term for this... workspace. in the example of small-size infrastructures, it is odd that there's no modules being used; but, there doesn't necessarily have to be in order to be considered a workspace. terraform.tfvar should not be found in modules, and should instead be found only in workspaces. Since the small-size infrastructure is indeed a workspace, it's okay for there to be terraform.tfvar files. It should be noted that by using terraform.tfvars files you are making somewhat more difficult to reuse this code. You would more than likely benefit from reading these docs, https://www.terraform.io/cloud-docs/guides/recommended-practices/part1. This document lists using one workspace per environment per configuration, this is where code reusability for workspaces comes into play.

sanman1k98 commented 2 years ago

"Workspaces" are not "Workspaces"

I think that it is important to note that the term "workspace" refers to very different things depending on if you are using it in the context of Terraform Cloud or Terraform CLI.

In this case, a Terraform Cloud Workspace is "used to manage a collection of infrastructure," which is, in essence, what a "composition" does. This equivalent to a working directory in the CLI, which is probably how you're used to managing different collections of infrastructure with a local machine using different directories for each.

Terraform CLI Workspaces are used to switch between multiple distinct instances of the same configuration, that is, using multiple state files with the same backend. This is convenient because you don't have to create a separate configuration with its own backend just to create a slightly different version of your infrastructure, for example if you wanted to test out a change to your configuration but you didn't want to affect your existing infrastructure. The docs expound on this and offers suggestions on whether or not to use the feature depending on the situation, but I digress.

Compositions?

But going back to the question of where to use a .tfvars file, the Terraform docs say you can supply values to variables in your top-level configuration (called the root module which is also the directory you run Terraform commands) using a .tfvars file. I recommend checking out how root and child modules are used here.

I think the problem with the term "composition" is that it is defined with a specific hierarchy and encapsulation rules. Going by that definition, technically the small-sized infrastructure example isn't a composition because it doesn't call any modules, let alone modules that call other modules. It is, however, the root module which in this case makes up the complete Terraform configuration.

terraform.tfvars should not be used anywhere except in the root of a composition

Perhaps specifying the above would be an improvement, but I think the real problem is with the idea of what a "composition" is.