title: Run terraform modules conditionally
body: <p>I am trying to run modules conditionally. Below is the code. It works fine if the values are provided but if var.accounts[*].vpc_ids is blank, it fails saying var.vpc_id can't be empty. But that is basically the condition based on which the modules should run. If the vpc_id count is 0, then the modules should not run. Please help.</p>
  <pre><code>resource &quot;aws_ec2_transit_gateway_vpc_attachment&quot; &quot;this&quot; {
     transit_gateway_id = var.transit_gateway_id
    vpc_id = var.vpc_id
    subnet_ids = var.subnet_ids
    dns_support                                     = &quot;disable&quot;
    ipv6_support                                    = &quot;disable&quot;
    transit_gateway_default_route_table_association = false
    transit_gateway_default_route_table_propagation = false
  }
  
  locals {
    create_tgw_attach = var.accounts[*].vpc_ids != &quot;&quot; ? true : false
  }
  
  module &quot;tgw_peer2&quot; {
    source = &quot;../modules/tgw&quot;
      count = length(var.accounts[2].vpc_ids)
    providers  = {
      aws = aws.accepter2
    }
    create_tgw_attach      = local.create_tgw_attach
    transit_gateway_id = aws_ec2_transit_gateway.this.id
    vpc_id = var.accounts[2].vpc_ids[count.index]
    subnet_ids = var.accounts[2].vpc_subnets[count.index].subnet_ids
    destination_cidr_block = var.destination_cidr_block_route
  
    share_tgw                             = true
    create_tgw                            = false
  }
  
  module &quot;tgw_peer3&quot; {
    source = &quot;../modules/tgw&quot;
    create_tgw_attach      = local.create_tgw_attach
    count = length(var.accounts[3].vpc_ids)
    providers  = {
      aws = aws.accepter3
    }
    transit_gateway_id = aws_ec2_transit_gateway.this.id
    vpc_id = var.accounts[3].vpc_ids[count.index]
    subnet_ids = var.accounts[3].vpc_subnets[count.index].subnet_ids
  
    share_tgw                             = true
    create_tgw                            = false  
  }
  </code></pre>