title: Create Terraform Output When Count Enabled
body: <p>I am trying to create an outputs.tf for a resource in Terraform which has count set. When count is 1, all works fine, however when count is 0, it does not:</p>
  
  <p>If I create the output like this</p>
  
  <pre><code>output "ec2_instance_id" {
    value = aws_instance.ec2_instance.id
  }
  </code></pre>
  
  <p>It errors </p>
  
  <blockquote>
    <p>because aws_instance.ec2_instance has "count" set, its attributes must
    be accessed on specific instances.</p>
  </blockquote>
  
  <p>However, changing it to</p>
  
  <pre><code>output "ec2_instance_id" {
    value = aws_instance.ec2_instance[0].id
  }
  </code></pre>
  
  <p>works for a count of 1, but when count is 0 gives</p>
  
  <blockquote>
    <p>aws_instance.ec2_instance is empty tuple</p>
    
    <p>The given key does not identify an element in this collection value.</p>
  </blockquote>
  
  <p>Therefore I saw <a href="https://github.com/hashicorp/terraform/issues/22480#issuecomment-521762472" rel="noreferrer">this post</a> and tried</p>
  
  <pre><code>output "ec2_instance_id" {
    value = aws_instance.ec2_instance[count.index].id
  }
  </code></pre>
  
  <p>but that gives</p>
  
  <blockquote>
    <p>The "count" object can be used only in "resource" and "data" blocks,
    and only when the "count" argument is set.</p>
  </blockquote>
  
  <p>What is the correct syntax?</p>