title: How to have conditional resources inside a module with 0.12 for_each
body: <p>I'm passing my modules a list and it's going to create EC2 instances and eips and attach.</p>
  <p>I'm using for_each so users can reorder the list and Terraform won't try to destroy anything.</p>
  <p>But how do I use conditional resources now? Do I still use count? If so how, because you can't use count with for_each?</p>
  <p>This is my module now:</p>
  <pre><code>variable &quot;mylist&quot; {
    type            = set(string)
    description     = &quot;Name used for tagging, AD, and chef&quot;
  }
  
  variable &quot;createip&quot; {
    type            = bool
    default         = true
  }
  
  resource &quot;aws_instance&quot; &quot;sdfsdfsdfsdf&quot; {
    for_each    = var.mylist
    user_data   = data.template_file.user_data[each.key].rendered
    tags        = each.value
    ...
  
  #conditional for EIP
  resource &quot;aws_eip&quot; &quot;public-ip&quot; {
    for_each    = var.mylist
    // I can't use this anymore!
    // how can I say if true create else don't create
    #count       = var.createip ? 0 : length(tolist(var.mylist))
    instance = aws_instance.aws-vm[each.key].id
    vpc      = true
    tags     = each.value
  }
  </code></pre>
  <p>I also need to get the value of the mylist item for eip too because I use that to tag the eip. So I think I need to index into the foreach loop somehow and also be able to use count or another list to determine if it's created or not - is that correct?</p>