Reputation: 10930
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 ""
Upvotes: 1
Views: 1946
Reputation: 74209
It sounds like you have a template which generates JSON but which sometimes generates an empty JSON object, and in that special case you'd prefer to leave this argument entirely unset rather than setting it to be literally "{}"
.
If so, I think the problem in what you tried is that you compared the string "{}"
with a real empty object {}
, which will always return false because strings and objects are not comparable. (As a general rule, ==
always returns false
for values of differing types.)
If you know that the empty object case will always be literally the string "{}"
, and never any other equivalent JSON such as "{ }"
(with a space between the braces), then you can write an expression quite similar to the one you wrote except to compare with that string instead:
parameters = (
data.template_file.policy_parameters_file.rendered != "{}" ?
data.template_file.policy_parameters_file.rendered :
null
)
If you want to be a little more robust and catch any valid encoding of a JSON object, you could write a slightly more elaborate expression like this:
parameters = (
length(jsondecode(data.template_file.policy_parameters_file.rendered)) > 0 ?
data.template_file.policy_parameters_file.rendered :
null
)
This decodes the JSON back into a real object again and then tests to see if it has at least one attribute.
Please note that the template_file
data source is deprecated for anyone using Terraform v0.12.0 or later. While moving away from that should not be necessary to get a working result as described above, I would suggest also planning to migrate to the templatefile
function in future, because it is the one that will be supported on an ongoing basis and is integrated properly into the Terraform language so it can avoid various weird quirks that come from the template_file
data source being implemented in an external plugin.
Upvotes: 2