1

I have the following JSON file and using it while creating users in terraform

{
    "[email protected]" : [
        {
        "name" : "abc",
        "description" : 100,
        },        
        {
        "name" : "efg",
        "description" : 100
        },        
        {
        "name" : "wer",
        "description" : 100
        }
    ],
    "[email protected]" : [
        {
        "name" : "xyz",
        "description" : 100
        },
        {
        "name" :  "qwe",
        "description" : 100        
        }
    ]
}

I tried with a few things but it errors out ... while usin in the resource creation. I'm unable to figure out on how to grab the keys, values

resource "oci_identity_user" "users" {
    count = keys(local.users)[*] 
    
    compartment_id = var.tenancy_ocid
    description = count.index[description]
    name = count.index[name]
    email = count.index[email]
}


Error: Invalid index
│
│   on local.tf line 14, in locals:
│   14:   services = keys(local.users[1])
│     ├────────────────
│     │ local.odsc_serivices is object with 2 attributes
│

1 Answer 1

1

Since you have two lists in your users object, you have to concatenate them. You can do this with combining flatten and values functions. Moreover, I suggest using for_each instead of count:

resource "oci_identity_user" "users" {
  for_each = { for index, value in flatten(values(local.users)) : index => value }

  compartment_id = var.tenancy_ocid
  description    = each.value.name
  name           = each.value.description
  email          = each.value.email
}

Update:

resource "oci_identity_user" "users" {
  for_each = {
    for index, value in flatten([
      for email, value in local.users : [
        for user in value : { email : email, name : user.name, description : user.description }
      ]
    ]) : index => value
  }

  compartment_id = var.tenancy_ocid
  description    = each.value.name
  name           = each.value.description
  email          = each.value.email
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Ervin Szilagyi. How can we get "users_set1" and "users_set2". Is that something each.index or each.index[0]
I apologize. I updated the JSON in the question. Email-id is on the top and I need to grab the email from the top.
Updated my answer. Please don't modify your question. Instead post a new one, so other people will be able to see it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.