title: Using data for existing resource (security group), getting: A managed resource has not been declared in the root module
body: <p>Learning Terraform, I'm trying to bring up an EC2 instance, reusing existing security group (tagged <code>my-tib-sg</code>).</p>
  <p>I'm getting the following error, and not sure what I'm doing wrong:</p>
  <pre><code>Error: Reference to undeclared resource
  
  on module_three.tf line 62, in resource &quot;aws_instance&quot; &quot;nginx&quot;:
  62:   vpc_security_group_ids = [aws_security_groups.my-tib-sg.id]
  
  A managed resource &quot;aws_security_groups&quot; &quot;my-tib-sg&quot; has not been declared in
  the root module.
  </code></pre>
  <p>Here is the code:</p>
  <pre><code>##################################################################################
  # VARIABLES
  ##################################################################################
  
  variable &quot;aws_access_key&quot; {}
  variable &quot;aws_secret_key&quot; {}
  variable &quot;private_key_path&quot; {}
  variable &quot;key_name&quot; {}
  variable &quot;region&quot; {
    default = &quot;us-east-1&quot;
  }
  
  ##################################################################################
  # PROVIDERS
  ##################################################################################
  
  provider &quot;aws&quot; {
    access_key = var.aws_access_key
    secret_key = var.aws_secret_key
    region     = var.region
  }
  
  ##################################################################################
  # DATA
  ##################################################################################
  
  data &quot;aws_ami&quot; &quot;aws-linux&quot; {
    most_recent = true
    owners      = [&quot;amazon&quot;]
  
    filter {
      name   = &quot;name&quot;
      values = [&quot;amzn-ami-hvm*&quot;]
    }
  
    filter {
      name   = &quot;root-device-type&quot;
      values = [&quot;ebs&quot;]
    }
  
    filter {
      name   = &quot;virtualization-type&quot;
      values = [&quot;hvm&quot;]
    }
  }
  
  data &quot;aws_security_groups&quot; &quot;my-tib-sg&quot; {
    tags = {
      Name = &quot;my-tib-sg&quot;
    }
  }
  
  
  ##################################################################################
  # RESOURCES
  ##################################################################################
  
  resource &quot;aws_instance&quot; &quot;nginx&quot; {
    ami                    = data.aws_ami.aws-linux.id
    instance_type          = &quot;t2.micro&quot;
    key_name               = var.key_name
    vpc_security_group_ids = [aws_security_groups.my-tib-sg.id]
  
    connection {
      type        = &quot;ssh&quot;
      host        = self.public_ip
      user        = &quot;ec2-user&quot;
      private_key = file(var.private_key_path)
  
    }
  
    provisioner &quot;remote-exec&quot; {
      inline = [
        &quot;sudo yum install nginx -y&quot;,
        &quot;sudo service nginx start&quot;
      ]
    }
  }
  
  ##################################################################################
  # OUTPUT
  ##################################################################################
  
  output &quot;aws_instance_public_dns&quot; {
    value = aws_instance.nginx.public_dns
  }
  </code></pre>