'How to enable terraform module only with a specific workspace

I define a provider containing modules.

I can call terraform apply at the provider level with two different workspace:

workspace list
  default
  wk-p1
* wk-p2

I would like the following module only to be launch when I use the workspace wk-1:

module "sync" {
  source = "./../test-modules/sync"

  workspace="${local.workspace}"
  entity = "${local.entity}"
}

I would like something like this:

module "sync" {
  if ("${local.workspace}" == "wk-p2") {
    source = "./../test-modules/sync"

    workspace="${local.workspace}"
    entity = "${local.entity}"
  }
}

Do you have any idea?



Solution 1:[1]

For this you could use the terraform.workspace variable in conjuction with the count directive

 count = (terraform.workspace == "wk-p2") ? 1 : 0

This way when the workspace is wk-p2, terraform will create an instance of the module.

On the other hand if it evaluates to false it will create 0 instances of the modules, meaning it will not create anything

Solution 2:[2]

Maybe try with "count". Something like:

count = terraform.workspace == "default" ? 1 : 0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tolis Gerodimos
Solution 2 blazejpoland