def lambda_handler()

in backend/training-pipeline/functions/step_functions/delete_oldest_endpoint.py [0:0]


def lambda_handler(event, context):
    # Print inputs
    print(f"event['trainingId'] = {event['trainingId']}")
    print(f"event['created'] = {event['created']}")
    training_id = event['trainingId']
    created = event['created']

    # List all end points
    list_response = client.list_endpoints(
        SortBy='CreationTime',
        SortOrder='Ascending'
    )
    endpoints = list_response['Endpoints']

    print('Comparing against max number of endpoint')
    print(f'MAX_NUMBER_ENDPOINT = {MAX_NUMBER_ENDPOINT}')
    if len(endpoints) >= MAX_NUMBER_ENDPOINT:
        print('Deleting the oldest endpoint')
        endpoint_to_delete = endpoints[0]['EndpointName']
        client.delete_endpoint(
            EndpointName=endpoint_to_delete
        )
        delete_msg = f'Endpoint {endpoint_to_delete} has been deleted.'
        print(delete_msg)

        endpoint_arn = endpoints[0]['EndpointArn']
        update_endpoint_arn(endpoint_arn, 'DELETED')

        print('Updating DynamoDB record')
        model_table.update_item(
            Key={
                'trainingId': training_id,
                'created': created
            },
            UpdateExpression='SET #st = :ns',
            ExpressionAttributeNames={
                '#st': 'status'
            },
            ExpressionAttributeValues={
                ':ns': 'ENDPOINT_DELETED'
            }
        )

        return delete_msg

    else:
        return 'No endpoint deleted'