Home > Back-end >  How to use terraform workspace interpolation to separate resources creation?
How to use terraform workspace interpolation to separate resources creation?

Time:01-08

Lets suppose I have dev, uat and prod environment. I wan't to have some modules to be deployed in the dev environment but not in other environment.

I want to put a condition based on the workspace I have but can't figure it out how. Any recommendation would be appreciated.

I tried to use $(terraform.workspace) to select 'dev' enviroment but wasn't working.

count = $(terraform.workspace) == "dev" ? 1 : 0 and it says 

which resulted in:

This character is not used within the language.

CodePudding user response:

You don't need to use $ sign.

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

CodePudding user response:

There are two different styles and two different logics to write this condition.

Different Styles

  • If terraform.workspace is equal to "dev" then create one instance else zero instance.
count = "${terraform.workspace}" == "dev" ? 1 : 0
count = terraform.workspace == "dev" ? 1 : 0

Another logic

  • If terraform.workspace is not equal to "dev" then create zero instance else one instance.
count = "${terraform.workspace}" != "dev" ? 0 : 1
count = terraform.workspace != "dev" ? 0 : 1
  • Related