At the study session, I made a presentation entitled "I tried to classify texts using TensorFlow". I was able to get some results, so I decided to use it in earnest, and my favorite AWS Lambda and [Amazon API Gateway] I decided to build an API using (https://aws.amazon.com/jp/api-gateway/).
I used TensorFlow to calculate "1 + 2" and got the correct result "3". At least, I was able to confirm that TensorFlow can be ʻimport` in the AWS Lambda environment.
AWS Lambda runs on Amazon Linux. For the specific environment, see "[Lambda runtime environment and available libraries --AWS Lambda](http://docs.aws.amazon.com/ja_jp/lambda/latest/dg/current-supported-versions.html" on the official page. ) ”.
This time I want to operate in the Tokyo region, so I can see from the above page that I should use ʻami-383c1956. Please note that the AMI is different for each region. I created a
t2.micro instance of ʻami-383c1956
on Amazon EC2 and prepared a native module.
The completed ZIP file is placed at gist: 9b11f081186b98fe130e as ʻami-383c1956-python2.7-tensorflow0.7.1-20160320.zip. The file size is about 23MiB and the SHA-1 hash value is
3157f010853fee1769a1149afbed15383bf2be96`.
The construction procedure is as follows.
#Check the environment
$ uname -a
Linux ip-xxx-xxx-xxx-xxx 4.1.10-17.31.amzn1.x86_64 #1 SMP Sat Oct 24 01:31:37 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
$ cat /etc/system-release
Amazon Linux AMI release 2015.09
#Update and install packages
$ sudo yum -y update
$ sudo yum -y upgrade
$ sudo yum -y groupinstall "Development Tools"
$ sudo yum -y install python27-devel python27-pip gcc
#Prepare virtualenv
$ export ENV_ROOT=~/env
$ virtualenv ${ENV_ROOT}
$ source ${ENV_ROOT}/bin/activate
#Check Python version and path
(env)$ python2.7 --version
Python 2.7.10
(env)$ which python2.7
~/env/bin/python2.7
(env)$ which pip2.7
~/env/bin/pip2.7
#Install TensorFlow and dependent libraries
(env)$ pip2.7 install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.7.1-cp27-none-linux_x86_64.whl
Successfully installed numpy-1.10.4 protobuf-3.0.0b2 setuptools-20.3.1 six-1.10.0 tensorflow-0.7.1 wheel-0.29.0
(env)$ deactivate
#Create a ZIP file
$ export OUT_FILE=~/ami-383c1956-python2.7-tensorflow0.7.1-20160320.zip
$ cd ${ENV_ROOT}/lib/python2.7/site-packages
$ touch google/__init__.py
$ zip -r9v ${OUT_FILE} . --exclude \*.pyc
$ cd ${ENV_ROOT}/lib64/python2.7/site-packages
$ zip -r9v ${OUT_FILE} . --exclude \*.pyc
$ sha1sum ${OUT_FILE}
3157f010853fee1769a1149afbed15383bf2be96 /home/ec2-user/ami-383c1956-python2.7-tensorflow0.7.1-20160320.zip
This time, I used the following code. It's a very simple code that just calculates "1 + 2".
main.py
import tensorflow as tf
def lambda_handler(event, context):
a = tf.constant(1)
b = tf.constant(2)
with tf.Session() as sess:
return str(sess.run(a + b))
I created a deployment package by copying ʻami-383c1956-python2.7-tensorflow0.7.1-20160320.zipand adding
main.py`.
$ cd /project/path/tensorflow
$ cp ami-383c1956-python2.7-tensorflow0.7.1-20160320.zip test.zip
$ cd /project/path/tensorflow/lambda
$ vi main.py
$ zip -r9v ../test.zip . --exclude \*.pyc
This time I created a Lambda function from the web console and uploaded test.zip
.
The default memory size is 128MB. The timeout value has been extended to 10 seconds.
When I pressed the "Test" button from the web console and executed the Lambda function, I was able to get the correct result of "3". At least from the second time onward, the execution time was less than 100ms, and the maximum memory usage was 33MB.
The following is a reference page.
Recommended Posts