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

JDiMatteo picture JDiMatteo · Nov 11, 2017 · Viewed 13.3k times · Source

How do you check if a terraform string contains another string?

For example, I want to treat terraform workspaces with "tmp" in the name specially (e.g. allowing rds instances to be deleted without a snapshot), so something like this:

locals
{
  is_tmp = "${"tmp" in terraform.workspace}"
}

As far as I can tell, the substr interpolation function doesn't accomplish this.

Answer

JDiMatteo picture JDiMatteo · Nov 11, 2017

You can indirectly check for substrings using replace, e.g.

locals
{
  is_tmp = "${replace(terraform.workspace, "tmp", "") != terraform.workspace}"
}