a.moussa
a.moussa

Reputation: 3287

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?

Upvotes: 1

Views: 1943

Answers (2)

Tolis Gerodimos
Tolis Gerodimos

Reputation: 4408

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

Upvotes: 4

blazejpoland
blazejpoland

Reputation: 21

Maybe try with "count". Something like:

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

Upvotes: 2

Related Questions