Scalr
Scalr
June 1, 2022

Introducing Native Support for Terraform Variable Files in Scalr

By
Ryan Fee

Scalr has now added native support for variable files in the workspace configuration! A common theme among our customers is to create reusable code and one of the core concepts of that is being able to inject variables per workspace to allow for the reusable code. 

Previously, the way to do this was to pass in a shell variable and reference the file:

TF_CLI_ARGS_plan= -var-file=env/xxxx.tfvars

While this worked and will continue to work, the variable files are really part of the workspace configuration, which is why we have added the native support for it. Going forward, the best practice will be to add the .tfvar file(s) to the workspace configuration:

resource "scalr_workspace" "dev" {
  name = "manager-dev"
  environment_id = data.scalr_vcs_provider.manager.id
  vcs_provider_id = data.scalr_vcs_provider.manager.id
  working_directory = "module"
  var_files = ["common.tfvars", "dev.tfvars"]

  vcs_repo {
    identifier = "org/name"
    branch = "main"
  }
}

resource "scalr_workspace" "prod" {
  name = "manager-dev"
  environment_id = data.scalr_vcs_provider.manager.id
  vcs_provider_id = data.scalr_vcs_provider.manager.id
  working_directory = "module"
  var_files = ["common.tfvars", "prod.tfvars"]

  vcs_repo {
    identifier = "org/name"
    branch = "main"
  }
}

This now gives you the ability to add multiple files as part of the workspace configuration, and most importantly it is now part of the Scalr provider, which is the best practice in creating and maintaining workspaces. The ability to add files through the UI will follow very soon.

The combination of the Scalr module registry and variables in the workspace configuration now makes it even easier to structure your Terraform deployments at scale.

Note: While this blog references Terraform, everything mentioned in here also applies to OpenTofu. New to OpenTofu? It is a fork of Terraform 1.5.7 as a result of the license change from MPL to BUSL by HashiCorp. OpenTofu is an open-source alternative to Terraform that is governed by the Linux Foundation. All features available in Terraform 1.5.7 or earlier are also available in OpenTofu. Find out the history of OpenTofu here.

Start using the OpenTofu & Terraform platform of the future.

A screenshot of the modules page in the Scalr Platform