0
votes

I am going through this article and trying to understand the benefit of Terraform Workspaces when I use Azure Blob Storage for state management.

https://danielwertheim.se/terraform-workspaces-and-remote-state-in-azure/

Since I am using Azure Blob Storage for state management, I can use a variable to use (switch to) a different container for each of the environments (such as DEV or PROD) - pointed below:

enter image description here

What additional value terraform workspace adds that I cannot do with a simple Azure Blob state with dedicated container_name per each environment (DEV, PROD)?

1

1 Answers

1
votes

For one you can't set a variable in the terraform backend. So if you want to reuse the same module without different terraform blocks, you would want to switch to a workspace.

Some people will use a single storage account for all their state. Use the key to define the project. The backend will take care of keying the state into a separate location based on the workspace name.

This is descent for smallish projects when driving the terraform command manually. You can also reference terraform.workspace in your code to have conditionals.

The biggest downside is dealing with authentication and variables. If you are using different subscriptions and logged in with the state of another subscription you get terrible news that everything needs replaced. So situational awareness needs to be heighten.

Tracking the values for input needs to be stored somewhere. When I used to do this, I ended up storing the variables as locals with a conditional on the workspace name.

▶ cat .\main.tf
variable "environment" {
  type = string
}

terraform {
  backend "azurerm" {
    resource_group_name  = "tfstate"
    storage_account_name = "account"
    container_name       = var.environment
    key                  = "terraform.tfstate"
  }
}

~\projects\test\t8                                                                                     ◷ 9:32:49 AM
▶ terraform init

Initializing the backend...

Error: Variables not allowed

  on main.tf line 9, in terraform:
   9:     container_name       = var.environment

Variables may not be used here.