Skip to content

Instantly share code, notes, and snippets.

@chanux

chanux/main.tf Secret

Last active March 16, 2024 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chanux/6de8bcdfeea327240776b7e4af72eb69 to your computer and use it in GitHub Desktop.
Save chanux/6de8bcdfeea327240776b7e4af72eb69 to your computer and use it in GitHub Desktop.
A whacker in the try
#---
# Update: My original premise was due to a major mistake on my part.
# Terraform said try is for when an evaluation errors out!
# I used null to mimick error, which was wrong.
#
# I have noticed that try would just ignore any expression that is not first
# parameter to it. This gist is to demonstarte that and explore other
# possibilities.
#--
locals {
good_eval_first = try(
["foo", "bar"][0],
null,
)
good_eval_second = try(
[][0],
["foo", "bar"][0],
["foo", "bar"][1],
)
good_eval_third = try(
[][0],
{ result = "baz" }["res"],
["foo", "bar"][0],
)
# This was my original misunderstanding
null_first_is_good = try(
null,
["foo", "bar"][0],
)
null_first_with_static = try(
null,
"foo",
)
with_coalesce = coalesce(
null,
"",
can([][0]) ? [][0] : null,
["foo", "bar"][0],
)
}
output "result" {
value = {
good_eval_first = local.good_eval_first
good_eval_second = local.good_eval_second
good_eval_third = local.good_eval_third
null_first_is_good = local.null_first_is_good
null_first_with_static = local.null_first_with_static
with_coalesce = local.with_coalesce
}
}
# Example output:
# result = {
# "good_eval_first" = "foo"
# "good_eval_second" = "foo"
# "good_eval_third" = "foo"
# "null_first_is_good" = null
# "null_first_with_static" = null
# "with_coalesce" = "foo"
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment