Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting minimum value by output block

I need to get minimum value in output block using variable type number with list

Terraform:

variable "ports" {
   type = list(number)
   default = [12,16,23]
}

output "ports_count" {
  value = length(var.ports)
}

output "ports_min" {
  value = min(var.ports)
}

Output

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

│ Error: Invalid function argument
│ 
│   on main.tf line 11, in output "ports_min":
│   11:   value = min(var.ports)
│     ├────────────────
│     │ var.ports is a list of number, known only after apply
│ 
│ Invalid value for "numbers" parameter: number required.

>Solution :

Since output will require the value to be known, I suggest using local variables:

locals {
  ports = [12,16,23]
}

Then in the outputs, you would do the following:

output "ports_min" {
  value = min(local.ports...)
}

This will output 12. The ... is function expansion operator [1].


[1] https://www.terraform.io/language/expressions/function-calls#expanding-function-arguments

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading