Difference between revisions of "Terraform: for each"

From wikieduonline
Jump to navigation Jump to search
 
(19 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{lc}}
+
<code>for_each</code> [https://developer.hashicorp.com/terraform/language/meta-arguments/for_each ref]
for_each
 
  
 +
== Examples ==
 +
=== Official examples ===
 +
resource "azurerm_resource_group" "rg" {
 +
  for_each = {
 +
    a_group = "eastus"
 +
    another_group = "westus2"
 +
  }
 +
  name    = [[each.]]key
 +
  location = each.value
 +
}
 +
 +
 +
 +
resource "[[aws_ssm_parameter]]" "secret" {
 +
  [[for_each]]    = toset(var.secrets)
 +
  name        = each.key
 +
  description = each.key
 +
  type        = "SecureString"
 +
  value      = "1"
 +
 +
  lifecycle {
 +
    ignore_changes = [value]
 +
  }
 +
}
 +
 +
 +
 +
resource "opsgenie_user" "main" {
 +
  [[for_each]] = [[local.]]myusers
 +
 +
  username  = "${each.key}@yourdomain.com"
 +
  full_name = [[each.]]value.name
 +
  role      = each.value.role
 +
  timezone  = each.value.timezone
 +
}
 +
 +
== Errors ==
 +
* <code>[[Error: Unexpected resource instance key]]</code>
  
 
== Related ==
 
== Related ==
* [[for]]
+
* <code>[[Terraform expression: for|for]]</code>
 +
* <code>[[Terraform meta argument: count|count]]</code>
 +
* [[Terraform best practices]]
 +
* [[meta arguments]]
  
 
== See also ==
 
== See also ==
 +
* {{for_each}}
 +
* {{for}}
 
* {{tf functions}}
 
* {{tf functions}}
  
 
[[Category:Terraform]]
 
[[Category:Terraform]]

Latest revision as of 06:50, 12 June 2024

for_each ref

Examples[edit]

Official examples[edit]

resource "azurerm_resource_group" "rg" {
  for_each = {
    a_group = "eastus"
    another_group = "westus2"
  }
  name     = each.key
  location = each.value
}


resource "aws_ssm_parameter" "secret" {
  for_each    = toset(var.secrets)
  name        = each.key
  description = each.key
  type        = "SecureString"
  value       = "1"

  lifecycle {
    ignore_changes = [value]
  }
}


resource "opsgenie_user" "main" {
  for_each = local.myusers 

  username  = "${each.key}@yourdomain.com"
  full_name = each.value.name
  role      = each.value.role
  timezone  = each.value.timezone
}

Errors[edit]

Related[edit]

See also[edit]

Advertising: