# Output is not published by a root module

**URL:** <https://discuss.hashicorp.com/t/output-is-not-published-by-a-root-module/48893>\
**Category:** HCP Terraform\
**Created:** [January 12, 2023, 11:37pm UTC](https://discuss.hashicorp.com/t/output-is-not-published-by-a-root-module/48893 "2023-01-12T23:37:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![AZZ](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/azz/32/17585_2.png) [@AZZ](https://discuss.hashicorp.com/u/AZZ)\
**Post date:** [January 12, 2023, 11:37pm UTC](https://discuss.hashicorp.com/t/output-is-not-published-by-a-root-module/48893/1 "2023-01-12T23:37:50Z")

</div>

Hello,  
it appears that in order to obtain an “output” value from the root module, I have to define it (output) twice. Once in the root and then in the calling module.

Is this an expected behavior and why? I’d think that publishing in the root level should be sufficient…

```auto
main.tf:

module "vm_key" {
  source = "../../../modules/AWS/comp/keys"
...
}
output "vm_key" {
  value = module.vm_key.key_name
}

```

root module ( …/…/…/modules/AWS/comp/keys/keys.tf) :

```auto
resource "aws_key_pair" "vm_key" {
...
}
output "key_name" {
  value = aws_key_pair.vm_key.key_name
}

```

Log:

```auto
terraform apply -var-file=common.tfvars 
module.vm_key.aws_key_pair.vm_key: Refreshing state... [id=id_XX]

Changes to Outputs:
  + vm_key = "id_XX"

```

---

<div class="post-metadata">

**Author:** ![stuart-c](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/stuart-c/32/10789_2.png) [@stuart-c](https://discuss.hashicorp.com/u/stuart-c)\
**Post date:** [January 12, 2023, 11:54pm UTC](https://discuss.hashicorp.com/t/output-is-not-published-by-a-root-module/48893/2 "2023-01-12T23:54:52Z")

</div>

To have an output from your root module (which can be seen using the remote state data source or via `terraform output`) you need to include an `output` block within your root module. The associated “value” attribute of the block can reference anything the root module knows about, for example a local, an attribute from a resource defined within the root module or _an output from a module called by the root module_.

If you have a value that you do want to include in the root module output that comes from a module, that module needs to define an `output` block itself that exposes whatever value you want. A module is a way of encapsulating code, very similar in concept to a library or class in other programming languages. The only way to transfer data into or out of a module is by that module allowing it, using `variable` and `output` blocks within the module code.

---

<div class="post-metadata">

**Author:** ![stuart-c](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/stuart-c/32/10789_2.png) [@stuart-c](https://discuss.hashicorp.com/u/stuart-c)\
**Post date:** [January 13, 2023, 12:01am UTC](https://discuss.hashicorp.com/t/output-is-not-published-by-a-root-module/48893/3 "2023-01-13T00:01:45Z")

</div>

Also I’d double check your nomenclature. The root module is the directory containing the Terraform state settings and where you run the Terraform command itself from. I’m not sure from what you posted if that matches with your understanding?

---

<div class="post-metadata">

**Author:** ![AZZ](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/azz/32/17585_2.png) [@AZZ](https://discuss.hashicorp.com/u/AZZ)\
**Post date:** [January 13, 2023, 12:24am UTC](https://discuss.hashicorp.com/t/output-is-not-published-by-a-root-module/48893/4 "2023-01-13T00:24:05Z")

</div>

@stuart-c ,  
you are right, my nomenclature was incorrect. Thank you for the explanation - I think I understand now.

But something else is missing…  
I’m attempting to get the output value from a different workspace in the same org ( TerraformCloud) as per [The terraform\_remote\_state Data Source | Terraform | HashiCorp Developer](https://developer.hashicorp.com/terraform/language/state/remote-state-data) and [Terraform Registry](https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/data-sources/outputs)

I do see the “key\_name” output in the TFC workspace.  
The code :

```auto
data tfe_outputs key {
  organization = "XXX"
  workspace = "TheOtherWorkspace"
}

module "vm" {
...
  key_name = data.tfe_outputs.values.key.values.key_name
...
}

```

Log:

```auto
terraform plan -var-file=../D9.tfvars
data.tfe_outputs.key: Reading...
data.tfe_outputs.key: Read complete after 0s [id=XXXXXXX]
module.vm.data.aws_ami.AM2: Reading...
module.vm.data.aws_ami.AM2: Read complete after 0s [id=QQQQQQQQ]
╷
│ Error: Unsupported attribute
│ 
│ on ec2.tf line 21, in module "vm":
│ 21: key_name = data.tfe_outputs.key.values.key_name
│ 
│ This object does not have an attribute named "key_name".

```

Do you mind to point my error, please?

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [January 13, 2023, 12:39am UTC](https://discuss.hashicorp.com/t/output-is-not-published-by-a-root-module/48893/5 "2023-01-13T00:39:27Z")

</div>

This error suggests that either the root module of `TheOtherWorkspace` doesn’t have an `output "key_name"` block, or that you’ve not yet applied the change to add that block to the other workspace and so the value hasn’t been committed in the workspace’s latest state snapshot. (`tfe_outputs` reads output values from the most recent state snapshot of the workspace).

If you have access to `TheOtherWorkspace` in the Terraform Cloud UI then you can find a table of all of that workspace’s output values on the main page representing that workspace. Can you see an output value named `key_name` in the table there?

It tends to be hard to answer questions about interactions between specific Terraform Cloud workspaces in this forum because the contributors here cannot see how you have all of your workspaces set up. This might be a better question for [HashiCorp Support](https://support.hashicorp.com/hc/en-us), since they can (with your permission) look directly at how you’ve set up your workspaces, whereas in this forum we can only make general guesses about what the problem might be.

---

<div class="post-metadata">

**Author:** ![AZZ](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/azz/32/17585_2.png) [@AZZ](https://discuss.hashicorp.com/u/AZZ)\
**Post date:** [January 13, 2023, 2:06am UTC](https://discuss.hashicorp.com/t/output-is-not-published-by-a-root-module/48893/6 "2023-01-13T02:06:17Z")

</div>

> [@apparentlymart](#):
>
> Can you see an output value named `key_name` in the table there?

yes, it was there.  
I just realized that by default the workspace is _not_ sharing outputs. Changing that option solved my little problem.

@apparentlymart and @stuart-c : Thank you for helping me out with this problem! Truly appreciate your time and effort.

A quick question:  
is there a way to examine what the return object/value has? As of right now, i’m guessing what the attributes object might have… I’m thinking something like python’s inspect ([https://www.codingem.com/inspect-a-python-object/](https://www.codingem.com/inspect-a-python-object/))

---

<div class="post-metadata">

**Author:** ![stuart-c](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/stuart-c/32/10789_2.png) [@stuart-c](https://discuss.hashicorp.com/u/stuart-c)\
**Post date:** [January 13, 2023, 10:39am UTC](https://discuss.hashicorp.com/t/output-is-not-published-by-a-root-module/48893/7 "2023-01-13T10:39:26Z")

</div>

Take a look at the `terraform output` and `terraform state show` commands. Also `terraform console` can be useful.

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [January 13, 2023, 4:25pm UTC](https://discuss.hashicorp.com/t/output-is-not-published-by-a-root-module/48893/8 "2023-01-13T16:25:29Z")

</div>

If you’ve already applied at least one plan that included reading that `tfe_outputs` data source then one way to directly see the effective type (as seen by your downstream configuration) is to run `terraform console` and use its special `type` command which is specific to the console only:

```auto
type(data.tfe_outputs.key.values)

```

You can also see the types at the source workspace using commands like what @stuart-c mentioned. But the console technique I’ve described here is something you can do from the workspace that is consuming these outputs, rather than from the workspace that produces them.
