简体   繁体   中英

Synchronize passed value with each resource in module : Terraform

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. 在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM