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:
value_list
contains any one of the values then proceed with the next steps to create resources.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?
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")
)