hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.1k stars 9.47k forks source link

`terraform output` causes `Error: could not read state version outputs` on fresh project #31997

Open dex4er opened 1 year ago

dex4er commented 1 year ago

Terraform Version

1.3.2

Terraform Configuration Files

# none

Debug Output

nothing special

Expected Behavior

│ 
│ 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.
╵

Actual Behavior

│ Error: could not read state version outputs: resource not found
│ 
│ 

Steps to Reproduce

terraform {
  cloud {
    organization = "org"
    workspaces {
      name = "something"
    }
  }
}

then

terraform output

Additional Context

No response

References

No response

dex4er commented 1 year ago

Suggestion:

See https://github.com/hashicorp/terraform/blob/main/internal/cloud/state.go#L412-L422

Instead of throwing an error it should be (nil, nil) returned.

sebasslash commented 1 year ago

:wave: Hi Piotr!

Thanks for submitting this issue. Most if not all of TFC's endpoints return 404s if either the user is not authorized or the resource itself was not found, see status code table for Show Current State Version Outputs for a Workspace. Unfortunately the client is unable to make this distinction, so we couldn't necessarily ignore a "not found" error here and return nil, nil.

Hopefully this clears things up for you! :smile:

dex4er commented 1 year ago

@sebasslash Ok, but there is still a bug: TFC behaves differently than Terraform without TFC when I run terraform output. It breaks my pipelines when I call terraform output after terraform plan for fresh projects without a stored state.

Is there any possibility to create a state version with Terraform just after a new workspace is created? I don't see anything like that in a documentation for terraform-provider-tfe