My Terrform code would like below. Here my parameters file some time have values and most of the time would be an empty JSON {}
resource "azurerm_policy_assignment" "example" {
name = "example-policy-assignment"
scope = azurerm_resource_group.example.id
policy_definition_id = azurerm_policy_definition.example.id
description = "Policy Assignment created via an Acceptance Test"
display_name = "My Example Policy Assignment"
parameters = "${data.template_file.policy_parameters_file.rendered}"
So I need to do some validation to check if my parameter file is empty json then I need to assign an empty string.
I tried like below but its not working
"${data.template_file.policy_parameters_file.rendered}" == {} ? "" : "${data.template_file.policy_parameters_file.rendered}"
This validation is needed as the state files were already having value for parameter as “”. Now if i ran the terraform plan it showing as
parameters = jsonencode({}) # forces replacement
which makes force replacement.
If i hardcode the parameter to “” all goes well. So to handle this i need to find a way to handle the validation of empty json in the parameter file. If the json is empty I need to pass as “”