Обработка null с помощью for_each

#terraform

#terraform

Вопрос:

Как я могу обработать следующее, где variables переменная может быть нулевой

 locals {
  tf_variables  = (var.variables == null) ? null : jsondecode(var.variables)["variables"]
}

resource "tfe_variable" "this" {
  for_each = local.tf_variables
...
}
  

Я нажимаю

 Error: Invalid for_each argument

  on ....main.tf line 63, in resource "tfe_variable" "this":
  63:   for_each = local.tf_variables

The given "for_each" argument value is unsuitable: the given "for_each"
argument value is null. A map, or set of strings is allowed.
  

Ответ №1:

Вместо этого вы можете заменить null на пустой набор, список или карту.

Изменение вашего локального на this должно сработать:

 locals {
  tf_variables  = (var.variables == null) ? [] : jsondecode(var.variables)["variables"]
}
  

Кроме того, вам не нужно использовать jsondecode там, потому var.variables что уже должен быть сериализованный объект HCL.