awsdocs/aws-lambda-developer-guide

Lambda Function URLs not invoking Sagemaker endpoint

temiwale88 opened this issue · 2 comments

Hello team -

I'm having issues with the new Lambda function URL invoking a Sagemaker endpoint.
When I test the following, it works, however when I click the 'function url', I get 'Internal Server Error'.
Can anyone please assist me with this?

import base64
import logging
import json
import boto3
import pickle

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

print('Loading Lambda function')

runtime=boto3.Session().client('sagemaker-runtime')
endpoint_Name='XX_XX'
s3 = boto3.resource('s3')
bucket = 'XX_XX'
pkl_key = "dogImages/classes/dog_breeds_labels.pkl"
classes = pickle.loads(s3.Bucket(bucket).Object(pkl_key).get()['Body'].read())

def lambda_handler(event, context):

    bs=event
    runtime=boto3.Session().client('sagemaker-runtime')
    
    response=runtime.invoke_endpoint(EndpointName=endpoint_Name,
                                    ContentType="application/json",
                                    Accept='application/json',
                                    #Body=bytearray(x)
                                    Body=json.dumps(bs))
    
    result=response['Body'].read().decode('utf-8')
    sss=json.loads(result)[0]
    max_value = max(sss) #let's retrieve highest number
    max_index = sss.index(max_value) #let's retrieve the index of the max number
    predicted_dog = classes[max_index]
    final_response = {"predicted_dog" : predicted_dog}

    return {
        'statusCode': 200,
        'headers' : { 'Content-Type' : 'text/plain', 'Access-Control-Allow-Origin' : '*' },
        'type-result':str(type(result)),
        'Content-Type-In':str(context),
        'body' : json.dumps(final_response)
        }

Hello @temiwale88 , I would suggest you create an API using API Gateway that invokes your lambda function. You can use this as a reference:

https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started.html

After creating the API and giving it the necessary permissions to invoke your function, you will be able to pass the payload in the body of the API which will then be passed on to the lambda function.

The advantage of this approach would be it allows you to pass the payload and other metadata through the API as JSON. All this can be accessed in your lambda function using the event passed to the lambda_handler.

API - > Lambda -> SageMaker Endpoint

The " Internal server error" could be because of multiple resources, do you have cloud watch logs available for your function?