Skip to content

Instantly share code, notes, and snippets.

@lorne-luo
Last active December 3, 2019 18:15
Show Gist options
  • Save lorne-luo/c66d5e098da9b6c1ef4686b50cc676a4 to your computer and use it in GitHub Desktop.
Save lorne-luo/c66d5e098da9b6c1ef4686b50cc676a4 to your computer and use it in GitHub Desktop.
machine learning inference with AWS Lambda and Layers
pipenv lock -r > requirements.txt
zip -r MyMLfunction_layer1.zip python
aws s3 cp MyMLfunction_layer1.zip s3://mybucket/layers/
# layer 1
aws lambda publish-layer-version --layer-name MyMLfunction_layer1 --content S3Bucket=mybucket,S3Key=layers/MyMLfunction_layer1.zip --compatible-runtimes python3.6
# layer 2
zip MyMLfunction_layer2_model20190104v1.zip *.joblib
aws lambda publish-layer-version --layer-name MyMLfunction_layer2 --zip-file fileb://MyMLfunction_layer2_model20190104v1.zip --compatible-runtimes python3.6
# lambda
zip MyModel20190104v1_lambda_function.zip lambda_function.py
aws lambda create-function --function-name MyModelInference --runtime python3.6 --handler lambda_function.lambda_handler --role arn:aws:iam::xxxxxxxxxxx:role/MyModelInference_role --zip-file fileb://MyModel20190104v1_lambda_function.zip
# Link the layers
aws lambda update-function-configuration --function-name MyModelInference --layers arn:aws:lambda:eu-west-1:xxxxxxxxxxx:layer:MyMLfunction_layer1:1 arn:aws:lambda:eu-west-1:xxxxxxxxxxx:layer:MyMLfunction_layer2:1
from sklearn.externals import joblib
# Save to file in the current working directory
joblib_file = “MyModel20190104v1.joblib”
joblib.dump(model, joblib_file)
# Load from file
joblib_model = joblib.load(joblib_file)
#!/bin/bash
export PKG_DIR="python"
rm -rf ${PKG_DIR} && mkdir -p ${PKG_DIR}
docker run --rm -v $(pwd):/foo -w /foo lambci/lambda:build-python3.6 \
pip install -r requirements.txt --no-deps -t ${PKG_DIR}
import base64
import json
from sklearn.externals import joblib
PREP1_FILE_NAME = '/opt/PreProc1_MyModel.joblib'
PREP2_FILE_NAME = '/opt/PreProc2_MyModel.joblib'
MODEL_FILE_NAME = '/opt/MyModel.joblib'
def predict(data):
# Load the model pre-processors
pre1 = joblib.load(PREP1_FILE_NAME)
pre2 = joblib.load(PREP2_FILE_NAME)
clf = joblib.load(MODEL_FILE_NAME)
print("Loaded Model and pre-processors")
# perform the prediction
X1 = pre1.transform(data)
X2 = pre2.transform(X1)
output = dict(zip(data, clf.predict(X2)[:]))
return output
def lambda_handler(event, context):
predictList = []
for record in event['Records']:
# decode the base64 Kinesis data
decoded_record_data = base64.b64decode(record['kinesis']['data'])
# load the dynamo DB record
deserialized_data = json.loads(decoded_record_data)
predictList.append(json.loads(deserialized_data['textblob']))
result = predict(predictList)
return result
@YahyaAbraheem
Copy link

Why you put the folder in /opt/ ?

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