Hello!
I’m trying to output the password and policie from my data_json as below:
The idea is use the value from the output of module vault_test in docker module.
resource "vault_generic_endpoint" "vault_endpoint_test" {
for_each = try(var.vault_endpoint_test, {})
provider = vault.vault_test
depends_on = [vault_auth_backend.userpass_auth]
path = each.value.generic_endpoint_path
ignore_absent_fields = true
data_json = <<EOT
{
"policies": ["${each.value.generic_endpoint_policie}"],
"password": "${each.value.generic_endpoint_password}"
}
EOT
}
module vault_test output:
output "vault_test_pass" {
value = jsondecode(vault_generic_endpoint.vault_endpoint_test[*].data_json)["password"]
sensitive = true
}
I’m getting the error below
Error: Invalid function argument
on modules/vault_test/output.tf line 12, in output "vault_env_vault_pass":
12: value = jsondecode(vault_generic_endpoint.vault_endpoint_test[*].data_json)["password"]
├────────────────
│ vault_generic_endpoint.vault_endpoint_test is object with 3 attributes
Invalid value for "str" parameter: string required.
Error: Unsupported attribute
on modules/vault_test/output.tf line 12, in output "vault_env_vault_pass":
12: value = jsondecode(vault_generic_endpoint.vault_endpoint_test[*].data_json)["password"]
This object does not have an attribute named "data_json".
I tried this as well.
│ on modules/vault_test/output.tf line 12, in output "vault_env_vault_pass":
│ 12: value = jsondecode(vault_generic_endpoint.vault_endpoint_test.data_json)["password"]
│
│ Because vault_generic_endpoint.vault_endpoint_test has "for_each" set, its attributes must be accessed on specific instances.
│
│ For example, to correlate with indices of a referring resource, use:
│ vault_generic_endpoint.vault_endpoint_test[each.key]
map(object) used by for_each
vault_endpoint_test = {
"test1" = {
endpoint_path = "auth/userpass/users/acctest1"
endpoint_policie = "acc-test"
endpoint_password = "passtest1"
}
"test2" = {
endpoint_path = "auth/userpass/users/acctest2"
endpoint_policie = "acc-test2"
endpoint_password = "passtest2"
}
"test3" = {
endpoint_path = "auth/userpass/users/acctest3"
endpoint_policie = "acc-test3"
endpoint_password = "passtes3"
}
}
The output will be used as input in a different module (docker) to fill:
resource "docker_container" "my_app" { #create 3 containers (test1, test2 and test 3
for_each = try(var.image_map, {})
name = each.key
image = each.value.image
env = [
"VAULT_USERNAME=${value_from_vault_module}", #same as police
"VAULT_PASSWORD=${value_from_vault_module}"
]
....
}
I’ve tried a lot of different ways without success.
What would be the best approach for this scenario ?
Thanks