Terraform: `Output` does not not show module outputs

Created on 13 May 2015  ยท  11Comments  ยท  Source: hashicorp/terraform

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,

cli enhancement

Most helpful comment

@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": []
        }
    ]
}

All 11 comments

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.

:+1:

:+1: Would be great to get this in

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,

facing same issue - was there any resolution to this?

@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.

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

@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": []
        }
    ]
}

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.

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.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zeninfinity picture zeninfinity  ยท  3Comments

rkulagowski picture rkulagowski  ยท  3Comments

ketzacoatl picture ketzacoatl  ยท  3Comments

carl-youngblood picture carl-youngblood  ยท  3Comments

darron picture darron  ยท  3Comments