© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.


© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
• 

•
•
•
•
•
model = Sequential()
model.add(Dense(1, activation='sigmoid', input_dim=100))
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense
import numpy as np
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
data = np.random.random((1000, 100))
labels = np.random.randint(2, size=(1000, 1))
model.fit(data, labels, epochs=10, batch_size=32)
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
• 

•
from tensorflow.python.keras.applications.xception import Xception
model = Xception(weights='imagenet')
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.


• ç
•
•
•
•
•
from tensorflow.python.keras.applications.xception import Xception
from tensorflow.python.keras.preprocessing import image
from tensorflow.python.keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
model = Xception(weights='imagenet')
img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
print('Predicted:', decode_predictions(preds, top=3)[0])
•
•
•
• 

•
• 



• 



•
• 



• 



© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
•
•
•
from tensorflow.python.keras.applications.xception import Xception
model = Xception(include_top=False, weights='imagenet')








def train(train_data_dir, validation_data_dir, model_path):
base_model = Xception(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
predictions = Dense(nb_classes, activation='softmax')(x)
model = Model(base_model.input, predictions)
transformation_ratio = .05
train_datagen = ImageDataGenerator(rescale=1. / 255,
rotation_range=transformation_ratio,
shear_range=transformation_ratio,
zoom_range=transformation_ratio,
cval=transformation_ratio,
horizontal_flip=True,
vertical_flip=True)
def train(train_data_dir, validation_data_dir, model_path):
...( )...
train_generator = train_datagen.flow_from_directory(train_data_dir,
batch_size=32,
class_mode='categorical')
validation_generator = validation_datagen.flow_from_directory(validation_data_dir,
batch_size=32,
class_mode='categorical')
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
top_weights_path = os.path.join(os.path.abspath(model_path), 'top_model_weights.h5')
callbacks_list = [
ModelCheckpoint(top_weights_path, monitor='val_acc', verbose=1, save_best_only=True),
EarlyStopping(monitor='val_acc', patience=5, verbose=0)
]
model.fit_generator(train_generator,
samples_per_epoch=train_generator.nb_sample,
nb_epoch=nb_epoch / 5,
validation_data=validation_generator,
nb_val_samples=validation_generator.nb_sample,
callbacks=callbacks_list)
• 

• 

final_weights_path = os.path.join(os.path.abspath(model_path), 'model_weights.h5')
model.save_weights(final_weights_path)
model_json = model.to_json()
json_file = open(os.path.join(os.path.abspath(model_path), 'model.json'), 'w')
json_file.write(model_json)
def inference(trained_model_dir, test_data_dir, results_path):
# load json and create model
json_file = open(os.path.join(trained_model_dir, model_name), 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
model.load_weights(os.path.join(trained_model_dir, model_weights))
# Read Data
test_datagen = ImageDataGenerator(rescale=1. / 255)
test_generator = test_datagen.flow_from_directory(test_data_dir,
batch_size=batch_size,
shuffle=False)
# Calculate class posteriors probabilities
y_probabilities = model.predict_generator(test_generator,
val_samples=test_generator.nb_sample)
# Calculate class labels
y_classes = probas_to_classes(y_probabilities)
filenames = [filename.split('/')[1] for filename in test_generator.filenames]
ids = [filename.split('.')[0] for filename in filenames]
•
•
• 

© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.


© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
•
•
•
• 



© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
• 

• 

•
• 







• 

• 

• 

• 

• 



• 

• 



• 



• 

© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
•
•
# event
{
'Records': [
{
'eventVersion': '2.0',
'eventSource': 'aws:s3',
'awsRegion': 'ap-northeast-2', #
'eventTime': '2017-12-13T03:28:13.528Z', #
'eventName': 'ObjectCreated:Put',
'userIdentity': {'principalId': 'AFK2RA1O3ML1F'},
'requestParameters': {'sourceIPAddress': '123.24.137.5'},
'responseElements': {
'x-amz-request-id': '1214K424C14C384D',
'x-amz-id-2': 'BOTBfAoB/gKBbn412ITN4t2psTW499iMRKZDK/CQTsjrkeSSzSdsDUMGabcdnvHeYNtbTDHoHKs='
},
's3': {
's3SchemaVersion': '1.0', 'configurationId': 'b249eeda-3d48-4319-a7e2-853f964c1a25',
'bucket': {
'name': 'aws-summit-kr-2018', #
'ownerIdentity': {
'principalId': 'AFK2RA1O3ML1F'
},
'arn': 'arn:aws:s3:::aws-summit-kr-2018'
},
'object': {
'key': 'img/test_img.png', #
'size': 11733, #
'eTag': 'f2d12d123aebda1cc1fk17479207e838',
'sequencer': '125B119E4D7B2A0A48'
}
}
}
]
}
•
•
•
•
#
def handler(event, context):
bucket_name = event['Records'][0]['s3']['bucket']['name']
file_path = event['Records'][0]['s3']['object']['key']
•
ACCESS_KEY = os.environ.get('ACCESS_KEY')
SECRET_KEY = os.environ.get('SECRET_KEY')
def downloadFromS3(strBucket, s3_path, local_path):
s3_client = boto3.client('s3',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY)
s3_client.download_file(strBucket, s3_path, local_path)
def uploadToS3(bucket, s3_path, local_path):
s3_client = boto3.client('s3',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY)
s3_client.upload_file(local_path, bucket, s3_path)
•
• 

def handler(event, context):
bucket_name = event['Records'][0]['s3']['bucket']['name']
file_path = event['Records'][0]['s3']['object']['key']
file_name = file_path.split('/')[-1]
downloadFromS3(bucket_name, file_path, '/tmp/'+file_name)


•
def handler(event, context):
bucket_name = event['Records'][0]['s3']['bucket']['name']
file_path = event['Records'][0]['s3']['object']['key']
file_name = file_path.split('/')[-1]
downloadFromS3(bucket_name, file_path, '/tmp/'+file_name)
downloadFromS3(
'aws-summit-kr-2018',
'xception_weights_tf_dim_ordering_tf_kernels.h5',
'/tmp/.keras/xception_weights_tf_dim_ordering_tf_kernels.h5'
)
•
from tensorflow.python.keras.applications.xception import Xception
from tensorflow.python.keras.preprocessing import image
from tensorflow.python.keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
def predict(img_path):
model = Xception(weights='imagenet')
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
return decode_predictions(preds, top=3)[0]
•
def handler(event, context):
bucket_name = event['Records'][0]['s3']['bucket']['name']
file_path = event['Records'][0]['s3']['object']['key']
file_name = file_path.split('/')[-1]
downloadFromS3(bucket_name, file_path, '/tmp/'+file_name)
downloadFromS3(
'aws-summit-kr-2018',
'xception_weights_tf_dim_ordering_tf_kernels.h5',
'/tmp/.keras/xception_weights_tf_dim_ordering_tf_kernels.h5'
)
result = predict('/tmp/'+file_name)
return result
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
•
•
•
• 

•


• 

• 





• 

• 

•
•
•
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
•
•
• 



• 

•
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
• 

• 

• 

docker run -v $(pwd):/outputs --name lambdapack -d amazonlinux:latest tail -f /dev/null
•
•
• 

•
• 

dev_install() {
yum -y update
yum -y upgrade
yum install -y 
wget 
gcc 
gcc-c++ 
cmake 
python36-devel 
python36-virtualenv 
python36-pip 
findutils 
zlib-devel 
zip 
unzip 
blas-devel lapack-devel atlas-devel
}
mkvirtualenv() {
cd /home/
rm -rf env
python3 -m virtualenv env --python=python3
source env/bin/activate
}
pip_install() {
source /home/env/bin/activate
pip install -U pip wheel
pip install --use-wheel tensorflow==1.7.0 --no-deps
pip install protobuf html5lib bleach --no-deps
pip install --use-wheel pillow==4.0.0
pip install h5py
}
gather_pack() {
cd /home/ && rm -rf pack && mkdir pack && cd pack
cp -R /home/env/lib/python3.6/site-packages/* .
cp -R /home/env/lib64/python3.6/site-packages/* .
cp /outputs/index.py /home/pack/index.py
find . -type d -name "test" -exec rm -rf {} +
find -name "*.so" | xargs strip
find -name "*.so.*" | xargs strip
rm -r pip && rm -r pip-* && rm -r wheel && rm -r wheel-*
find . | grep -E "(__pycache__|.pyc$)" | xargs rm -rf
echo "stripped size $(du -sh /home/pack | cut -f1)"
zip -FS -r1 /outputs/pack.zip * > /dev/null
echo "compressed size $(du -sh /outputs/pack.zip | cut -f1)"
}
•
•
•
•
•
•
• 

• 

•
import boto3
def upload_to_s3(bucket, s3_path, local_path):
client = boto3.client('s3',
aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY)
client.upload_file(local_path, bucket, s3_path)
def update_lambda(function_name, bucket, s3_path):
client = boto3.client('lambda',
aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY)
client.update_function_code(
FunctionName=function_name,
S3Bucket=bucket,
S3Key=s3_path,
)
uploadToS3(' ', ' /pack.zip', './pack.zip')
update_lambda(' ', ' ', ' /pack.zip')
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.


•
• 



•
•
•
•
def download_s3_object(strBucket, s3_path):
import io
file_obj = io.BytesIO()
s3_client = boto3.client(
's3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY
)
s3_client.download_fileobj(strBucket, s3_path, file_obj)
return file_obj
pack2 = download_s3_object('bucket-name', 'pack2.zip')
import zipfile
zip_ref = zipfile.ZipFile(pack2)
zip_ref.extractall('/tmp')
zip_ref.close()
import sys
sys.path.append("/tmp")
•
• 

• 



• 

© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.


© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
• 

•
• 

• 

© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
• 

• 

• 

• 



© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
docker run --rm -v "$PWD":/var/task lambci/lambda:python3.6 my_module.my_handler
docker run --rm -v "$PWD":/var/task lambci/lambda:python3.6
my_module.my_handler '{"some": "event"}'
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.


© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
AWS Lambda를 통한 Tensorflow 및 Keras 기반 추론 모델 서비스하기 :: 이준범 :: AWS Summit Seoul 2018

AWS Lambda를 통한 Tensorflow 및 Keras 기반 추론 모델 서비스하기 :: 이준범 :: AWS Summit Seoul 2018

  • 1.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 2.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 3.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 4.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 5.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved. 

  • 6.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 7.
  • 8.
    • model = Sequential() model.add(Dense(1,activation='sigmoid', input_dim=100)) from tensorflow.python.keras.models import Sequential from tensorflow.python.keras.layers import Dense import numpy as np model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) data = np.random.random((1000, 100)) labels = np.random.randint(2, size=(1000, 1)) model.fit(data, labels, epochs=10, batch_size=32)
  • 9.
  • 10.
  • 11.
    • • 
 • from tensorflow.python.keras.applications.xceptionimport Xception model = Xception(weights='imagenet')
  • 12.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved. 

  • 13.
  • 14.
    • from tensorflow.python.keras.applications.xception importXception from tensorflow.python.keras.preprocessing import image from tensorflow.python.keras.applications.resnet50 import preprocess_input, decode_predictions import numpy as np model = Xception(weights='imagenet') img_path = 'elephant.jpg' img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) print('Predicted:', decode_predictions(preds, top=3)[0])
  • 15.
  • 16.
  • 17.
  • 18.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 19.
    • • • from tensorflow.python.keras.applications.xception importXception model = Xception(include_top=False, weights='imagenet')
  • 20.
  • 21.
    def train(train_data_dir, validation_data_dir,model_path): base_model = Xception(weights='imagenet', include_top=False) x = base_model.output x = GlobalAveragePooling2D()(x) predictions = Dense(nb_classes, activation='softmax')(x) model = Model(base_model.input, predictions) transformation_ratio = .05 train_datagen = ImageDataGenerator(rescale=1. / 255, rotation_range=transformation_ratio, shear_range=transformation_ratio, zoom_range=transformation_ratio, cval=transformation_ratio, horizontal_flip=True, vertical_flip=True)
  • 22.
    def train(train_data_dir, validation_data_dir,model_path): ...( )... train_generator = train_datagen.flow_from_directory(train_data_dir, batch_size=32, class_mode='categorical') validation_generator = validation_datagen.flow_from_directory(validation_data_dir, batch_size=32, class_mode='categorical') model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) top_weights_path = os.path.join(os.path.abspath(model_path), 'top_model_weights.h5') callbacks_list = [ ModelCheckpoint(top_weights_path, monitor='val_acc', verbose=1, save_best_only=True), EarlyStopping(monitor='val_acc', patience=5, verbose=0) ] model.fit_generator(train_generator, samples_per_epoch=train_generator.nb_sample, nb_epoch=nb_epoch / 5, validation_data=validation_generator, nb_val_samples=validation_generator.nb_sample, callbacks=callbacks_list)
  • 23.
    • 
 • 
 final_weights_path= os.path.join(os.path.abspath(model_path), 'model_weights.h5') model.save_weights(final_weights_path) model_json = model.to_json() json_file = open(os.path.join(os.path.abspath(model_path), 'model.json'), 'w') json_file.write(model_json)
  • 24.
    def inference(trained_model_dir, test_data_dir,results_path): # load json and create model json_file = open(os.path.join(trained_model_dir, model_name), 'r') loaded_model_json = json_file.read() json_file.close() model = model_from_json(loaded_model_json) model.load_weights(os.path.join(trained_model_dir, model_weights)) # Read Data test_datagen = ImageDataGenerator(rescale=1. / 255) test_generator = test_datagen.flow_from_directory(test_data_dir, batch_size=batch_size, shuffle=False) # Calculate class posteriors probabilities y_probabilities = model.predict_generator(test_generator, val_samples=test_generator.nb_sample) # Calculate class labels y_classes = probas_to_classes(y_probabilities) filenames = [filename.split('/')[1] for filename in test_generator.filenames] ids = [filename.split('.')[0] for filename in filenames]
  • 25.
  • 26.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved. 

  • 27.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 28.
  • 29.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 30.
    • 
 • 
 • •
 
 
 

  • 31.
  • 32.
  • 33.
  • 34.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 35.
  • 36.
    # event { 'Records': [ { 'eventVersion':'2.0', 'eventSource': 'aws:s3', 'awsRegion': 'ap-northeast-2', # 'eventTime': '2017-12-13T03:28:13.528Z', # 'eventName': 'ObjectCreated:Put', 'userIdentity': {'principalId': 'AFK2RA1O3ML1F'}, 'requestParameters': {'sourceIPAddress': '123.24.137.5'}, 'responseElements': { 'x-amz-request-id': '1214K424C14C384D', 'x-amz-id-2': 'BOTBfAoB/gKBbn412ITN4t2psTW499iMRKZDK/CQTsjrkeSSzSdsDUMGabcdnvHeYNtbTDHoHKs=' }, 's3': { 's3SchemaVersion': '1.0', 'configurationId': 'b249eeda-3d48-4319-a7e2-853f964c1a25', 'bucket': { 'name': 'aws-summit-kr-2018', # 'ownerIdentity': { 'principalId': 'AFK2RA1O3ML1F' }, 'arn': 'arn:aws:s3:::aws-summit-kr-2018' }, 'object': { 'key': 'img/test_img.png', # 'size': 11733, # 'eTag': 'f2d12d123aebda1cc1fk17479207e838', 'sequencer': '125B119E4D7B2A0A48' } } } ] }
  • 37.
    • • • • # def handler(event, context): bucket_name= event['Records'][0]['s3']['bucket']['name'] file_path = event['Records'][0]['s3']['object']['key']
  • 38.
    • ACCESS_KEY = os.environ.get('ACCESS_KEY') SECRET_KEY= os.environ.get('SECRET_KEY') def downloadFromS3(strBucket, s3_path, local_path): s3_client = boto3.client('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY) s3_client.download_file(strBucket, s3_path, local_path) def uploadToS3(bucket, s3_path, local_path): s3_client = boto3.client('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY) s3_client.upload_file(local_path, bucket, s3_path)
  • 39.
    • • 
 def handler(event,context): bucket_name = event['Records'][0]['s3']['bucket']['name'] file_path = event['Records'][0]['s3']['object']['key'] file_name = file_path.split('/')[-1] downloadFromS3(bucket_name, file_path, '/tmp/'+file_name) 

  • 40.
    • def handler(event, context): bucket_name= event['Records'][0]['s3']['bucket']['name'] file_path = event['Records'][0]['s3']['object']['key'] file_name = file_path.split('/')[-1] downloadFromS3(bucket_name, file_path, '/tmp/'+file_name) downloadFromS3( 'aws-summit-kr-2018', 'xception_weights_tf_dim_ordering_tf_kernels.h5', '/tmp/.keras/xception_weights_tf_dim_ordering_tf_kernels.h5' )
  • 41.
    • from tensorflow.python.keras.applications.xception importXception from tensorflow.python.keras.preprocessing import image from tensorflow.python.keras.applications.resnet50 import preprocess_input, decode_predictions import numpy as np def predict(img_path): model = Xception(weights='imagenet') img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) return decode_predictions(preds, top=3)[0]
  • 42.
    • def handler(event, context): bucket_name= event['Records'][0]['s3']['bucket']['name'] file_path = event['Records'][0]['s3']['object']['key'] file_name = file_path.split('/')[-1] downloadFromS3(bucket_name, file_path, '/tmp/'+file_name) downloadFromS3( 'aws-summit-kr-2018', 'xception_weights_tf_dim_ordering_tf_kernels.h5', '/tmp/.keras/xception_weights_tf_dim_ordering_tf_kernels.h5' ) result = predict('/tmp/'+file_name) return result
  • 43.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 44.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 45.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 46.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 47.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 48.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 49.
  • 50.
  • 51.
  • 52.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 53.
  • 54.
  • 55.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 56.
    • 
 • 
 •
 docker run -v $(pwd):/outputs --name lambdapack -d amazonlinux:latest tail -f /dev/null
  • 57.
  • 58.
    dev_install() { yum -yupdate yum -y upgrade yum install -y wget gcc gcc-c++ cmake python36-devel python36-virtualenv python36-pip findutils zlib-devel zip unzip blas-devel lapack-devel atlas-devel }
  • 59.
    mkvirtualenv() { cd /home/ rm-rf env python3 -m virtualenv env --python=python3 source env/bin/activate } pip_install() { source /home/env/bin/activate pip install -U pip wheel pip install --use-wheel tensorflow==1.7.0 --no-deps pip install protobuf html5lib bleach --no-deps pip install --use-wheel pillow==4.0.0 pip install h5py }
  • 60.
    gather_pack() { cd /home/&& rm -rf pack && mkdir pack && cd pack cp -R /home/env/lib/python3.6/site-packages/* . cp -R /home/env/lib64/python3.6/site-packages/* . cp /outputs/index.py /home/pack/index.py find . -type d -name "test" -exec rm -rf {} + find -name "*.so" | xargs strip find -name "*.so.*" | xargs strip rm -r pip && rm -r pip-* && rm -r wheel && rm -r wheel-* find . | grep -E "(__pycache__|.pyc$)" | xargs rm -rf echo "stripped size $(du -sh /home/pack | cut -f1)" zip -FS -r1 /outputs/pack.zip * > /dev/null echo "compressed size $(du -sh /outputs/pack.zip | cut -f1)" }
  • 61.
  • 62.
  • 63.
    import boto3 def upload_to_s3(bucket,s3_path, local_path): client = boto3.client('s3', aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY) client.upload_file(local_path, bucket, s3_path) def update_lambda(function_name, bucket, s3_path): client = boto3.client('lambda', aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY) client.update_function_code( FunctionName=function_name, S3Bucket=bucket, S3Key=s3_path, ) uploadToS3(' ', ' /pack.zip', './pack.zip') update_lambda(' ', ' ', ' /pack.zip')
  • 64.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved. 

  • 65.
  • 66.
    def download_s3_object(strBucket, s3_path): importio file_obj = io.BytesIO() s3_client = boto3.client( 's3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY ) s3_client.download_fileobj(strBucket, s3_path, file_obj) return file_obj pack2 = download_s3_object('bucket-name', 'pack2.zip') import zipfile zip_ref = zipfile.ZipFile(pack2) zip_ref.extractall('/tmp') zip_ref.close() import sys sys.path.append("/tmp")
  • 67.
  • 68.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved. 

  • 69.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 70.
  • 72.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 73.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 74.
  • 75.
  • 77.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 78.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 79.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved. docker run --rm -v "$PWD":/var/task lambci/lambda:python3.6 my_module.my_handler docker run --rm -v "$PWD":/var/task lambci/lambda:python3.6 my_module.my_handler '{"some": "event"}'
  • 80.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 81.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 82.
  • 83.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 84.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 85.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 86.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 87.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.
  • 88.
    © 2018, AmazonWeb Services, Inc. or Its Affiliates. All rights reserved.