in samcli/lib/samlib/resource_metadata_normalizer.py [0:0]
def get_resource_id(resource_properties, logical_id):
"""
Get unique id for a resource.
for any resource, the resource id can be the customer defined id if exist, if not exist it can be the
cdk-defined resource id, or the logical id if the resource id is not found.
Parameters
----------
resource_properties dict
Properties of this resource
logical_id str
LogicalID of the resource
Returns
-------
str
The unique function id
"""
resource_metadata = resource_properties.get("Metadata", {})
customer_defined_id = resource_metadata.get(SAM_RESOURCE_ID_KEY)
if isinstance(customer_defined_id, str) and customer_defined_id:
LOG.debug(
"Sam customer defined id is more priority than other IDs. Customer defined id for resource %s is %s",
logical_id,
customer_defined_id,
)
return customer_defined_id
resource_cdk_path = resource_metadata.get(RESOURCE_CDK_PATH_METADATA_KEY)
if not isinstance(resource_cdk_path, str) or not resource_cdk_path:
LOG.debug(
"There is no customer defined id or cdk path defined for resource %s, so we will use the resource "
"logical id as the resource id",
logical_id,
)
return logical_id
# aws:cdk:path metadata format of functions: {stack_id}/{function_id}/Resource
# Design doc of CDK path: https://github.com/aws/aws-cdk/blob/master/design/construct-tree.md
cdk_path_partitions = resource_cdk_path.split("/")
min_cdk_path_partitions_length = 2
LOG.debug("CDK Path for resource %s is %s", logical_id, cdk_path_partitions)
if len(cdk_path_partitions) < min_cdk_path_partitions_length:
LOG.warning(
"Cannot detect function id from aws:cdk:path metadata '%s', using default logical id", resource_cdk_path
)
return logical_id
cdk_resource_id = (
cdk_path_partitions[-2]
if cdk_path_partitions[-1] == "Resource"
or (
resource_properties.get("Type", "") == AWS_CLOUDFORMATION_STACK
and cdk_path_partitions[-2].endswith(CDK_NESTED_STACK_RESOURCE_ID_SUFFIX)
)
else cdk_path_partitions[-1]
)
# Check if the Resource is nested Stack
if resource_properties.get("Type", "") == AWS_CLOUDFORMATION_STACK and cdk_resource_id.endswith(
CDK_NESTED_STACK_RESOURCE_ID_SUFFIX
):
cdk_resource_id = cdk_resource_id[: -len(CDK_NESTED_STACK_RESOURCE_ID_SUFFIX)]
return cdk_resource_id