Loops and Iteration in HCL
Loops and iteration are fundamental programming constructs in HashiCorp Configuration Language (HCL). They enable you to repeat a set of instructions multiple times, making your Terraform configurations more dynamic and versatile. In this guide, we’ll explore how loops and iteration work in HCL, their syntax, and practical use cases.
Basic Syntax
In HCL, there are two primary ways to perform iteration: count-based loops and for-each loops.
Count-Based Loops
Count-based loops repeat a block of code a specified number of times. You can use the count
argument within resource or module blocks to control how many instances of that resource or module should be created.
resource "aws_instance" "example" {
count = 3
instance_type = "t2.micro"
ami = "ami-0c55b159cbfafe1f0"
subnet_id = aws_subnet.example.id
availability_zone = "us-west-2a"
}
In this example, three AWS instances of type “t2.micro” will be created, and the same configuration is repeated for each instance.
For-Each Loops
For-each loops, on the other hand, iterate over elements of a list or map. They allow you to create multiple instances with varying configurations.
variable "instance_types" {
type = list(string)
default = ["t2.micro", "t2.small", "t2.large"]
}
resource "aws_instance" "example" {
for_each = { for idx, instance_type in var.instance_types : idx => instance_type }
instance_type = each.value
ami = "ami-0c55b159cbfafe1f0"
subnet_id = aws_subnet.example.id
availability_zone = "us-west-2a"
}
In this example, an AWS instance is created for each item in the “instance_types” list. The instances will have different instance types based on the values in the list.
Conditional Loops
You can add conditions to your loops, making them conditional. For instance, you can iterate over a list and create resources only when a specific condition is met.
variable "environments" {
type = list(string)
default = ["development", "staging", "production"]
}
resource "aws_instance" "example" {
for_each = { for idx, env in var.environments : idx => env if env == "production" }
instance_type = "t2.micro"
ami = "ami-0c55b159cbfafe1f0"
subnet_id = aws_subnet.example.id
availability_zone = "us-west-2a"
}
In this example, AWS instances are only created for the “production” environment from the “environments” list.
Real-World Use Cases
Loops and iteration are invaluable in HCL when defining Terraform configurations. Some common use cases include:
1. Creating Multiple Resources
You can create multiple instances of resources, such as AWS EC2 instances, subnets, security groups, and more. This is especially useful when you need to replicate configurations.
2. Dynamic Resource Creation
Using loops, you can dynamically create resources with varying configurations, allowing you to adapt to changing requirements or environments.
3. Conditional Resource Creation
Loops can be used to conditionally create resources based on specific conditions or external variables, making your configurations more adaptive.
4. List and Map Iteration
For-each loops enable you to iterate over lists and maps, providing flexibility in defining resources and their properties.
Summary
Loops and iteration are essential components of HCL, allowing you to repeat configurations and create dynamic, adaptable Terraform resources. By understanding the syntax and use cases, you can efficiently manage and provision your infrastructure as code.