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 Value not returning values as expected even when the syntax is correct. #35186

Closed
yashdeored opened this issue May 19, 2024 · 12 comments
Closed
Labels
working as designed confirmed as reported and closed because the behavior is intended

Comments

@yashdeored
Copy link

Terraform Version

Terraform v1.8.3
on darwin_arm64
+ provider registry.terraform.io/hashicorp/aws v5.50.0

Terraform Configuration Files

locals {
    project_name = "yashdeore"
}

variable "instance_type" {
    type = string
}

resource "aws_instance" "my_server" {
  ami           = "ami-087c17d1fe0178315"
  instance_type = var.instance_type

  tags = {
    Name = "MyServer${local.project_name}"
  }
}

output "private_ip" {
  value = aws_instance.my_server.private_ip
}

Debug Output

│ Warning: No outputs found

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

Expected Behavior

It should have outputted the value for private ip address for an ec2 instance.
Outputs:

private_ip = "xyz"

Actual Behavior

When I ran the terraform configuration file "terraform output" command it resulted in no outputs even when it was specifies with the correct syntax on the official page.

Steps to Reproduce

  1. terraform refresh
  2. terraform output

Additional Context

Terraform is running in VS Code on Mac OS.

References

No response

@yashdeored yashdeored added bug new new issue not yet triaged labels May 19, 2024
@jbardin
Copy link
Member

jbardin commented May 20, 2024

Hi @yashdeored,

The only way I can see you getting the result shown here is if the configuration was never applied, so there is no aws_instance or private_ip in the state. Can you confirm that you have actually run terraform plan and terraform apply at some point, and the output and resource instance are in the state file? The command terraform show will show a formatted version of the objects in the state file.

@jbardin jbardin added waiting-response An issue/pull request is waiting for a response from the community waiting for reproduction unable to reproduce issue without further information labels May 20, 2024
@yashdeored
Copy link
Author

Yes I have ran the terraform plan and terraform apply code and there was a tfstate file which was created but I have debugged it after running the code several time but this bug could be reproduced so I request you to look into this matter.

@jbardin
Copy link
Member

jbardin commented May 20, 2024

Thanks @yashdeored, If that's the case, since it does work under normal circumstances, we're going to need more detail to determine what is different in your case. What would be useful is a minimal reproducible example, with all the exact steps required to go from scratch to the situation you outlined above.

@yashdeored
Copy link
Author

So it is reproduced if we install a fully new installed terraform cli on vs code and then write the output code it generates error but if it is refreshed multiple times then it generates the correct output. To regenerate it just setup a new terraform and type 'terraform plan' or 'terraform output' it generates an error.

@jbardin
Copy link
Member

jbardin commented May 21, 2024

If you are setting up a new configuration and have not run terraform apply, then terraform output is expected to generate that warning output. The command terraform plan cannot generate that error, as it only comes from the output command. It appears this is working as designed, since terraform output reads output from the saved state, and requires the configuration to have been successfully applied.

@yashdeored
Copy link
Author

I have ran the terraform apply earlier and after that terraform output and the state was also saved so everything was upto date but the issue persisted

@jbardin
Copy link
Member

jbardin commented May 21, 2024

If that's the case, please show all the steps, meaning the entire configuration and exact commands as executed, required to replicate the issue starting from an empty configuration. If I copy the above configuration exactly and apply it, it will not produce that warning with terraform output if you have a state file saved which contains that output. Without a reproducible example, we don't know how you ended up in this situation, or what else may be going on that we can't see.

@yashdeored
Copy link
Author

Firstly, I installed terraform on MacOS CLI using Homebrew:

brew tap hashicorp/tap

Now, install Terraform with hashicorp/tap/terraform.

brew install hashicorp/tap/terraform

This installs a signed binary and is automatically updated with every new official release.
To update to the latest version of Terraform, first update Homebrew.

brew update

Then, run the upgrade command to download and use the latest Terraform version.

brew upgrade hashicorp/tap/terraform

Once this is done we will run a sample tf code for outputs:

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "5.50.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "my_server" {
  ami           = "ami-087c17d1fe0178315"
  instance_type = var.instance_type

  tags = {
    Name = "MyServer-${local.project_name}"
  }
}

variable "instance_type" {
    type = string
}

output "instance_ip_addr" {
  value = aws_instance.my_server.private_ip
}

Then we have to save this file as main.tf and run

terraform fmt
terraform plan

OR

terraform output

Problems starts to occurs here as you run terraform plan or terraform output as the errors states that:

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

@jbardin
Copy link
Member

jbardin commented May 23, 2024

Hi @yashdeored

There is no step in your instructions where you apply any plan (running terraform plan can't produce that warning), so there is no output to show. Until you successfully run apply the output will remain empty, and terraform output will show that warning.

@yashdeored
Copy link
Author

What if prior to this we have already applied terraform and then destroyed it and then running terraform plan and terraform output

@jbardin
Copy link
Member

jbardin commented May 23, 2024

If you've destroyed the prior state, then there is no state, and hence no output.

@yashdeored
Copy link
Author

Ok I was following a guide wherein he destroyed the prior state and even got output after that.

@jbardin jbardin added working as designed confirmed as reported and closed because the behavior is intended and removed bug waiting-response An issue/pull request is waiting for a response from the community new new issue not yet triaged waiting for reproduction unable to reproduce issue without further information labels May 23, 2024
@jbardin jbardin closed this as not planned Won't fix, can't repro, duplicate, stale May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
working as designed confirmed as reported and closed because the behavior is intended
Projects
None yet
Development

No branches or pull requests

2 participants