Created
February 1, 2020 19:25
-
-
Save cbare/2de4900f4ed1619b97789b03178f92c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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