def delete_cert_and_policy()

in lambda/lambda_function.py [0:0]


def delete_cert_and_policy(deviceId, principals):
    for principal in principals:
        certificateId = principal.split('/')[-1]
        policies = iot.list_attached_policies(target=principal)
        for policy in policies['policies']:
            try:
                #update certificate status to INACTIVE
                iot.update_certificate(
                    certificateId=certificateId, newStatus='INACTIVE'
                )
                #deatch thing's principle
                #Asynchronous call. How to make sure it done or not?
                iot.detach_thing_principal(
                    thingName=deviceId,
                    principal=principal
                )
                #deatch thing's policy
                iot.detach_policy(
                    policyName=policy['policyName'], target=principal
                )
                #delete policy
                iot.delete_policy(policyName=policy['policyName'])
                #delete thing's certificate
                iot.delete_certificate(
                    certificateId=certificateId, forceDelete=True
                )
            except Exception as e:
                print(e)