Introduction
This article demonstrates how to use Terraform's local values and conditional logic for several common use cases. In infrastructure-as-code workflows, conditional logic allows you to configure dynamic values based on specific conditions, which simplifies your Terraform configurations.
Procedure
The following examples show how to apply locals and conditional logic to manage different aspects of your infrastructure, such as configuration settings and dynamic behavior.
Example 1: Set Values Based on Resource Size
You can determine the number of CPUs for a resource based on a size input variable. This allows for dynamic resource allocation.
# Determines the number of CPUs based on resource size input
locals {
cpu_count = var.resource_size == "large" ? 16 : var.resource_size == "medium" ? 8 : 4
}
output "cpu_count_output" {
description = "The number of CPUs assigned based on the resource size."
value = local.cpu_count
}
# Variable declaration
variable "resource_size" {
description = "Defines the resource size (e.g., large, medium, small)."
type = string
}The local.cpu_count value sets the number of CPUs based on the var.resource_size input. If the value is "large", it assigns 16 CPUs. If the value is "medium", it assigns 8. Otherwise, it defaults to 4.
Example 2: Enable Services Based on Availability Zones
You can enable a service only when it is deployed in specific availability zones.
# Conditional logic to enable a service if deployed in certain availability zones
locals {
is_service_enabled = contains(var.availability_zones, "us-west-1a") || contains(var.availability_zones, "us-west-1b") ? true : false
}
output "service_enabled_output" {
description = "Determines if the service is enabled based on availability zones."
value = local.is_service_enabled
}
# Variable declaration
variable "availability_zones" {
description = "List of availability zones to deploy resources."
type = list(string)
}The local.is_service_enabled value checks whether "us-west-1a" or "us-west-1b" are present in the var.availability_zones list. If either zone is found, the service is enabled (true); otherwise, it is disabled (false).
Example 3: Calculate Cost Based on Resource Type
You can calculate a cost value based on the resource type, which is useful for budgeting or resource allocation.
# Conditional logic for cost calculation based on resource type
locals {
resource_cost = var.resource_type == "compute" ? 100 : var.resource_type == "storage" ? 50 : 25
}
output "resource_cost_output" {
description = "The calculated cost for the resource based on its type."
value = local.resource_cost
}
# Variable declaration
variable "resource_type" {
description = "Specifies the type of resource (e.g., compute, storage, network)."
type = string
}The local.resource_cost value assigns a cost of 100 for "compute" resources, 50 for "storage" resources, and defaults to 25 for all other types.
Example 4: Toggle a Feature Based on User Input
You can toggle a feature on or off dynamically based on a boolean input variable.
# Toggle a feature on or off based on user input
locals {
feature_toggle = var.enable_feature ? "Feature Enabled" : "Feature Disabled"
}
output "feature_toggle_status" {
description = "Displays the status of a feature toggle based on user input."
value = local.feature_toggle
}
# Variable declaration
variable "enable_feature" {
description = "A boolean flag to enable or disable a feature."
type = bool
}The local.feature_toggle value returns a status message based on the boolean var.enable_feature input.
Example 5: Set Resource Count Based on Scaling Strategy
You can determine the number of resources to provision based on a chosen scaling strategy.
# Determine resource count based on scaling strategy
locals {
resource_count = var.scaling_strategy == "aggressive" ? 10 : var.scaling_strategy == "moderate" ? 5 : 2
}
output "resource_count_output" {
description = "The number of resources to provision based on scaling strategy."
value = local.resource_count
}
# Variable declaration
variable "scaling_strategy" {
description = "Defines the scaling strategy (e.g., aggressive, moderate, conservative)."
type = string
}The local.resource_count value sets the number of resources to 10 for an "aggressive" strategy, 5 for "moderate", and defaults to 2 for any other strategy.
Example 6: Configure a Path Based on Operating System
You can set a file path dynamically based on the target operating system.
# Conditional logic to set a file path based on the operating system
locals {
file_path = var.os_type == "windows" ? "C:\\app\\config" : "/etc/app/config"
}
output "file_path_output" {
description = "The file path set based on the operating system."
value = local.file_path
}
# Variable declaration
variable "os_type" {
description = "Specifies the operating system (e.g., windows, linux)."
type = string
}The local.file_path value sets the path to "C:\\app\\config" if var.os_type is "windows". Otherwise, it uses the Linux-style path "/etc/app/config".
Example 7: Check Conditions Based on Multiple Inputs
You can require an additional check based on the values of two separate variables.
# Additional check based on two variables
locals {
requires_additional_check = var.resource_type == "compute" && var.resource_zone == "zone-1" ? false : true
}
output "requires_additional_check" {
description = "Indicates if additional checks are required based on the resource type and zone."
value = local.requires_additional_check
}
# Variable declarations
variable "resource_type" {
description = "Specifies the type of resource (e.g., compute, storage)."
type = string
}
variable "resource_zone" {
description = "Specifies the resource zone (e.g., zone-1, zone-2)."
type = string
}This logic checks two conditions. If var.resource_type is "compute" and var.resource_zone is "zone-1", no additional checks are required (false). Otherwise, additional checks are required (true).
Example 8: Use Negation Logic for General Conditions
You can use negation logic to check if an input is not a specific value.
# Negation logic for checking if the input is not a specific value
locals {
is_not_compute = !(var.resource_type == "compute") ? true : false
}
output "is_not_compute" {
description = "Indicates whether the resource type is not 'compute'."
value = local.is_not_compute
}
# Variable declaration
variable "resource_type" {
description = "Specifies the type of resource (e.g., compute, storage)."
type = string
}This logic negates the condition to check if the resource type is not "compute". If var.resource_type is anything other than "compute", the result is true.
Additional Information
Using locals and conditional logic in Terraform allows for more flexible and maintainable configurations by dynamically adjusting values based on various inputs. For more details on conditional logic, refer to the Terraform Conditional Expressions documentation.