Manish Poddar
2 min readApr 13, 2021

How to use Sagemaker endpoint for real-time scoring?

Once we have trained our ML model on Sagemaker. We need to host this model for scoring it as a real-time application. Along with the Sagemaker endpoint, we need other AWS services(AWS Lambda function, API Gateway). We will leverage the below architecture to implement this.

Architecture

As described in the architecture above

  • The user will send the request to the API gateway. API Gateway will check for auth and validation of payload if it is successful traffic will be sent to Lambda function.
  • Lambda function will perform preprocessing activities and will transfer the data in such a format that the model is trained on
  • Lambda function will pass payload to Sagemaker endpoint which will infer the result and send the result back to lambda
  • The result is sent back to the user as shown above.

Lambda function for scoring

We can use something like the below code snippet in the lambda function

## Lambda function for scoring
import json
import boto3

def lambda_handler(event, context):
client = boto3.client('sagemaker-runtime') # boto3 client
endpoint_name = 'poct-endpoint' # Name of endpoint where we have to post request
scoring_vector = [event['a'],event['b'],event['c'],event['d']] # Find dataset which have to be posted to sagemaker endpoint
body = ','.join([str(item) for item in scoring_vector])
response = client.invoke_endpoint(EndpointName=endpoint_name,
ContentType='text/csv',
Body=body)
return {

'result': str(float(response["Body"].read())) # Response
}
Manish Poddar
Manish Poddar

Written by Manish Poddar

Machine Learning Engineer at AWS | Generative AI | MS in AI & ML, Liverpool John Moores University | Solving Data Problem

No responses yet