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

Outputs are not removed from state #13555

Closed
sethvargo opened this issue Apr 11, 2017 · 16 comments
Closed

Outputs are not removed from state #13555

sethvargo opened this issue Apr 11, 2017 · 16 comments
Assignees

Comments

@sethvargo
Copy link
Contributor

Terraform Version

0.9.2

Affected Resource(s)

  • core

Terraform Configuration Files

output "foo" {
  value = "bar"
}

Expected Behavior

After adding an output and running terraform refresh, removing the output and running terraform refresh, there should be no outputs.

Actual Behavior

After removing the output and refreshing, the output is still cached in the state and queryable.

Steps to Reproduce

  1. Add output
  2. Run terraform refresh
  3. Remove output
  4. Run terraform refresh
  5. Observe output is still in state, even though it's not in configs

Important Factoids

This appears to be a regression.

@apparentlymart
Copy link
Contributor

Thanks for this, @sethvargo.

I think this has the same root cause as #12572 and #11716, which is that outputs are only updated when their graph nodes are visited during apply. All of these tickets are various reasons why the apply step of an output wouldn't be run. In this case, it's because the terraform refresh command doesn't intentionally doesn't run the apply walk.

If that's true, then a targeted fix for this would be to make sure the outputs get evaluated during the refresh walk too. The more comprehensive fix would be something like what I suggested in a comment in #11716: update outputs immediately whenever any of their dependencies change, rather than waiting until their graph nodes are visited.

@jbardin
Copy link
Member

jbardin commented Apr 14, 2017

@apparentlymart, I made an attempt at solving the related issue that outputs don't generate errors, but it turned out to be a larger problem than anticipated. I think the solution to that is to promote outputs to be (or act like) an actual resource, where they are graph nodes that we can properly order, and destroy, which will in turn solve this issue.

@bclodius
Copy link
Contributor

bclodius commented May 2, 2017

@apparentlymart @jbardin Just curious when you think this will be included into a future release?

@apparentlymart
Copy link
Contributor

There are no firm plans yet, since addressing this family of output-not-updating issues is likely to require a change in approach for outputs. We have had some initial discussion about different alternatives but it will take some iterations to settle on something that works well with the rest of the system while achieving these goals.

@bclodius
Copy link
Contributor

bclodius commented May 2, 2017

@apparentlymart Thanks for the update. If there is some way I can help test any updates please let me know. We rely on outputs heavily for our infrastructure.

@bclodius
Copy link
Contributor

bclodius commented May 8, 2017

As a workaround I am using the following method

  1. Create a template_file with all values that rely on the outputs that aren't getting refreshed properly.

     data "template_file" "userdata-green" {
     template = "${file("${path.module}/somefile.sh.tpl")}"
    
     vars = {
       "ami_id"                 = "${var.green_ami_id}"
       "chef_cookbook"          = "${var.green_chef_cookbook}"
       "code_build"             = "${var.code_build_green}"
       }
     }
    
  2. Create a null_resource with local provisioner to echo the rendered template_file on triggered changes

    resource "null_resource" "green-vars {
      triggers {
        template = "${data.template_file.userdata-green.rendered}"
      }
    
      provisioner "local-exec" {
        command = "echo \"${data.template_file.userdata-green.rendered}\" > test-green.txt"
      }
    }
    
  3. Create a targeted plan for the null_resource. Apply the targeted plan. Repeat this step until no changes are found!

    terraform plan -out=output-refresh.plan -target=null_resource.green-vars
    terraform apply output-refresh.plan
    
  4. Proceed with your normal targeted plan

@jbardin
Copy link
Member

jbardin commented Nov 16, 2017

This is fixed in master via #16599, and will be in the 0.11.0 release.

@jbardin jbardin closed this as completed Nov 16, 2017
gberche-orange added a commit to orange-cloudfoundry/cf-ops-automation-broker that referenced this issue Nov 20, 2017
@jeffmccune
Copy link

This is fixed in master via #16599, and will be in the 0.11.0 release.

I'm running into this in 0.11.7, could it be a different bug? In my base module, I rename an output and all previous names persist when doing terraform refresh and terraform output

@jbardin
Copy link
Member

jbardin commented May 21, 2018

@jeffmccune, the outputs are only written during an apply.

@abitrolly
Copy link

It would be nice if terraform refresh could update outputs as well, or at least explain why only apply updates it.

@zx1986
Copy link

zx1986 commented Apr 12, 2019

I can confirm this issue still with Terraform v0.11.13

terraform refresh still show the old state.
terraform output show the old state too.

I am not running terraform apply, just terraform plan.

@kenk667
Copy link

kenk667 commented May 23, 2019

confirmed this is still an issue in 11.14 and 12.0

@abitrolly
Copy link

I switched from using terraform output to custom script that queries terraform state list and terraform state show which is extremely slow, but reliable.

@a13xb
Copy link

a13xb commented Jun 1, 2019

the outputs are only written during an apply.

@jbardin Is there a reasoning behind it?

(I have run into it with 0.12.0 which led me to this issue.)

@jblose-old
Copy link

jblose-old commented Jun 20, 2019

the outputs are only written during an apply.

@jbardin Is there a reasoning behind it?

(I have run into it with 0.12.0 which led me to this issue.)

@a13xb Using 0.12.0:
From the looks of it, terraform refresh called with the any variable file (if needed) seems to properly write the outputs. May be worth trying as well.

Note: This seems to work when adding new outputs from existing applied plans.

@ghost
Copy link

ghost commented Jul 25, 2019

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.

@ghost ghost locked and limited conversation to collaborators Jul 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants