Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use for_each in resource with array of objects #22522

Closed
EPinci opened this issue Aug 20, 2019 · 2 comments
Closed

Unable to use for_each in resource with array of objects #22522

EPinci opened this issue Aug 20, 2019 · 2 comments

Comments

@EPinci
Copy link

EPinci commented Aug 20, 2019

Current Terraform Version

Terraform v0.12.6
+ provider.azurerm v1.32.1
+ provider.random v2.1.2

Use-cases

I need to create a number of similar resources with different configuration based on an array of maps (or objects).
Basically I'm trying to use the for_each in resource in the same way that dynamics do but this is failing.

Attempted Solutions

I tried the following but it crashes with "panic: not a string".

var.spec.databases is an array of maps (can also be a more complex object).

resource "azurerm_sql_database" "sqldb" {
  for_each = var.spec.databases

  name                             = each.value.name
  resource_group_name              = "some_fixed_value"
  location                         = azurerm_resource_group.resource_group.location
  server_name                      = "some_fixed_value"
  edition                          = each.value.edition
  requested_service_objective_name = each.value.sku
}

Proposal

Allow the for_each to be used in reources the same way that dynamic do.
Also having the iterator will be nice.

@teamterraform
Copy link
Contributor

For the crash, this is a known issue with 0.12.6, and will be fixed in 0.12.7 #22279.

However, the way to get the behavior you're seeking is to use a map as for_each is designed:

{
  dev = { ... some dev settings ... },
  staging = {... some staging settings ... }
}

You would then be able to use the lookup function in your configuration on each.value. for_each does not support lists because it creates resources identified by the keys in the map (or set of strings, if one is used).

@ghost
Copy link

ghost commented Sep 20, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Sep 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants