def lambda_handler()

in backend/lambda/app.py [0:0]


def lambda_handler(event, context):

    # elasticsearch variables
    service = 'es'
    region = environ['AWS_REGION']
    elasticsearch_endpoint = environ['ES_ENDPOINT']

    session = boto3.session.Session()
    credentials = session.get_credentials()
    awsauth = AWS4Auth(
        credentials.access_key,
        credentials.secret_key,
        region,
        service,
        session_token=credentials.token
        )

    es = Elasticsearch(
        hosts=[{'host': elasticsearch_endpoint, 'port': 443}],
        http_auth=awsauth,
        use_ssl=True,
        verify_certs=True,
        connection_class=RequestsHttpConnection
    )

    # sagemaker variables
    sagemaker_endpoint = environ['SM_ENDPOINT']

    api_payload = json.loads(event['body'])
    k = api_payload['k']
    payload = api_payload['searchString']

    if event['path'] == '/postText':
        features = get_features(sm_runtime_client, sagemaker_endpoint, payload)
        s3_uris_neighbors = get_neighbors(features, es, k_neighbors=k)
        s3_presigned_urls = generate_presigned_urls(s3_uris_neighbors)
        return {
            "statusCode": 200,
            "headers": {
                "Access-Control-Allow-Origin":  "*",
                "Access-Control-Allow-Headers": "*",
                "Access-Control-Allow-Methods": "*"
            },
            "body": json.dumps({
                "images": s3_presigned_urls,
            }),
        }
    else:
        search = es_match_query(payload, es, k)

        for i in range(len(search)):
            search[i]['presigned_url'] = generate_presigned_urls([search[i]['image']])[0]
            search[i]['description'] = " ".join(search[i]['description'])
            search[i]['description'] = search[i]['description'].replace("<em>",'<em style="background-color:#f18973;">')
        return {
            "statusCode": 200,
            "headers": {
                "Access-Control-Allow-Origin":  "*",
                "Access-Control-Allow-Headers": "*",
                "Access-Control-Allow-Methods": "*"
            },
            "body": json.dumps(search),
        }