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

terraform output and multiple resources #6054

Closed
jseaidou opened this issue Apr 6, 2016 · 7 comments
Closed

terraform output and multiple resources #6054

jseaidou opened this issue Apr 6, 2016 · 7 comments
Labels

Comments

@jseaidou
Copy link

jseaidou commented Apr 6, 2016

I am trying to define a output that looks like this:

output "machines" {
    value = "${join("\n", openstack_compute_instance_v2.test_instance.*)}"
}

output "machines_count" {
    value = "${length(openstack_compute_instance_v2.test_instance.*)}"
}

but however I am not getting any output. The purpose of this is we're trying to extract the number of instances associated with a given resource.

@apparentlymart
Copy link
Contributor

It looks like you're resource references are missing an attribute part. For example, if you want the ids you might write openstack_compute_instance_v2.test_instance.*.id.

Did this give you an error or just output the empty string? There is supposed to be an error message that explains this so if you didn't see that here that seems like a bug.

@jseaidou
Copy link
Author

jseaidou commented Apr 7, 2016

There was no error message, however id isn't what we're looking for. I am looking for a way to get the fully qualified name openstack_compute_instance_v2.test_instance.0, openstance_compute_instance_v2.test_instance.1, ...

@jseaidou
Copy link
Author

jseaidou commented Apr 7, 2016

Just for clarification, this is somewhat related to issue #6058 . What we are trying to do is attempt at recycling our VMs, and to do this, we're trying to get all the resources known currently and run at a high level the following logic.

  1. get all resources of the form openstack_compute_instance_v2.test_instance from terraform output machines
  2. for each of those resources as i do
  • terraform apply -target=test_instance
  • if apply was successful: terraform destroy openstack_compute_instance_v2.test_instance.i
  • otherwise delete all new instances and rollback to old state and preserve it so as not to destroy our current cluster

This could also be simplified if issue #6058 is implemented that would allow us to condense step 2 into:
2. for each of those resources as i do:

  • terraform taint openstack_compute_instance_v2.test_instance.i
  • terraform apply

for step 1., this is where the block output machines comes into play

@apparentlymart
Copy link
Contributor

Hmm, interesting... Terraform intentionally doesn't really expose the resource "name" used within the configuration, because it's intended to be local to the configuration rather than something to be exposed to callers. But you do have an interesting new use-case of taking the Terraform output and then feeding it back in to Terraform as input.

One workaround you might like to try for now is to make your process read the terraform.tfstate file, which is a JSON serialization of all of the resources known to Terraform. Its structure is not actually documented and not considered part of the public interface, but in practical terms it's unlikely to change in super-incompatible ways because it's preserved between Terraform runs and thus we endeavor to keep it working on a broad set of Terraform versions.

In Terraform 0.7, the next major release, it is planned to add a new command terraform state list that may better address your use-case in the long run. You can read more about that in #5811, but here's an example of how it would work for your use-case, I think:

$ terraform state list openstack_compute_instance_v2..test_instance
openstack_compute_instance_v2..test_instance[0]
openstack_compute_instance_v2..test_instance[1]

If that looks like it'd solve your problem then if it's okay with you I'd like to close this to consolidate the discussion over in #5811.

@jseaidou
Copy link
Author

jseaidou commented Apr 7, 2016

This would definitely be useful to us. I think that would achieve the same thing for us. However I think there is a inconsistency in the usage of: openstack_compute_instance_v2.test_instance[0] vs openstack_compute_instance_v2.test_instance.0 in some of the commands which might make it harder to use. Is there a issue open to address that?

@mitchellh
Copy link
Contributor

Dup of a core enhance issue!

@ghost
Copy link

ghost commented Apr 19, 2020

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 Apr 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants