Search code examples
terraform

How to merge tag resource


Does anyone know how to merge tags? I am working on code in Azure that is used in multiple environments.

my main.tf file contains:

tags = {
  application     =  "Myapp"
  BU              =  "IT"
  DeploymentMode  = "terraform"
  Environment     =  "Production"
}

4 environments in all(Production, Staging, Development, Test)

terraform login
terraform workspace list

Production
Staging
Development
Test

I want to apply the changes to the Test workspace.

terraform workspace select Test 
terraform validate
terraform init

all work with no issues.

I then run terraform plan Part of the code returned includes:

~tags    = {
  ~ Environment = "Test" -> "Production"}

I suspect this means changing the environment. Which isn't what I want. My research tells me I need to merge the tags. Not sure how to do this.


Solution

  • The shortest answer to this is to use terraform.workspace interpolation for the Environment tag, i.e:

    tags = {
      application     =  "Myapp"
      BU              =  "IT"
      DeploymentMode  = "terraform"
      Environment     =  terraform.workspace
    }
    

    That should let you know then if you are in the wrong workspace.