def disable_termination_protection()

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


def disable_termination_protection(stack_name, session=None):
    """Disables Termination Protection on CloudFormation Stacks

    Args:
        stack_name (str): Passing key word arguments that will be used to create the
        session (object, optional): boto3 session object

    Returns:
        none
    """
    try:
        client = boto3_client(service='cloudformation', session=session)
        stack_exists = describe_stack(stack_name=stack_name, session=session)
        if stack_exists:
            logger.info("Checking time difference between Stack Creation and now. (disable if < 20 min)")
            diff = datetime.now(timezone.utc) - stack_exists['Stacks'][0]['CreationTime']
            if stack_exists and (divmod(diff.days * 86400 + diff.seconds, 60)[0] < 20):
                logger.info(f"Disabling Termination Protection on {stack_name}")
                response = client.update_termination_protection(
                    EnableTerminationProtection=False,
                    StackName=stack_name
                )
                logger.debug(response)

            else:
                logger.warning('Time difference is greater than 20 min')

    except Exception as e:
        logger.error(str(e))
        raise Exception