Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output does not not show module outputs #1940

Closed
alexlouden opened this issue May 13, 2015 · 11 comments
Closed

Output does not not show module outputs #1940

alexlouden opened this issue May 13, 2015 · 11 comments

Comments

@alexlouden
Copy link

Hey all,

I've got a state that looks something like this - just a single module with all my resources and an output defined within:

terraform.tfstate:

{
    "version": 1,
    "serial": 32,
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {},
            "resources": {}
        },
        {
            "path": [
                "root",
                "mymodule"
            ],
            "outputs": {
                "api public": "ec2-1-2-3-4.us-west-2.compute.amazonaws.com"
           }
...

I'm trying to access the output within the module, but I keep getting told that the state file has no outputs defined. (Presumably this line?)

❯ terraform output "mymodule.api public"
The state file has no outputs defined. Define an output
in your configuration with the `output` directive and re-run
`terraform apply` for it to become available.

Is this something that there are plans to support? e.g. like taint -module=path resource
Or is there another way to get an output (e.g. ec2 public dns) from a module?

Thanks,

@mitchellh
Copy link
Contributor

Well, one bug is that outputs shouldn't be able to have spaces. So let's fix that up (and you should too!)

But we should support this, tagged.

@ernoaapa
Copy link

👍

@james-s-nduka
Copy link

👍 Would be great to get this in

@Constantin07
Copy link

On CentOS 7.2, Terraform v0.7.6 with this config:

output "nat_ip" {
    value = "${aws_eip.nat_elastic_ip.ip}"
}

I get the following even after running "terraform apply":

[ terraform]$ terraform output
The state file has no outputs defined. Define an output
in your configuration with the `output` directive and re-run
`terraform apply` for it to become available.
[ terraform]$

It turns out that the output is not printed even during "terraform apply" too. Should it behave like that ?

Regards,

@ddimri
Copy link

ddimri commented Nov 8, 2016

facing same issue - was there any resolution to this?

@ernoaapa
Copy link

ernoaapa commented Nov 8, 2016

@ddimri only way what I found were that you need to "pipe" the output from module down in the hierarchy. So take the output of module and pass forward to new output.

@apparentlymart
Copy link
Contributor

Indeed, re-exporting the value from the top-level (root) module is the best way to do this right now. Generally Terraform thinks of nested modules as being implementation details of the root module and won't expose them, though of course we can potentially do this here as a convenience.

In the mean time, doing something like this in the root module would be my suggestion:

output "api_public" {
    value = "${module.mymodule.api_public}"
}

Then you can retrieve it from the command line like this:

$ terraform output api_public

@haokanga
Copy link

haokanga commented May 7, 2018

@apparentlymart

It seems re-exporting is no longer necessary with "version": 3.

terraform output will not show module output, but only the root path.
terraform output -module= mymodule will show module output.

$ terraform output
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. You can use the 
`terraform console` command to assist.

$ terraform output -module=mysql-db
generated_user_password = ...
instance_address = ...
instance_address_time_to_retire = ...
instance_name = ...
self_link = ...

$ terraform output -module=mysql-db generated_user_password
<pwd>

terraform.tfstate:

{
    "version": 3,
    "terraform_version": "0.11.3",
    "serial": 12,
    "lineage": "069eb25b-5800-439f-89fa-9a0a6742dfa0",
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {},
       },
        {
            "path": [
                "root",
                "mysql-db"
            ],
            "outputs": {
                "generated_user_password": ...
            },
            "depends_on": []
        }
    ]
}

@apparentlymart apparentlymart added cli and removed core labels Nov 6, 2018
@smifun
Copy link

smifun commented Jun 6, 2019

Just run into it. Apparently in terraform version 0.12 you need to reexport outputs.

terraform output --module=frontend_db

Error: Unsupported option

The -module option is no longer supported since Terraform 0.12, because now
only root outputs are persisted in the state.

@teamterraform
Copy link
Contributor

Hi all,

Indeed, in prior versions of Terraform the module outputs would end up in the serialized state snapshots as a side-effect of how Terraform tracked these internally, but Terraform 0.12 now handles them in a different way and so only the root module outputs appear in state snapshots. Child module outputs now exist only temporarily in memory, due to them now being implemented consistently with input variables and local values.

The CLI layer (of which terraform output is part) can set root module variables and access root module outputs, but all other variables and outputs in child modules are internal only.

Any data that you wish to access from outside of the Terraform configuration itself must be explicitly exported with an output block in the root module, which will then allow you to access it both with terraform output and with the terraform_remote_state data source.

Since the data in question is intentionally no longer in the state snapshots, we're going to close this now. Sorry for the long silence here, and for the confusing accidental fixing and breaking of this feature along the way.

@ghost
Copy link

ghost commented Aug 18, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Aug 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants