Hello,
I am trying create a number of resources using for_each and a map of objects.
However when I apply terraform I get an invalid context errror for each.value.
Steps:
1- read the content of repos.json file and run jsondecode on it
2- extract the json object containing data and assign to reposmap variable
3- call the module that generates the repositories and pass to it reposmap variable
repos.json
{
“repos”: {
“test_repo1”: {
“key”: “test_repo1”,
“name”: “test_repo1”,
“package_type”: “docker”
},
“test_repo2”: {
“key”: “test_repo2”,
“name”: “test_repo2”,
“package_type”: “docker”
}
}
}
main.tf
locals {
reposdata = jsondecode(file("${path.module}/repos.json"))
}
module “artifactory” {
source = “./test”
reposmap = local.reposdata.repos
module artifactory
artifactory/variable.tf
variable “reposmap” {
type = map(object({
key = string
name = string
package_type = string
}))
}
Here is where defining multiple instances of repo using for_each
resource “artifactory_local_repository” “createrepos” {
for_each = var.reposmap
key = each.value.key
name = each.value.name
package_type = each.value.package_type
}
Error: each.value cannot be used in this context
on artifactory/test_repo.tf line 48, in resource “artifactory_local_repository” “createrepos”:
48: key = each.value.key
A reference to “each.value” has been used in a context in which it
unavailable, such as when the configuration no longer contains the value in
its “for_each” expression. Remove this reference to each.value in your
configuration to work around this error.
Error: each.value cannot be used in this context
Any input on this appreciated.