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

Feature Request: output variable merging #20176

Open
Justin-W opened this issue Jan 31, 2019 · 0 comments
Open

Feature Request: output variable merging #20176

Justin-W opened this issue Jan 31, 2019 · 0 comments

Comments

@Justin-W
Copy link

Current Terraform Version

Terraform v0.11.10
+ provider.aws v1.25.0

Use-cases

The TF documentation on variables states that:

When multiple values are provided for the same input variable, map values are merged while all other values are overriden by the last definition.

It would be useful if map-typed output variables could also be multiply-defined and automatically merged into a single output map. This would allow subsets of the map's key-value pairs (either individual pairs or sets of pairs) to be added/appended/merged into the output using separate output variable declarations.

The need for this is based on having separate 'sections' (not TF modules, but modular, encapsulated, independently-maintained subsets of a TF configuration that is TF-apply-ed as a single, combined unit) that would each like to insert a set of key-value pairs (related only to the section itself) without any coupling to or knowledge of other sections that might also be trying to write a different set of keys to an output variable of the same name.

Attempted Solutions

// Note: Sections X and Y are colocated anf nearly identical only to simplify the example.
// In the real world, sections X and Y would be in separate files
// and contain separate content (that is maintained separately).
// The only thing they have in common is that each section wants to insert 1+ key-value pairs
// into the same (map-type) output variable,
// such that the output variable's value contains the merged pairs from all of the declarations of that variable.

// ##### HCL Section X: Start #####
resource "random_pet" "pet1" {
}

//This output variable works fine
output "pet1.id" {
  value = "${random_pet.pet1.id}"
}

output "pets_map_1" {
  value = "${map("pet1", "${random_pet.pet1.id}")}"
}

output "pets_map_2" {
  value = "${
    map(
      "pet1", "${random_pet.pet1.id}"
    )
  }"
}

output "pets_map_3" {
  value = {
    "pet1" = "${random_pet.pet1.id}"
  }
}
// ##### HCL Section X: End #####

// ##### HCL Section Y: Start #####
resource "random_pet" "pet2" {
}

//This output variable works fine
output "pet2.id" {
  value = "${random_pet.pet2.id}"
}

output "pets_map_1" {
  value = "${map("pet2", "${random_pet.pet2.id}")}"
}

output "pets_map_2" {
  value = "${
    map(
      "pet2", "${random_pet.pet2.id}"
    )
  }"
}

output "pets_map_3" {
  value = {
    "pet2" = "${random_pet.pet2.id}"
  }
}
// ##### HCL Section Y: End #####

Actual Result:

Error: output "pets_map_1": an output of this name was already defined

Error: output "pets_map_2": an output of this name was already defined

Error: output "pets_map_3": an output of this name was already defined

Proposal

Although the current behavior is reasonable (although it was unexpected, in light of the 'variable merging' documentation referenced above), it would be nice if there was a way to allow separate declarations of the same output variable to be merged (without an error).

Option 1: Change the existing behavior. This obviously has some drawbacks as well as some benefits.

Option 2: Introduce new syntax (e.g. a new HCL keyword or operator) that would (if present) trigger the merging behavior, while leaving the existing behavior as-is.

For example, if the 'output' keyword allowed an optional '+' prefix to trigger the merging functionality, I could use this to achieve what I'm attempting:

// Note: Sections X and Y are colocated anf nearly identical only to simplify the example.
// In the real world, sections X and Y would be in separate files
// and contain separate content (that is maintained separately).
// The only thing they have in common is that each section wants to insert 1+ key-value pairs
// into the same (map-type) output variable,
// such that the output variable's value contains the merged pairs from all of the declarations of that variable.

// ##### HCL Section X: Start #####
resource "random_pet" "pet1" {
}

//This output variable works fine
output "pet1.id" {
  value = "${random_pet.pet1.id}"
}

+output "pets_map_1" {
  value = "${map("pet1", "${random_pet.pet1.id}")}"
}

+output "pets_map_2" {
  value = "${
    map(
      "pet1", "${random_pet.pet1.id}"
    )
  }"
}

+output "pets_map_3" {
  value = {
    "pet1" = "${random_pet.pet1.id}"
  }
}
// ##### HCL Section X: End #####

// ##### HCL Section Y: Start #####
resource "random_pet" "pet2" {
}

//This output variable works fine
output "pet2.id" {
  value = "${random_pet.pet2.id}"
}

+output "pets_map_1" {
  value = "${map("pet2", "${random_pet.pet2.id}")}"
}

+output "pets_map_2" {
  value = "${
    map(
      "pet2", "${random_pet.pet2.id}"
    )
  }"
}

+output "pets_map_3" {
  value = {
    "pet2" = "${random_pet.pet2.id}"
  }
}
// ##### HCL Section Y: End #####

Using +output is only one possible implementation option; output+, >>output, output>>, or other possibilities would be equivalent and also satisfactory.

References

I actually tried to use HCL similar to the attempted solution above as a (hacky) workaround for the lack of feature #19931. However, that represents only one possible use case for this feature request. This feature request would still remain useful and desirable even if feature #19931 is also implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants