79147342

Date: 2024-11-01 08:29:44
Score: 0.5
Natty:
Report link

After digging around some more, I ended up adding a lifecycle precondition to my schema resource.

locals {
  schema     = file(var.schema_path)
  schemaJson = jsondecode(local.schema)
}
resource "confluent_schema" "schema" {
  depends_on   = [confluent_kafka_topic.topic]
  subject_name = "${confluent_kafka_topic.topic.topic_name}-value"
  format       = var.schema_format
  schema       = local.schema


  lifecycle {
    precondition {
      condition     = var.schema_format != "AVRO" || jsondecode(local.schema).type == "record"
      error_message = "Schema must be a valid AVRO schema. Key 'Type' must have value 'record'"
    }
  }
}

Using a precondition means that an error will be thrown if the condition is not met. Unfortunatley, this block will only run during terraform plan and terraform apply, meaning that terraform validate will no longer run all the validation logic.


This is a workaround that will be fine for my use case, but the central question still stands:

Why does using other variables in a variable's validate block cause the condition to always evaluate to true?

I feel like I am still missing something. Either this is a bug in Terraform or I am missing something with how this functionality is intended to be used.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Thomas Bakken Moe