16

I'm trying to apply only the output variable definition. Here is the "output" I want to apply.

output "environment_information" {
   value = {
      foo = "value1"
      bar = "value2"
   }
}

Altough I added the "bar" as a new variable, I cannot see anything when I applied it by using the following Terraform command:

terraform apply -target=output.environment_information

Any idea? Is it possible to apply output without any resource/module definition? Thank you.

2 Answers 2

27

Running terraform apply -refresh-only should take care of any new outputs. It will read the latest data from each resource and then update all of the outputs in terms of those updates, which includes re-evaluating your output expressions to incorporate any changes.

0
2

You cannot use resource targeting in this way as an output is not a resource, you can however run a simple apply, as terraform is idempotent that should not change any existing infrastructure but simply put in your output into terraform state.

you can test this with a simple terraform plan. Tested with terraform 1.0. With nothing else in your statefile you will end up with this, after apply:

{
  "version": 4,
  "terraform_version": "1.0.0",
  "serial": 1,
  "lineage": "d79eacc7-51eb-2bf4-5ec6-cf8dca57348d",
  "outputs": {
    "environment_information": {
      "value": {
        "bar": "value2",
        "foo": "value1"
      },
      "type": [
        "object",
        {
          "bar": "string",
          "foo": "string"
        }
      ]
    }
  },
  "resources": []
}

If you have other non applied/half applied updates you can do one of two things, roll back your code to when they were not there and then remove any associated resources from state (keep a backup) then run apply again, or manually edit the statefile, either way update your hash in the locking db. If its only a couple of outputs i'd go with editing the statefile, just follow the syntax and always always have yourstate versioned and pull a copy just in case.

1
  • 2
    Thanks for the answer. I know it does not make sense, but we have a non-applied updates in the terraform. I need a solution to apply only the output part without touching/applying other stuff. Commented Jul 14, 2021 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.