aws / aws/amazon-sagemaker-examples
Suggestion: Calling SageMaker Endpoint in Lambda Function URLs
- Dominant language
- Jupyter Notebook
- Stars
- 11k
- Forks
- 7k
- Avg merge
- 8h 29m
- Merged PRs (30d)
- 8
Description
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)
}
```
Contributor guide
Research direction
The issue provides a Python Lambda handler that invokes a SageMaker endpoint through a Function URL, but names no repository file or test. Start by reproducing the Function URL request and reviewing the Lambda and SageMaker logs; done requires identifying the cause of the Internal Server Error and documenting a confirmed fix.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, python
- Domain
- cloud, machine-learning
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 18/100