Home > Enterprise >  Terraform trying to replace existing resource when new user is added
Terraform trying to replace existing resource when new user is added

Time:01-21

i have this yaml file

team1:
   test@gmail,com: ${live_user_role}
   [email protected]: ${live_user_role} 
   [email protected]: ${live_admin_role}
 

terraform code

locals {
  render_membership = templatefile("${path.module}/teammembers.yaml",
    {
      live_user_role      = var.liveteam_user_role_id
      live_admin_role     = var.liveteam_admin_role_id
    }
  )

  membership_nested = yamldecode(local.render_membership)
  team_names        = keys(local.membership_nested)

  membership_flat = flatten(
    [
      for team_name in local.team_names : [
        for user_email, roles in local.membership_nested[team_name] : {
          team_name = team_name
          user_name = user_email
          roles     = [roles]
        }
      ]
    ]
  )
}


resource "squadcast_team_member" "membership" {
  for_each = { for i, v in local.membership_flat : i => v }
  team_id  = data.squadcast_team.teams[each.key].id
  user_id  = data.squadcast_user.users[each.key].id
  role_ids = each.value.roles
  lifecycle {
    create_before_destroy = true
  }
}

data "squadcast_team" "teams" {
  for_each = { for i, v in local.membership_flat : i => v }
  name     = each.value.team_name
}

data "squadcast_user" "users" {
  for_each = { for i, v in local.membership_flat : i => v }
  email    = each.value.user_name
}

Now when i add the new member in the list let's say like this:

team1:
   test@gmail,com: ${live_user_role}
   [email protected]: ${live_user_role} 
   [email protected]: ${live_admin_role}
   [email protected]:  ${live_admin_role}

terraform is deleting the previous user and recreating all the users again

    squadcast_team_member.membership["1"] must be replaced
 /- resource "squadcast_team_member" "membership" {
      ~ id       = "62ed115ab4b4017fa2a4b786" -> (known after apply)
      ~ role_ids = [
          - "61b08676e4466d68c4866db0",
            "61b08676e4466d68c4866db1",
        ]
      ~ user_id  = "62ed115ab4b4017fa2a4b786" -> "61b7d915a14c2569ea9edde6" # forces replacement
        # (1 unchanged attribute hidden)
    }

how to modify the code that will create a new member only and not change the other members during its creation

CodePudding user response:

This happens because membership_flat results in a list of maps. In a list, order is important. Thus its better to flatten your data, to get a map instead:

  membership_flat = merge([
    for team_name in local.team_names : {
      for user_email, roles in local.membership_nested[team_name] :
        "${team_name}-${user_email}" => {
          team_name = team_name
          user_name = user_email
          roles     = [roles]        
        }
    }
  ]...) # dots are important. Do not Remove them.

then

data "squadcast_team" "teams" {
  for_each = local.membership_flat 
  name     = each.value.team_name
}

data "squadcast_user" "users" {
  for_each = local.membership_flat 
  email    = each.value.user_name
}


resource "squadcast_team_member" "membership" {
  for_each = local.membership_flat 
  team_id  = data.squadcast_team.teams[each.key].id
  user_id  = data.squadcast_user.users[each.key].id
  role_ids = each.value.roles
  lifecycle {
    create_before_destroy = true
  }
}
  • Related