Skip to content

Instantly share code, notes, and snippets.

@cbare
Created February 1, 2020 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbare/2de4900f4ed1619b97789b03178f92c4 to your computer and use it in GitHub Desktop.
Save cbare/2de4900f4ed1619b97789b03178f92c4 to your computer and use it in GitHub Desktop.
import os
import io
import boto3
import json
import csv
# grab environment variables
ENDPOINT_NAME = 'xgboost-2020-02-01-16-28-38-417'
runtime= boto3.client('runtime.sagemaker')
def lambda_handler(event, context):
print("Received event: " + json.dumps(event, indent=2))
body = event.get('body')
body = json.loads(body) if isinstance(body, str) else body
payload = body['data']
response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
ContentType='text/csv',
Body=payload)
print(response)
result = json.loads(response['Body'].read().decode())
print("result=", result)
predicted_label = int(result)
print('nothing left but return')
# return format required by API Gateway's HTTP API
# see: https://aws.amazon.com/blogs/compute/announcing-http-apis-for-amazon-api-gateway/
# see: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html
return {
'statusCode': 200,
'body': json.dumps({'predicted': predicted_label})
}
@cbare
Copy link
Author

cbare commented Feb 1, 2020

This is the lambda function I used to expose an HTTP endpoint for the mnist handwritten digits model created in the tutorial at https://docs.aws.amazon.com/sagemaker/latest/dg/getting-started-client-app.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment