How to check the value is present in list or not using terraform 0.13.5?
Asked Answered
S

1

11

I need to check the value that exists in a variable or not and based on that I need to create resources. If value_list doesn't have these values('abc','def','ghi') it should not create the resource.

What I'm trying here is:

  1. Converting the string variable to list
  2. Check that list is having values 'abc' or 'def' or 'ghi'. If value_list contains any one of the values then proceed with the next steps to create resources.
  3. If value_list doesn't have these values('abc','def','ghi') it should not create the resource.

variables.tf

variable "value_list" {
    default = "abc,def,ghi"
    type= string
}

resource.tf

resource "azurerm_kubernetes_cluster_node_pool" "user" {
  value_list = ${split(",", var.value_list)}
  count = "${contains(value_list,"abc") ? 1 : 0 || contains(value_list,"def") ? 1 : 0 || contains(value_list,"ghi") ? 1 : 0 
}

Error:

This character is not used within the language. Expected the start of an expression, but found an invalid expression token.

How to check if the value_list is having the desired value or not?

Saturant answered 3/3, 2021 at 8:58 Comment(1)
Could you tweak the code that you have in your question to form a minimal reproducible example please? Right now you have bits that make that unrunnable and it's hard to know if that's contributing to your error or where else the error lies.Speedboat
N
12

Terraform has functions that can help with that:

It looks like you are using contains, but in a strange way, if you need to split something you can do it in a local that way it is available to multiple resources, also the expression in your count does not look right you might want to look at the documentation for that:
https://www.terraform.io/docs/language/meta-arguments/count.html#using-expressions-in-count

Here is a sample usage:

variable "value_list" {
  default = "abc,def,ghi"
  type    = string
}

locals {
  vlist = split(",", var.value_list)
}

resource "null_resource" "test_abc" {
  count = contains(local.vlist, "abc") ? 1 : 0

  provisioner "local-exec" {
    command = "echo FOUND;"
  }
}

resource "null_resource" "test_xyz" {
  count = contains(local.vlist, "xyz") ? 1 : 0

  provisioner "local-exec" {
    command = "echo FOUND;"
  }
}

resource "null_resource" "test_abc_or_def" {
  count = (contains(local.vlist, "abc") || contains(local.vlist, "def")) ? 1 : 0

  provisioner "local-exec" {
    command = "echo FOUND;"
  }
}

See the count in that last resource:
count = (contains(local.vlist, "abc") || contains(local.vlist, "def")) ? 1 : 0

that is a conditional expression in the format:
<CONDITION> ? <TRUE VAL> : <FALSE VAL>

the condition is what looks strange in your sample code, you can have as many or in your condition as you want but don't mix the values there

( vlist contains "abc" OR vlist contains "def" )

( contains(local.vlist, "abc") || contains(local.vlist, "def") )

Nabal answered 3/3, 2021 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.