title: terraform How to use conditional if in for_each into map object
body: <p>I have maps of variables like this:</p>
  <p>users.tfvars</p>
  <pre><code>users = {
    &quot;testterform&quot; = {
      path          = &quot;/&quot;
      force_destroy = true
      email_address = &quot;testterform@example.com&quot;
      group_memberships = [ &quot;test1&quot; ]
      tags = { department : &quot;test&quot; }
      ssh_public_key = &quot;ssh-rsa AAAAB3NzaC1yc2EAAA4l7&quot;
    }
  
    &quot;testterform2&quot; = {
      path          = &quot;/&quot;
      force_destroy = true
      email_address = &quot;testterform2@example.com&quot;
      group_memberships = [ &quot;test1&quot; ]
      tags = { department : &quot;test&quot; }
      ssh_public_key = &quot;&quot;
    }
  
  </code></pre>
  <p>I would like to upload ssh key only if <code>ssh_public_key</code> not empty for the user. But don't understand how to check this</p>
  <p>#main.tf</p>
  <pre><code>resource &quot;aws_iam_user&quot; &quot;this&quot; {
    for_each = var.users
  
    name                 = each.key
    path                 = each.value[&quot;path&quot;]
    force_destroy        = each.value[&quot;force_destroy&quot;]
  
    tags = merge(each.value[&quot;tags&quot;], { Provisioner : var.provisioner, EmailAddress : each.value[&quot;email_address&quot;] })
  }
  
  resource &quot;aws_iam_user_group_membership&quot; &quot;this&quot; {
    for_each = var.users
  
    user   = each.key
    groups = each.value[&quot;group_memberships&quot;]
  
    depends_on = [ aws_iam_user.this ]
  }
  
  resource &quot;aws_iam_user_ssh_key&quot; &quot;this&quot; {
  
    for_each = var.users
  
    username = each.key
    encoding = &quot;SSH&quot;
    public_key = each.value[&quot;ssh_public_key&quot;]
  
    depends_on = [ aws_iam_user.this ]
  }
  </code></pre>