I’m trying to create a local variable and I have a conditional to avoid looking at not existing values.
The problem is that terraform keeps returning an error for the last lookup not able to find the key, but it won’t find the key as the map is empty as the condition tested.
The line of code is this:
all_apps_secrets= var.common_secrets == {} ? {for serv in var.ecs_services : serv.service_name => serv.secrets} : {for serv in var.ecs_services : serv.service_name => merge(serv.secrets, {for k,v in lookup(local.app_secrets, serv.service_name,{}): k => lookup(local.common_secrets, v)}) }
As you can see, lookup functions are used only when conditional evaluates to false, so as common_secrets is {}, it should never try to construct the for with the lookups.
So, does Terraform supports incomplete evaluation to avoid running the lookups?
Thanks
Terraform supports a form of partial evaluation when dealing with unknown values, which can still be evaluated in expressions, and possibly return unknown values as a result. Conditional expressions will also “short circuit”, however both sides must be valid and result in equal types.
What you have here however appears to an object which is known, but does not contain the desired attribute, meaning there is type error in the expression. While it would be better to provide more complete type information, that might not be possible in all situations. In that case there is the try function, which can evaluate the expression and return the default if evaluating that expression fails.
Thanks. I’ve solved the issue by rewriting the whole code. Try is of much help but it doesn’t output any information in case of an error, just allows you to pass a default. This makes debugging harder as you don’t know why the code actually failed and try made his part.
Regards