Skip to main content
French version below

Terraform: for_each with a list in dynamic block

Below an example to loop on a list terraform type to create multiple dynamic terraform blocks within a terraform resource. It uses terraform content block documented here: https://www.terraform.io/docs/language/expressions/dynamic-blocks.html

  • In variables.tf file:
variable "list_to_iterate" {
    description = "A list to iterate on"
    type = list(string)
    default = []
}
  • In main.tf file:
resource "resource" "local_given_name" {
    dynamic "block" {
    for_each = var.list_to_iterate
        content {
        block_arg = local_given_name.value
        }
    } 
}

Terraform : "for_each" sur une liste dans un bloc "dynamic"

Ci-dessous un exemple de boucle. On utilise le bloc content de terraform, documenté ici : https://www.terraform.io/docs/language/expressions/dynamic-blocks.html

  • Dans le fichier variables.tf :
variable "list_to_iterate" {
    description = "A list to iterate on"
    type = list(string)
    default = []
}
  • Dans le fichier main.tf :
resource "resource" "local_given_name" {
    dynamic "block" {
    for_each = var.list_to_iterate
        content {
        block_arg = local_given_name.value
        }
    } 
}
Close Menu

OÙ NOUS TROUVER


PARIS

Bâtiment Crisco Duo
7 avenue de la Cristallerie
92310 Sèvres

NANTES

Centre d’affaires Euptouyou
4 rue Edith Piaf Immeuble Asturia C
44800 Saint-Herblain

TÉLÉPHONE

01 46 48 26 00

EMAIL

[email protected]