title: How to reference a resource created by a Terraform module
body: <p>I'm using the <a href="https://registry.terraform.io/modules/terraform-aws-modules/vpc/aws/1.46.0" rel="noreferrer">AWS VPC Terraform module</a> to create a VPC. Additionally, I want to create and attach an Internet Gateway to this VPC using the <a href="https://www.terraform.io/docs/providers/aws/r/internet_gateway.html" rel="noreferrer">aws_internet_gateway resource</a>.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>module "vpc" "vpc_default" {
    source = "terraform-aws-modules/vpc/aws"
  
    name = "${var.env_name}-vpc-default"
    cidr = "10.0.0.0/16"
    enable_dns_hostnames = true
  }
  
  resource "aws_internet_gateway" "vpc_default_igw" {
    vpc_id = "${vpc.vpc_default.id}"
  
    tags {
      Name = "${var.env_name}-vpc-igw-vpcDefault"
    }
  }
  </code></pre>
  
  <p>When I run <code>terraform init</code>, I get the following error:</p>
  
  <blockquote>
    <p>Initializing modules...
    - module.vpc</p>
    
    <p>Error: resource 'aws_internet_gateway.vpc_default_igw' config: unknown resource 'vpc.vpc_default' referenced in variable vpc.vpc_default.id</p>
  </blockquote>
  
  <p>How can I reference a resource created by a Terraform module?</p>