-1

Please instead of doing negative voting, kindly read complete problem first.

Hi I am new to terraform. I have 3 modules in terraform.

/module1/eip/main.tf
/module1/eip/output.tf

/module2/eip/main.tf
/module2/eip/output.tf

/module3/eip/main.tf
/module3/eip/output.tf

These all 3 modules create an eip along with showing it in outputs.

From main.tf on root level i am using these functions like this.

module "module-one-eip" {
  source   = "./modules/module1/eip/"
  instance = module.ec2-for-one-module.ec2-one-id
}

module "module-two-eip" {
  source   = "./modules/module2/eip/"
  instance = module.ec2-for-two-module.ec2-two-id
}

module "module-three-eip" {
  source   = "./modules/module3/eip/"
  instance = module.ec2-for-three-module.ec2-three-id
}

Now I want to remove repetitive files and I want to use one file for all modules, like all code from these 3 will reside in one file, and all outputs mentioned above will have in same file, but main problem here is how I will handle different instance variable data being passed and how it will be synced with right code section in same file.

/module/generic-eip/main.tf
/module/generic-eip/outputs.tf

and from main.tf I want to call it like this.

   module "module-generic-eip" {
      source   = "./modules/generic-eip/"
      instance = (how to manage different instance names for same file)
    }

I know there is for_each and count stuff of terraform, problem is internal configurations are diffrent, i mean how i can make dynamic naming as well.

inside ./modules/eip/main.tf

resource "aws_eip" "(how to manage different names here)" {
  instance = var.instance
  vpc      = true
}
Danish Sattar
  • 49
  • 1
  • 6
  • Its not possible to have "dynamic naming" – Marcin Sep 13 '22 at 10:23
  • hi @Marcin first of all thankyou soo much for replying. so let suppose if i write 3 resources in that module, and i will send different values through for_Each to this submodule, so how these different resources will take their values from the array being passed from main.tf I mean Primary main.tf file sending resource one value -> should goes to code block of resource one in module main.tf sending resource two value -? should goes to code of block of resource two in module main.tf ... – Danish Sattar Sep 13 '22 at 10:25
  • If you use for_each, each module will only get a single value from the array. – Marcin Sep 13 '22 at 10:27
  • okay so you mean, if i call submodule from main.tf and i am sending for_Each 3 elements to it. And in submodule if i declare 3 resources which are expecting that data from main.tf, so according to numbering, they all 3 resources declared in submodule will get 1 by 1 that array element. like first variable of array will go to first resource, second will goes to second one and same for third right? – Danish Sattar Sep 13 '22 at 10:30

1 Answers1

2

Assuming you keep your inctance module separate, and only want to consolidate EIP module, it would be as follows:

locals {
    instance_ids = [module.ec2-for-one-module.ec2-one-id, 
                    module.ec2-for-two-module.ec2-two-id, 
                    module.ec2-for-three-module.ec2-three-id]
}

module "module-generic-eip" {
    source   = "./modules/generic-eip/"
    count    = length(local.instance_ids)
    instance = local.instance_ids[count.index]
}

Code inside ./modules/eip/main.tf does not change, as for each iteration of count, var.instance will be a single element from local.instance_ids.

Then you access, individual EIPs, using indieces, 0, 1, 2:

module.module-generic-eip[0]
module.module-generic-eip[1]
module.module-generic-eip[2]
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • this make more sense now, can you please just solve this last piece for me. i mean my /modules/eip/main.tf will be like below one. `resource "aws_eip "eip-for-one-module" { instance = var.instance vpc = true } resource "aws_eip" "eip-for-two-module" { instance = var.instance vpc = true } resource "aws_eip" "eip-for-three-module" { instance = var.instance vpc = true } ` or only one resource will be given?? focus point is "eip for 1 2 3" – Danish Sattar Sep 13 '22 at 10:43
  • 1
    @DanishSattar Only one EIP resource should be defined in the module. Since your loop in the root folder, `count = length(local.instance_ids)`, 3 times, your `module-generic-eip` will be instantiated 3 times, giving 3 EIPs in total, one for each instance. – Marcin Sep 13 '22 at 10:46
  • 1
    Thanks @marcin stay blessed man. will try to implement it now, if any issue I will leave comment. – Danish Sattar Sep 13 '22 at 10:49
  • 2
    @DanishSattar No problem. Maybe its better to make a new question if you have new issues. – Marcin Sep 13 '22 at 10:50