hashicorp/terraform

Allow entire resource to be output

joshuaspence opened this issue ยท 7 comments

Currently we do something like this to output multiple resource attributes:

output "configuration_bucket_arn" {
  value = "${aws_s3_bucket.configuration.arn}"
}

output "configuration_bucket_id" {
  value = "${aws_s3_bucket.configuration.id}"
}

It would be great if instead we could simply do this:

output "configuration_bucket" {
  value = "${aws_s3_bucket.configuration}"
}

Currently this produces an error:

Error reading config for output configuration_bucket: aws_s3_bucket.configuration: resource variables must be three parts: TYPE.NAME.ATTR in:

${aws_s3_bucket.configuration}

In particular, this would be useful for modules output variables.

This is something that should be enabled by the new version of the configuration language interpreter, since it has a way to represent whole resources as a type of value.

It will probably not make it into the first release of the new language because it requires some significant work on how values pass between modules, separately from the direct configuration language handling, but the groundwork will definitely be laid by the current work and supporting passing of complex types between modules is definitely a big goal of this current sequence of work,

+1

o6uoq commented

@apparentlymart when you say new version, do you mean Terraform v0.12 ?

Hi all!

In v0.12.0-alpha1 I tried the following configuration:

resource "random_pet" "example" {
}

output "pet" {
  value = random_pet.example
}

Here the entire random_pet.example resource is exported via output "pet" as an object:

$ terraform output
pet = {
  "id" = "striking-pup"
  "length" = 2
  "separator" = "-"
}

Returning a whole random_pet object is not very useful, but this could equally have worked with any other resource type, including an aws_s3_bucket as suggested in the original comment above.

Additionally if count is set for this resource then the output is a list of objects:

$ terraform output
pet = [
  {
    "id" = "alert-sculpin"
    "length" = 2
    "separator" = "-"
  },
  {
    "id" = "poetic-crayfish"
    "length" = 2
    "separator" = "-"
  },
  {
    "id" = "comic-bream"
    "length" = 2
    "separator" = "-"
  },
]

The same is possible for inputs to child modules, which means that a root module can pass an entire resource object from one module to another:

module "names" {
  source = "./names" # the configuration above, for example
}

module "servers" {
  source = "./servers"

  pets = names.pet
}

So with all of that said, it seems like this feature request is addressed in v0.12.0-alpha1 and so I'm going to close it. It will also be included in the subsequent v0.12.0 final release. Thanks for suggesting this and for your patience while we laid the groundwork to make it possible! ๐ŸŽ‰

I just attempted to do this with an "aws_instance" resource and it failed with the error: Unknown token: 6:11 IDENT

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.