I tried running TensorFlow in AWS Lambda environment: Preparation

This article in 3 lines

1. Motivation

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/).

2. Result

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.

3. Experimental method

3.1. Preparing Native Modules in Amazon EC2 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

3.2. Prepare AWS Lambda deployment package

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 addingmain.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

3.3. Upload AWS Lambda deployment package

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.

3.4. Running AWS Lambda

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.

4. References

The following is a reference page.

Recommended Posts

I tried running TensorFlow in AWS Lambda environment: Preparation
I tried running TensorFlow
I tried running GAN in Colaboratory
I tried putting virtualenv in Cygwin environment
I tried connecting AWS Lambda with other services
I tried running pymc
I tried AWS CDK!
I tried AWS Iot
I tried to get an AMI using AWS Lambda
AWS Lambda now supports Python so I tried it
I put Selenium and headless chrome in AWS lambda. (Notes under Win10 environment, etc.)
[Python] Scraping in AWS Lambda
I tried using magenta / TensorFlow
I tried using AWS Chalice
[AWS] I tried using EC2, RDS, Django. Environment construction from 1
I tried the super-resolution algorithm "PULSE" in a Windows environment
I just built a virtual environment with AWS lambda layer
I tried running the offline speech recognition system Julius with python in the Docker virtual environment
I tried the TensorFlow tutorial 1st
Touch AWS Lambda environment variable support
Write AWS Lambda function in Python
I tried the TensorFlow tutorial 2nd
install tensorflow in anaconda + python3.5 environment
Try giving AWS Lambda environment variables?
[Python / AWS Lambda layers] I want to reuse only module in AWS Lambda Layers
I tried Grumpy (Go running Python).
I compared Node.js and Python in creating thumbnails using AWS Lambda
Install pip in Serverless Framework and AWS Lambda with Python environment
I tried running prolog with python 3.8.2.
I tried Line notification in Python
I tried running the TensorFlow tutorial with comments (_TensorFlow_2_0_Introduction for beginners)
I tried to delete bad tweets regularly with AWS Lambda + Twitter API
I tried to notify Zabbix Server of execution error of AWS Lambda function
[Django] sqlite version error when running python manage.py in aws cloud9 environment
I tried using NVDashboard (for those who use GPU in jupyter environment)
I tried to make PyTorch model API in Azure environment using TorchServe
I tried the TensorFlow tutorial MNIST 3rd
I tried to implement PLSA in Python
I tried to create a linebot (preparation)
I tried tensorflow for the first time
I tried to visualize AutoEncoder with TensorFlow
Environment when AWS Lambda includes native libraries
I tried playing a ○ ✕ game using TensorFlow
I tried to implement PLSA in Python 2
I tried to classify text using TensorFlow
I tried to implement ADALINE in Python
I tried to implement PPO in Python
I built a TensorFlow environment on windows10
I started Node.js in a virtual environment
Implemented DQN in TensorFlow (I wanted to ...)
I just did FizzBuzz with AWS Lambda
[AWS] Let's run a unit test of Lambda function in the local environment
I tried running the TensorFlow tutorial with comments (text classification of movie reviews)
Summary of points I was addicted to running Selenium on AWS Lambda (python)
I tried to reduce costs by starting / stopping EC2 collectively on AWS Lambda
I tried to create an environment to check regularly using Selenium with AWS Fargate
I tried to use Twitter Scraper on AWS Lambda and it didn't work.
I tried using AWS Rekognition's Detect Labels API
Differences between queryStringParameters and multiValueQueryStringParameters in AWS Lambda
I was able to recurse in Python: lambda
I tried playing a typing game in Python