Conditional Expressions¶
Conditional expressions in Terraform are used to define conditional logic within your configurations. They allow you to make decisions or set values based on conditions. Conditional expressions are typically used to control whether resources are created or configured based on the evaluation of a condition.
The syntax for a conditional expression in Terraform is:
conditionis an expression that evaluates to eithertrueorfalse.true_valis the value that is returned if the condition istrue.false_valis the value that is returned if the condition isfalse.
Here are some common use cases and examples of how to use conditional expressions in Terraform:
Conditional Resource Creation Example¶
resource "aws_instance" "example" {
count = var.create_instance ? 1 : 0
ami = "ami-XXXXXXXXXXXXXXXXX"
instance_type = "t2.micro"
}
In this example, the count attribute of the aws_instance resource uses a conditional expression. If the create_instance variable is true, it creates one EC2 instance. If create_instance is false, it creates zero instances, effectively skipping resource creation.
Conditional Variable Assignment Example¶
variable "environment" {
description = "Environment type"
type = string
default = "development"
}
variable "production_subnet_cidr" {
description = "CIDR block for production subnet"
type = string
default = "10.0.1.0/24"
}
variable "development_subnet_cidr" {
description = "CIDR block for development subnet"
type = string
default = "10.0.2.0/24"
}
resource "aws_security_group" "example" {
name = "example-sg"
description = "Example security group"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = var.environment == "production" ? [var.production_subnet_cidr] : [var.development_subnet_cidr]
}
}
In this example, the locals block uses a conditional expression to assign a value to the subnet_cidr local variable based on the value of the environment variable. If environment is set to "production", it uses the production_subnet_cidr variable; otherwise, it uses the development_subnet_cidr variable.
Conditional Resource Configuration¶
resource "aws_security_group" "example" {
name = "example-sg"
description = "Example security group"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = var.enable_ssh ? ["0.0.0.0/0"] : []
}
}
In this example, the ingress block within the aws_security_group resource uses a conditional expression to control whether SSH access is allowed. If enable_ssh is true, it allows SSH traffic from any source ("0.0.0.0/0"); otherwise, it allows no inbound traffic.
Conditional expressions in Terraform provide a powerful way to make decisions and customize your infrastructure deployments based on various conditions and variables. They enhance the flexibility and reusability of your Terraform configurations.
Built-in Functions¶
Terraform is an infrastructure as code (IaC) tool that allows you to define and provision infrastructure resources in a declarative manner. Terraform provides a wide range of built-in functions that you can use within your configuration files (usually written in HashiCorp Configuration Language, or HCL) to manipulate and transform data. These functions help you perform various tasks when defining your infrastructure. Here are some commonly used built-in functions in Terraform:
concat(list1, list2, ...): Combines multiple lists into a single list.
variable "list1" {
type = list
default = ["a", "b"]
}
variable "list2" {
type = list
default = ["c", "d"]
}
output "combined_list" {
value = concat(var.list1, var.list2)
}
element(list, index): Returns the element at the specified index in a list.
variable "my_list" {
type = list
default = ["apple", "banana", "cherry"]
}
output "selected_element" {
value = element(var.my_list, 1) # Returns "banana"
}
length(list): Returns the number of elements in a list.
variable "my_list" {
type = list
default = ["apple", "banana", "cherry"]
}
output "list_length" {
value = length(var.my_list) # Returns 3
}
map(key, value): Creates a map from a list of keys and a list of values.
variable "keys" {
type = list
default = ["name", "age"]
}
variable "values" {
type = list
default = ["Alice", 30]
}
output "my_map" {
value = map(var.keys, var.values) # Returns {"name" = "Alice", "age" = 30}
}
lookup(map, key): Retrieves the value associated with a specific key in a map.
variable "my_map" {
type = map(string)
default = {"name" = "Alice", "age" = "30"}
}
output "value" {
value = lookup(var.my_map, "name") # Returns "Alice"
}
join(separator, list): Joins the elements of a list into a single string using the specified separator.
variable "my_list" {
type = list
default = ["apple", "banana", "cherry"]
}
output "joined_string" {
value = join(", ", var.my_list) # Returns "apple, banana, cherry"
}
These are just a few examples of the built-in functions available in Terraform. You can find more functions and detailed documentation in the official Terraform documentation, which is regularly updated to include new features and improvements