2
votes

I am making use of modules. And this is the structure of my files - Provisioner and module are different folders.The main.tf in stack calls out the modules.

  > provisioner 
        >stack
           |--main.tf
           |--variables.tf
  > module (folder)  
    |--aks
    |     |--main.tf
    |     |--outputs.tf
    |     |--variables.tf
    |
    |--postgresql
    |     |--main.tf
    |     |--outputs.tf
    |     |--variables.tf

When i run "terraform apply" command in the provsioner directory,it is expected to return the outputs after the apply is done. I dont get outputs. When i run 'terraform output' i get- "The state file either has no outputs defined, or all the defined outputs are empty. Please define an output in your configuration with the output keyword and run terraform refresh for it to become available. If you are using interpolation, please verify the interpolated value is not empty"

I would want to know why is this happening?

3
Does this answer your question? Terraform: Output a field from a moduleNHol

3 Answers

2
votes

Output vars of modules won't be printed out by default. You have to explicitly define the output vars in your provisioner dir.

9
votes

Terraform 0.12 and later intentionally track only root module outputs in the state. To expose module outputs for external consumption, you must export them from the root module using an output block, which as of 0.12 can now be done for a single module all in one output, like this:

output "example_module" {
value = module.example_module
}

So for your code, add a output.tf file in the root and then add output statement whichever you need output after apply.

Github issue : https://github.com/hashicorp/terraform/issues/22126

1
votes

Adding on to @crewy_stack answer. let's say your module is named as sample_ec2_mod. Inside the module directory ensure that the outputs are specified in outputs.tf

On the main.tf in the root folder, add the following:

module "sample_ec2_mod" {
    source = "./ec2"  
}
  
output "ec2_module" {  
  value = module.sample_ec2_mod  
}

When you enter terraform apply in your cli, it should give you an output option. After applying, simply use terraform output to see the outputs.