-1

I am trying to create dependency between multiple sub modules which should be able to create the resource individually as well as should be able to create the resource if they are dependent on each other.

basically i am trying to create multiple VMs, and based on the ip addresses and vip ip address returned as the output i want to create the lbaas pool and lbaas pool members.

i have kept the project structure as below

 - Root_Folder
    - main.tf // create all the vm's 
    - output.tf
    - variable.tf
    - calling_module.tf
    - modules
        - lbaas-pool
            - lbaas-pool.tf // create lbaas pool
            - variable.tf
            - output.tf
        - lbaas-pool-members  
            - lbaas-pool-members.tf // create lbaas pool member
            - variable.tf
            - output.tf

calling_module.tf contains the reference to the lbaas-pool module and lbaas-pool-members, as these 2 modules are dependent on the output of the resource generated by main.tf file. It is giving below error:

A managed resource has not been declared.

As the resource has not been generated yet, and while running terraform plan and apply command is trying to load the resource object which has not been created. Not sure with his structure declare the module implicit dependency between the resources so the module can work individually as well as when required the complete stack.

Expected behaviour:

main.tf output parameters should be create the dependency automatically in the terraform version 0.14 but seems like that is not the case from the above error.

  • 1
    It sounds like you are trying to directly reference a resource defined in `main.tf` inside the modules, instead of passing that resource as an input to those modules. You really need to show your code, per site rules [mcve] – Mark B Apr 14 '21 at 17:14
  • I can’t add complete code. But for further details main.tf contains resource config. It generates a vm. Output.tf contain resource parameters returned after vm creation. Now I wan to add the out vars in the lbaas-pool.tf file. As lbaas-pool.tf need fqdn generated output as part of the resource of main.tf. Similarly for lbaas-pool-member.tf requires pool members ipaddr generated as part of main.tf resource. My requirement is when I generate vm by calling the root module, it should first create vm, and out params of vm can be used as input vars of lbaas-pool and lbaas-pool-members. – user3899094 Apr 15 '21 at 18:31
  • Calling_module.tf have module calls to the child module lbaas-pool.tf and lbaas-pool-members.tf. Also for creating the internal dependency between module I tried adding data block in the lbaas-pool.tf. Data block name is main.tf resource name . And added a variable name same as the output variable of main.tf. But this is not working while I try to run terraform plan. It failed with error mentioned in the question. – user3899094 Apr 15 '21 at 18:34

1 Answers1

0

Let's say you have a module that takes an instance ID as an input, so in modules/lbaas-pool you have this inside variable.tf

variable "instance_id" {
   type = string
}

Now let's say you define that instance resource in main.tf:

resource "aws_instance" "my_instance" {
  ...
}

Then to pass that resource to any modules defined in calling_module.tf (or in any other .tf file in the same folder as main.tf), you would do so like this:

module "lbaas-pool" {
  src="modules/lbaas-pool"
  instance_id = aws_instance.my_instance.id
  ...
}

Notice how there is no output defined at all here. Any output at the root level is for exposing outputs to the command line console, not for sending things to child modules.

Also notice how there is no data source defined here. You are not writing a script that will run in a specific order, you are writing templates that tell Terraform what you want your final infrastructure to look like. Terraform reads all that, creates a dependency graph, and then deploys everything in the order it determines. At the time of running terraform plan or apply anything you reference via a data source has to already exist. Terraform doesn't create everything in the root module, then load the submodule and create everything there, it creates things in whatever order is necessary based on the dependency graph.

Mark B
  • 183,023
  • 24
  • 297
  • 295
  • i understand this. but my problem statement is bit different. i want to generate a graph in terraform where let say computed value of of the main.tf is added into the submodules lbaas-pool. what i understood from the your answer above i cant go for the structure mentioned in the question above. where if i call the parent module it should first call the resource from main.tf. once execution completes for the main.tf, computed values can be utilised in the lbaas-pool.tf module also once the execution completed it calls the lbaas-pool-members.tf module for creating the pool member. – user3899094 Apr 15 '21 at 19:47
  • You keep using terms like "calling" and "executing" modules that make it sound like you see these as individual executable scripts, when they are not. – Mark B Apr 15 '21 at 19:50
  • for the data source i added it in the lbaas-pool.tf file and tried creating a reference to the main.tf resource. but data source somehow is not working. I tried creating an output param for the main.tf and used the same name in the lbaas-pool.tf in the data-source. which is returning an error. – user3899094 Apr 15 '21 at 19:51
  • You keep using terms like "calling" and "executing" modules that make it sound like you see these as individual executable scripts, when they are not. You have refused to provide a [mvce]. Your comments just restate your question and don't indicate that you have read my answer. – Mark B Apr 15 '21 at 19:53
  • actually i am trying to make the resource to work individually as well as in case required then generate in this order. for maintaining the complexity. as i dont want to make keep everything in one level and trying to keep it in small modules. – user3899094 Apr 15 '21 at 19:58
  • Terraform doesn't work the way you are trying to use it. I suggest learning about the tool you are trying to use instead of trying to impose your own ideas about how it should work. – Mark B Apr 15 '21 at 19:59