Bird
Raised Fist0
Terraformcloud~5 mins

Numeric functions (min, max, ceil) in Terraform - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you need to compare numbers or round them up when setting infrastructure values. Terraform has simple functions to find the smallest, largest, or round numbers up to the nearest whole number. These help make your infrastructure settings smart and flexible.
When you want to pick the smallest number from a list of resource limits to avoid over-provisioning.
When you need to select the largest number from multiple inputs to ensure enough capacity.
When you want to round up a fractional number to the next whole number for instance counts.
When calculating storage sizes that must be whole numbers but inputs might be decimals.
When setting thresholds or limits that depend on dynamic calculations and need safe rounding.
Config File - main.tf
main.tf
variable "cpu_limits" {
  type    = list(number)
  default = [1.5, 2.3, 1.8]
}

variable "memory_limits" {
  type    = list(number)
  default = [512, 1024, 768]
}

output "min_cpu" {
  value = min(var.cpu_limits...)
}

output "max_memory" {
  value = max(var.memory_limits...)
}

output "rounded_cpu" {
  value = ceil(2.3)
}

variable "cpu_limits": List of CPU values to compare.

variable "memory_limits": List of memory values to compare.

output "min_cpu": Shows the smallest CPU value from the list using min.

output "max_memory": Shows the largest memory value from the list using max.

output "rounded_cpu": Rounds up the number 2.3 to 3 using ceil.

Commands
Initialize the Terraform working directory to download providers and prepare for deployment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/random... - Installing hashicorp/random v3.4.3... - Installed hashicorp/random v3.4.3 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
Apply the Terraform configuration to see the output values for min, max, and ceil functions.
Terminal
terraform apply -auto-approve
Expected OutputExpected
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: # output.min_cpu will be created # output.max_memory will be created # output.rounded_cpu will be created Plan: 0 to add, 0 to change, 0 to destroy. Changes to Outputs: + max_memory = 1024 + min_cpu = 1.5 + rounded_cpu = 3 Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
→
-auto-approve - Automatically approve the apply step without manual confirmation
Show the smallest CPU value calculated by the min function.
Terminal
terraform output min_cpu
Expected OutputExpected
1.5
Show the largest memory value calculated by the max function.
Terminal
terraform output max_memory
Expected OutputExpected
1024
Show the CPU value rounded up by the ceil function.
Terminal
terraform output rounded_cpu
Expected OutputExpected
3
Key Concept

If you remember nothing else from this pattern, remember: min picks the smallest number, max picks the largest, and ceil rounds numbers up to the next whole number.

Common Mistakes
Passing a list directly to min or max without unpacking it with ...
Terraform expects individual arguments for min and max, not a list, so it errors.
Use the spread operator ... to unpack the list when calling min or max, like min(var.list...).
Using ceil on a negative number expecting it to round down
ceil always rounds up, so negative numbers become less negative, not down.
Use floor to round down if that is the intended behavior.
Summary
Use min and max functions with the ... operator to find smallest or largest values from lists.
Use ceil to round decimal numbers up to the nearest whole number for resource counts.
Run terraform apply to see output values and terraform output to query them individually.

Practice

(1/5)
1. What does the Terraform function min(3, 7, 2) return?
easy
A. Error
B. 3
C. 7
D. 2

Solution

  1. Step 1: Understand the min function

    The min function returns the smallest number from the given list.
  2. Step 2: Compare the numbers 3, 7, and 2

    Among 3, 7, and 2, the smallest number is 2.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    min(3,7,2) = 2 [OK]
Hint: min returns the smallest number from the list [OK]
Common Mistakes:
  • Confusing min with max
  • Expecting average instead of minimum
  • Using ceil instead of min
2. Which of the following is the correct syntax to use the ceil function in Terraform?
easy
A. ceil(4.3)
B. ceil 4.3
C. ceil[4.3]
D. ceil{4.3}

Solution

  1. Step 1: Recall Terraform function syntax

    Terraform functions use parentheses to enclose arguments, like function_name(argument).
  2. Step 2: Check each option

    Only ceil(4.3) uses correct function call syntax.
  3. Final Answer:

    ceil(4.3) -> Option A
  4. Quick Check:

    Function calls use parentheses [OK]
Hint: Terraform functions always use parentheses for arguments [OK]
Common Mistakes:
  • Using spaces instead of parentheses
  • Using brackets or braces instead of parentheses
  • Missing parentheses
3. What is the output of this Terraform expression?
max(5, ceil(3.2), min(4, 2))
medium
A. 2
B. 5
C. 3
D. 4

Solution

  1. Step 1: Evaluate inner functions

    ceil(3.2) rounds 3.2 up to 4.
    min(4, 2) returns 2.
  2. Step 2: Evaluate max function

    Now the expression is max(5, 4, 2). The largest number is 5.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    max(5,4,2) = 5 [OK]
Hint: Evaluate inner functions first, then apply max [OK]
Common Mistakes:
  • Ignoring nested function calls
  • Mixing up min and max
  • Forgetting ceil rounds up
4. Identify the error in this Terraform expression:
ceil(5.7, 2)
medium
A. Invalid function name
B. Missing parentheses
C. Too many arguments for ceil function
D. ceil cannot be used with decimals

Solution

  1. Step 1: Check the ceil function signature

    The ceil function takes exactly one argument, a decimal number.
  2. Step 2: Analyze the given expression

    The expression ceil(5.7, 2) has two arguments, which is invalid.
  3. Final Answer:

    Too many arguments for ceil function -> Option C
  4. Quick Check:

    ceil expects one argument [OK]
Hint: ceil takes only one decimal argument [OK]
Common Mistakes:
  • Passing multiple arguments
  • Confusing ceil with round
  • Using wrong argument types
5. You want to find the smallest whole number greater than or equal to the minimum of 3.4, 2.1, and 5.9 in Terraform. Which expression achieves this?
hard
A. ceil(min(3.4, 2.1, 5.9))
B. min(ceil(3.4, 2.1, 5.9))
C. max(min(3.4, 2.1, 5.9))
D. ceil(max(3.4, 2.1, 5.9))

Solution

  1. Step 1: Find the minimum of the numbers

    min(3.4, 2.1, 5.9) returns 2.1, the smallest number.
  2. Step 2: Apply ceil to round up to the nearest whole number

    ceil(2.1) rounds 2.1 up to 3.
  3. Final Answer:

    ceil(min(3.4, 2.1, 5.9)) -> Option A
  4. Quick Check:

    min=2.1, ceil(2.1)=3 [OK]
Hint: Apply min first, then ceil to round up [OK]
Common Mistakes:
  • Applying ceil before min
  • Using max instead of min
  • Confusing order of functions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard