You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
Current Terraform Version
Use-cases
The TF documentation on variables states that:
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
Actual Result:
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:
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.
The text was updated successfully, but these errors were encountered: