Terraform Nested Module Calling and Outputs
Asked Answered
V

1

7

I'm working on infrastructure provisioning, so I'm calling modules as nested.

There is my file system tree.

   ├── main.tf
   └── modules
       ├── client.tf
       └── in
          └── main.tf

My files showing as below .

   #main.tf 
   module "my_vpc" {
          source = "./modules"
   }

   # modules/client.tf
   provider "aws" {
          region = "us-east-2"
   }

   module "inner" {
          source = "./in"
   }

  # in/main.tf

  provider "aws" {
        region = "us-east-2"
  }

  resource "aws_vpc" "main" {
        cidr_block = "10.0.0.0/16"
  }

  output "vpc_id" {
      value = "${aws_vpc.main.id}"
  }

So in my case, I want to get the outputs coming from resource-created modules at in/main.tf. But when I ran the terraform apply command there is no output.

How I can solve this problem?

Vivanvivarium answered 23/1, 2019 at 9:45 Comment(1)
Possible duplicate of 'Not a valid output for module' when using output variable with terraformCandace
W
19

You have two modules being used but only one output statement.

./main.tf creates module my_vpc from ./modules/client.tf in client.tf you create module inner from ./modules/in/main.tf

The module inner has a single output vpc_id defined in ./modules/in/main.tf You need to make an output statement at the ./modules/client.tf level as well. Any module that you want an output from, must have an output statement for that variable, even if the output is chaining the output of an inner module.

# ./modules/client.tf
provider "aws" {
   region = "us-east-2"
}

module "inner" {
   source = "./in"
}

output "vpc_id" {
   value = "${modules.inner.vpc_id}"
}

Now the module defined in ./modules/client.tf outputs the value you want at the top level. You can interact with it in ./main.tf like this:

#main.tf 
module "my_vpc" {
   source = "./modules"
}

locals {
   vpc_id = "${modules.my_vpc.vpc_id}"
}

# output the vpc id if you need to
output "vpc_id" {
   value = "${modules.my_vpc.vpc_id}"
}

As a side note, as you scale up your terraform and module usage, being consistent will help. If you are going to have a module inside of another module, I recommend having a consistent folder structure like the following.

├── main.tf
└── modules
   ├── vpc
      ├── modules
      ├  └── in
      ├     └── main.tf
      └── client.tf
   └── another_module
      └── main.tf
Wizard answered 23/1, 2019 at 13:8 Comment(2)
i've called output also as nested , it works thank you bro !Vivanvivarium
Glad I could help, if the answer solved your problem, could you mark the answer accepted please?Wizard

© 2022 - 2024 — McMap. All rights reserved.