def detach_policy()

in Lambda/BackupOrgPolicyManager/src/BackupOrgPolicyManager.py [0:0]


def detach_policy(org_client, policy_id, policy_target):
    """
    Helper function to detach the specified policy from policy_target
    """
    try:
        logger.info(f"Detaching {policy_id} from Account {policy_target}")
        response = org_client.detach_policy(PolicyId=policy_id,
                                            TargetId=policy_target)
        logger.info(f"Detached {policy_id} from Account {policy_target}, Response : {response}")
    except org_client.exceptions.PolicyNotAttachedException:
        logger.info('Ignoring error to continue trying for further detachments.')

    except ClientError as e:
        if e.response['Error']['Code'] == 'ConcurrentModificationException':
            logger.info("Concurrent update detected when detaching policy, sleeping 5s and trying again..")
            sleep(5)
            response = org_client.detach_policy(PolicyId=policy_id,
                                                TargetId=policy_target)
            logger.info(f"Detached {policy_id} from Account {policy_target}, Response : {response}")
        else:
            logger.error("Error occurred detaching policy {}: {}".format(policy_id, e))