PureDevOps Community

How to check if string contains substring in terraform

I would like to check my terraform workspace has a particular environment like dev, prod and then I will add certain values

locals
{
  is_dev = "${"dev" in terraform.workspace}"
}

For terraform doc you are suppose to use regexall to do this.

From the manual for terraform 0.12.XX: regexall() documentation

regexall can also be used to test whether a particular string matches a given pattern, by testing whether the length of the resulting list of matches is greater than zero.

Example from the manual:

> length(regexall("[a-z]+", "1234abcd5678efgh9"))
2

> length(regexall("[a-z]+", "123456789")) > 0
false

Example applied to your case in terraform 0.12.xx syntax should be something like:

locals
{
  is_dev = length(regexall(".*dev.*", terraform.workspace)) > 0
}