Is it possible to display output conditionally?

I would like to be able to display output conditionally, along the lines of:

variable "vue_enabled" {
 description = "Set to true to enable building a vue queue"
 default = false
}

output "vue_queue" {
    value = "${var.vue_enabled ? aws_sqs_queue.vue_queue.id : ""}"
}

Where the vue_queue output gives the resource id if it exists, or and empty string (or not all) if not.

Is this possible in Terraform?

Thanks!

1 Like

What you wrote here should work, indeed. Though given your phrasing about “displaying” the output, I think you’re talking about outputs in a root module and about how they are rendered to the terminal at the end of terraform apply.

With a configuration like you showed here, terraform apply would still mention this output value, but would show it as having an empty string as its value.

If you are using Terraform 0.12 then I believe you could hide it altogether by setting it to null conditionally, like this:

output "vue_queue" {
    value = var.vue_enabled ? aws_sqs_queue.vue_queue.id : null
}

With that said, hiding output values from the display is not really an intentional feature right now, so whether the above works may depend on the version of Terraform you are running and may change in future versions of Terraform.

1 Like