How to check if string contains a substring in terraform interpolation?

For terraform 0.12.xx apparently 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_tmp = length(regexall(".*tmp.*", terraform.workspace)) > 0
}

It also specifically says in the manual not to use “regex” but instead use regexall.

If the given pattern does not match at all, the regex raises an error. To test whether a given pattern matches a string, use regexall and test that the result has length greater than zero.

As stated above this is because you will actually get an exception error when you try to use it in the later versions of 0.12.xx that are out now when you run plan. This is how I found this out and why I posted the new answer back here.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)