When working with Terraform, it's common to have multiple resources with similar configurations. Instead of duplicating code, you can use for_each loops to iterate over a collection of items and create resources with dynamic values. In this article, we will discuss how to use for_each loops with maps of objects in Terraform, using Azure backup policies as an example. This technique can help streamline your infrastructure as code and make it more maintainable. This teqnique we used in our previous article where we build Azure Recovery Services Vaul terraform module.

We will cover the following topics

  1. Using for_each loops in Terraform
  2. Defining a map of objects in variables.tf
  3. Implementing for_each with maps of objects in main.tf
  4. Benefits of using this approach

Prerequisites

  • Basic understanding of Terraform
  • Familiarity with Azure and backup policies

Using for_each loops in Terraform

Terraform's for_each loop allows you to iterate over a collection of items and create resources for each item. It is a powerful feature that simplifies resource creation when dealing with multiple similar items. It is particularly useful when combined with maps of objects, as you can dynamically create resources with different attributes based on the input.

Defining a map of objects in variables.tf

In our example, we will create Azure backup policies for an Azure Recovery Services Vault. First, we need to define a map of objects in variables.tf to store our backup policies:

 variable "backup_policies" {
  type = map(object({
    name                          = string
    frequency                     = string
    time                          = string
    retention_daily               = number
    retention_weekly              = number
    retention_monthly             = number
    retention_yearly              = number
    tags                          = map(string)
  }))
  description = "A map of backup policies to be created in the Recovery Vault."
}

This map variable allows us to define multiple backup policies with different configurations.

Implementing for_each with maps of objects in main.tf

Now that we have our backup policies defined, we can use a for_each loop in main.tf to create an azurerm_backup_policy_file_share resource for each policy:

resource "azurerm_backup_policy_file_share" "policy" {
  for_each = var.backup_policies

  name                = each.value.name
  resource_group_name = var.resource_group_name
  recovery_vault_name = azurerm_recovery_services_vault.example.name 

  # ... additional configuration ...
}

In this example, the for_each loop iterates over the backup_policies map, creating a resource for each policy with the specified attributes.

Benefits of using this approach

  • Using for_each loops with maps of objects in Terraform offers several advantages:
  • Code reusability: You can create multiple resources with similar configurations without duplicating code.
  • Improved maintainability: Changes to the resource configuration can be made in a single location, making it easier to maintain and update.
  • Flexibility: The map of objects allows for dynamic input, making it easy to customize resources based on different requirements.

Conclusion

In this article, we demonstrated how to use for_each loops with maps of objects in Terraform, using Azure backup policies as an example. This approach enables efficient resource creation, improves maintainability, and allows for greater customization. By leveraging these techniques, you can streamline your infrastructure as code and ensure