def remove_unused_parameters()

in lambdas/custom_resources/CTE_CrossAccountCloudFormation/src/cfn_helper.py [0:0]


def remove_unused_parameters(template, parameters):
    """This will scan each parameter to ensure it still exists in the
    AWS CloudFormation template

    Args:
        template (str): String of AWS CloudFormation Template
        parameters (list): List of AWS CloudFormation Parameters

    Returns:
        str: Returns the StackStatus message from the response
    """
    return_parameters = list()
    logger.info(f"Scanning Parameters to be removed - {parameters}")
    logger.debug(f"template:{template}")
    cfn_template = load_yaml(template)
    template_params = cfn_template.get('Parameters')
    if template_params:
        logger.info(f"Template Parameters:{template_params}")
        template_parameters_keys = template_params.keys()

        for parameter in parameters:
            logger.debug(f"Checking Parameter:{parameter} against template_parameters_keys:{template_parameters_keys}")
            param_value = parameter['ParameterKey']
            if param_value in template_parameters_keys:
                logger.info(f"Found Parameter:{param_value} in template, appending to return parameter list")
                return_parameters.append(parameter)

            else:
                logger.info(f"Parameter:{param_value} was not found in template")

    return return_parameters