Terraform: Output variable data not being displayed after upgrade to 0.8.3

Created on 16 Jan 2017  ยท  3Comments  ยท  Source: hashicorp/terraform

Terraform version 0.8.3
Running on OSX 0.12.1

The following code in a module to output the IP address of an EIP in AWS , which was working fine in 0.7.7, has stopped working in the version above.

output "pub_ip_address" { value = "${aws_eip.lb.public_ip}" }

The module loads fine when you run "terraform get". Also when "terraform apply" is run , there are no errors, but the output does not get displayed.

When you try to output the variable with "Terraform output pub_ip_address" , you get the following message.

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.

However when you look at the state file, the output is clearly there.

"outputs": { "pub_ip_address": { "sensitive": false, "type": "string", "value": "192.168.1.1" },

  • address above deliberately changed.

Most helpful comment

Hi @codfather, thanks for the issue!

Terraform outputs need to be scoped to the root module, unless the -module flag is passed to output. Documentation: https://www.terraform.io/docs/commands/output.html

See the following example config:

$ tree
.
โ”œโ”€โ”€ fooModule
โ”‚ย ย  โ””โ”€โ”€ main.tf
โ””โ”€โ”€ main.tf

main.tf:

variable "test" { default = "test" }

module "foo" {
  source = "./fooModule"
}

fooModule/main.tf:

variable "foo" { default = "bar" }

output "fooOutput" { value = "${var.foo}" }
$ terraform output fooOutput
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 output -module=foo fooOutput
bar

If you pass the output up to the main module like so:

# main.tf
module "foo" {
  source = "./fooModule"
}

output "fooMain" { value = "${module.foo.fooOutput}" }

Then the output is correctly displayed at the root modules scope:

$ terraform output fooMain
bar

Please let me know if you're seeing different behavior though! Thanks.

All 3 comments

Hi @codfather, thanks for the issue!

Terraform outputs need to be scoped to the root module, unless the -module flag is passed to output. Documentation: https://www.terraform.io/docs/commands/output.html

See the following example config:

$ tree
.
โ”œโ”€โ”€ fooModule
โ”‚ย ย  โ””โ”€โ”€ main.tf
โ””โ”€โ”€ main.tf

main.tf:

variable "test" { default = "test" }

module "foo" {
  source = "./fooModule"
}

fooModule/main.tf:

variable "foo" { default = "bar" }

output "fooOutput" { value = "${var.foo}" }
$ terraform output fooOutput
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 output -module=foo fooOutput
bar

If you pass the output up to the main module like so:

# main.tf
module "foo" {
  source = "./fooModule"
}

output "fooMain" { value = "${module.foo.fooOutput}" }

Then the output is correctly displayed at the root modules scope:

$ terraform output fooMain
bar

Please let me know if you're seeing different behavior though! Thanks.

Thanks for the reply. Indeed your suggestion worked perfectly.

I was not aware of the -module parameter for the output command.

I have refactored my code with your suggestion.

Cheers

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

thebenwaters picture thebenwaters  ยท  3Comments

ronnix picture ronnix  ยท  3Comments

sprokopiak picture sprokopiak  ยท  3Comments

franklinwise picture franklinwise  ยท  3Comments

rjinski picture rjinski  ยท  3Comments