title: Terraform chicken/egg problem using aws_vpc data source in root module
body: <p>I have a root Terraform module that declares a VPC module and other modules such as an EC2 instance that is to launch in the VPC.</p>
  
  <p>In the EC2 module, I read the VPC using the <code>aws_vpc</code> type:</p>
  
  <pre class="lang-tf prettyprint-override"><code>data "aws_vpc" "vpc" {
    filter {
      name  = "tag:Name"
      values = [var.name_tag]
    }
  }
  </code></pre>
  
  <p>Now this works fine if I declare the modules independently.</p>
  
  <p>But when declaring a root module that declares these other modules separately, I get this failure:</p>
  
  <pre class="lang-none prettyprint-override"><code>▶ terraform apply
  module.cloudwatch.data.aws_ami.ami: Refreshing state...
  module.backend.data.aws_vpc.vpc: Refreshing state...
  module.backend.data.aws_ami.ami: Refreshing state...
  
  Error: no matching VPC found
  
    on .terraform/modules/backend/main.tf line 1, in data "aws_vpc" "vpc":
     1: data "aws_vpc" "vpc" {
  </code></pre>
  
  <p>So there is a chicken/egg problem here. </p>
  
  <p>I am confused. How can this ever work? If a root module cannot both declare a VPC and then use the <code>aws_vpc</code> data source later to read it into other modules, what is the use of these data sources? I would appreciate advice on the best practice here. Should I simply not use <code>aws_vpc</code> and instead read in the VPC ID as an output elsewhere?</p>