terraform icon indicating copy to clipboard operation
terraform copied to clipboard

Output Value not returning values as expected even when the syntax is correct.

Open yashdeored opened this issue 1 year ago • 3 comments

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 avatar May 19 '24 19:05 yashdeored

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 avatar May 20 '24 14:05 jbardin

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.

yashdeored avatar May 20 '24 16:05 yashdeored

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.

jbardin avatar May 20 '24 17:05 jbardin

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.

yashdeored avatar May 21 '24 16:05 yashdeored

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.

jbardin avatar May 21 '24 16:05 jbardin

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

yashdeored avatar May 21 '24 17:05 yashdeored

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.

jbardin avatar May 21 '24 17:05 jbardin

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

yashdeored avatar May 23 '24 18:05 yashdeored

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.

jbardin avatar May 23 '24 18:05 jbardin

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

yashdeored avatar May 23 '24 18:05 yashdeored

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

jbardin avatar May 23 '24 18:05 jbardin

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

yashdeored avatar May 23 '24 18:05 yashdeored

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.

github-actions[bot] avatar Jun 23 '24 02:06 github-actions[bot]