Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform.tfvars only for composition? #28

Open
dominikbraun opened this issue Oct 11, 2021 · 2 comments
Open

terraform.tfvars only for composition? #28

dominikbraun opened this issue Oct 11, 2021 · 2 comments

Comments

@dominikbraun
Copy link

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?

gitbook-com bot pushed a commit that referenced this issue Feb 12, 2022
@djaboxx
Copy link

djaboxx commented Mar 27, 2022

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
Copy link

"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.

gitbook-com bot pushed a commit that referenced this issue Sep 7, 2022
gitbook-com bot pushed a commit that referenced this issue Sep 20, 2022
gitbook-com bot pushed a commit that referenced this issue Nov 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants