in samcli/hook_packages/terraform/hooks/prepare/resource_linking.py [0:0]
def _resolve_module_variable(module: TFModule, variable_name: str) -> List[Union[ConstantValue, ResolvedReference]]:
# return a list of the values that resolve the passed variable
# name in the input module.
results: List[Union[ConstantValue, ResolvedReference]] = []
LOG.debug("Resolving module variable for module (%s) and variable (%s)", module.module_name, variable_name)
var_value = module.variables.get(variable_name)
if not var_value:
raise InvalidResourceLinkingException(
message=f"The variable {variable_name} could not be found in module {module.module_name}."
)
# check the possible constant value for this variable
if isinstance(var_value, ConstantValue) and var_value is not None:
LOG.debug("Found a constant value (%s) in module (%s)", var_value.value, module.module_name)
results.append(ConstantValue(var_value.value))
# check the possible references value for this variable
if isinstance(var_value, References) and var_value is not None:
LOG.debug("Found references (%s) in module (%s)", var_value.value, module.module_name)
cleaned_references = _clean_references_list(var_value.value)
for reference in cleaned_references:
LOG.debug("Resolving reference: %s", reference)
# refer to a variable passed to this module from its parent module
if reference.startswith("var."):
config_var_name = get_configuration_address(reference[len("var.") :])
if module.parent_module:
results += _resolve_module_variable(module.parent_module, config_var_name)
# refer to another module output. This module will be defined in the same level as this module
elif reference.startswith("module."):
module_name = reference[reference.find(".") + 1 : reference.rfind(".")]
config_module_name = get_configuration_address(module_name)
output_name = reference[reference.rfind(".") + 1 :]
if (
module.parent_module
and module.parent_module.child_modules
and module.parent_module.child_modules.get(config_module_name)
):
# using .get() gives us Optional[TFModule], if conditional already validates child module exists
# access list directly instead
child_module = module.parent_module.child_modules[config_module_name]
results += _resolve_module_output(child_module, output_name)
else:
raise InvalidResourceLinkingException(f"Couldn't find child module {config_module_name}.")
# this means either a resource, data source, or local.variables.
elif module.parent_module:
results.append(ResolvedReference(reference, module.parent_module.full_address))
else:
raise InvalidResourceLinkingException("Resource linking entered an invalid state.")
return results