Terraspace global variables declarations

Hi :slight_smile:,

After reading the docs about adding global configs such as backend.tf, provider.tf and terraform.tf, we read there is also a possibility to use global locals such as described here: https://terraspace.cloud/docs/config/locals/

Is it possible to also declare global variables declarations in config/terraform/ directory as well?
We tried specifying a new variables.tf file which will hold all of our common global variable declarations but it wont recognize it.

Here is an example of what the structure looks like and what we want to achieve:

config/
  terraform/
    terraform.tf
    backend.tf
    provider.tf
    locals.tf
    variables.tf

Example of what we want in variables.tf:

variable "environment" {
  description = "Environment (qa/prod)"
  type            = string
  validation {
    condition           = contains(["prod", "qa"], var.environment)
    error_message = "Allowed values for environment are 'prod', or 'qa'."
  }
}

variable "route53_zone_name" {
  description = "Zone name"
  type            = string
}

variable "region" {
  type           = string
  description = "AWS Region"
}

The idea behind this is so we could define common variable declarations and essentially use terraspace to copy it to each running stack we have instead of duplicating these variables over multiple stacks.

Thanks for anyone trying to help.

Think defining config/terraform/vars.tf should achieve what you’re looking for. So the app/stacks/demo/variables.tf in the stack code doesn’t interfere with the config/terraform/vars.tf.

Here’s a debugging session:

$ cat config/terraform/vars.tf 
variable "foo" {
  default = "bar"
}
$ terraspace build demo
Building .terraspace-cache/us-west-2/dev/stacks/demo
Built in .terraspace-cache/us-west-2/dev/stacks/demo
$ cat .terraspace-cache/us-west-2/dev/stacks/demo/vars.tf
variable "foo" {
  default = "bar"
}
$ cat .terraspace-cache/us-west-2/dev/stacks/demo/variables.tf 
variable "acl" {
  description = "The canned ACL to apply. Defaults to 'private'."
  type        = string
  default     = "private"
}
$ 

Then to set the variables with tfvar files, there are a few ways:

config/terraform/terraform.tfvars

Docs: https://terraspace.cloud/docs/config/tfvars/

Also,

config/terraform/tfvars/base.tfvars
config/terraform/tfvars/dev.tfvars

Docs: https://terraspace.cloud/docs/tfvars/full-layering/#project-level-and-stack-level-layering

Related:

Will consider additional PRs for docs improvements. Thanks.

1 Like

Awesome! We will definitely try this out.
Thank you for the explanation and details!