import boto3
import time

# Create an SSM client
ssm = boto3.client('ssm')

# Initialize global variables
REKOGNITION_MODEL_ARN = None

def get_parameter(name):
    params = {
        'Name': name,
        'WithDecryption': True
    }
    response = ssm.get_parameter(**params)
    return response['Parameter']['Value']

def lambda_handler(event, context):  
    global REKOGNITION_MODEL_ARN
    if REKOGNITION_MODEL_ARN is None:        
        REKOGNITION_MODEL_ARN = get_parameter('/datafy-rekognition-stack/model-arn')
    
    model_result = stop_model()
    result = {
        "Status": model_result 
    }
    return result


def stop_model():

    client=boto3.client('rekognition')

    print('Stopping model:' + REKOGNITION_MODEL_ARN)

    #Stop the model
    try:
        response=client.stop_project_version(ProjectVersionArn=REKOGNITION_MODEL_ARN)
        status=response['Status']
        print ('Status: ' + status)
        return status
    except Exception as e:  
        print(e)  
        return e
        