Home > Back-end >  Synchronize passed value with each resource in module : Terraform
Synchronize passed value with each resource in module : Terraform

Time:09-14

I have main.tf file where I have "eip" module, and i am passing some instance id's to eip module.

I have 3 eip resources declared in module, each of them expecting instance id from main.tf.

Problem is how each passed value(instance id) from main.tf will go to right resource in module, as I cant change "eip resources" name in module as these "eip" are already created, I just collected all 3 "eip" from 3 different files and placed in one file just for optimization purpose.

so do I need some understanding here, or its not possible the way I am assuming it.

Please have a look at screenshot for better understanding issue. enter image description here

CodePudding user response:

In your EIP module, you should have only one resource.

eip/eip.tf

resource "aws_eip" "eip" {
  instance = var.instance
  vpc      = true
}

output "public_ip" {
  value = aws_eip.eip.public_ip
}

Then in the parent module you can access the id using module indices

main.tf

locals {
  public_ip_first_eip = module.module-generic-eip[0].public_ip

Since you are iterating in the parent module, the module will be instantiated three times, and you will get three EIPs - one EIP for each instance.

  • Related