def lambda_handler()

in repos/serving/lambdas/functions/xgboost_inference/lambda_function.py [0:0]


def lambda_handler(event, context):
    # Get data from online feature store
    logger.info(event)
    val_policy_id = str(event["queryStringParameters"]["policy_id"])

    claims_response = featurestore_runtime.get_record(
        FeatureGroupName=claims_fg_name,
        RecordIdentifierValueAsString=str(val_policy_id),
    )

    if claims_response.get("Record"):
        claims_record = claims_response["Record"]
        claims_df = pd.DataFrame(claims_record).set_index("FeatureName")
    else:
        logging.info("No Record returned / Record Key in claims feature group\n")
        return {
            "statusCode": 404,
            "body": json.dumps({"Error": "Record not found in CLAIMS feature group"}),
        }

    customers_response = featurestore_runtime.get_record(
        FeatureGroupName=customers_fg_name,
        RecordIdentifierValueAsString=str(val_policy_id),
    )

    if customers_response.get("Record"):
        customer_record = customers_response["Record"]
        customer_df = pd.DataFrame(customer_record).set_index("FeatureName")
    else:
        logging.info("No Record returned / Record Key in CUSTOMERS feature group\n")
        return {
            "statusCode": 404,
            "body": json.dumps(
                {"Error": "Record not found in CUSTOMERS feature group"}
            ),
        }

    try:
        blended_df = pd.concat([claims_df, customer_df]).loc[col_order]
        data_input = ",".join(blended_df["ValueAsString"])

        logging.info("data_input: ", data_input)
        response = client_sm.invoke_endpoint(
            EndpointName=endpoint_name, Body=data_input, ContentType=content_type
        )

        score = json.loads(response["Body"].read())
        logging.info(f"score: {score}")

        return {
            "statusCode": 200,
            "body": json.dumps({"policy_id": val_policy_id, "score": score}),
        }
    except Exception:
        logging.exception(f"internal error")
        return {
            "statusCode": 500,
            "body": json.dumps(
                {"Error": f"internal error. Check Logs for more details"}
            ),
        }