Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Aws Terraform managed resource "aws_instance" has not been declared in module.vpc

I’, new to Terraform and experimenting with modules. I got everything working except when i need to Create Elastic IP and associate it the Network Interface.

Here is my code:

resource "aws_eip" "Terraform-eip" {
  instance                  =  aws_instance.terraform.id 
  depends_on                = [ aws_internet_gateway.internet- gateway ]
  network_interface         = aws_network_interface.private-interface.id
}

This code is located on my Networking module.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

On the Ec2 module ive defined my asw_instance with instance name of terraform. code below:

resource "aws_instance" "terraform" {
  ami           = "ami-053b0d53c279acc90"
  instance_type = var.ec2_instance_type
}

the Ec2 module has also been declared to to main.tf

module "vpc" {
  source = "./modules/networking"
}

module "ec2" {
  source = "./modules/ec2"
}

When I do terraform validate I get this error:

Error: Reference to undeclared resource
on modules/networking/main.tf line 64, in resource "aws_eip" "Terraform-eip":
64: instance = aws_instance.terraform.id
A managed resource "aws_instance" "terraform" has not been declared in module.vpc.

Not sure why this is an issue since everything is declare in the main.tf module.
Any advice?

>Solution :

At least two things need to happen for this to work:

  1. Define a variable in the networking module
  2. Define an output in the EC2 module which will be provided to the variable defined in the networking module

So, the first step would be defining the variable in the networking module:

variable "ec2_instance_id" {
  type       = string
  descrption = "EC2 instance ID to be used with the EIP."
}

You also need to define the variable in the EIP resource:

resource "aws_eip" "Terraform-eip" {
  instance                  = var.ec2_instance_id
  depends_on                = [ aws_internet_gateway.internet- gateway ]
  network_interface         = aws_network_interface.private-interface.id
}

Then, in the ec2 module, you would define an output like this:

output "instance_id" {
  value = aws_instance.terraform.id
}

Finally, when calling the modules, you would change the code to the following:

module "vpc" {
  source = "./modules/networking"
  ec2_instance_id = module.ec2.instance_id
}

module "ec2" {
  source = "./modules/ec2"
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading