AWS Lambda with TensorFlow
December 6, 2017 ยท View on GitHub
This is a demo that integrates Tensorflow with AWS Lambda.
Requirements
- An AWS account
Get started
Once inside this example's directory, install Serephem.
$ npm i
Deploy
The Serephem plugin will take care of retrieving all the TensorFlow dependencies and injecting them into the Serverless Lambdas zip artifact prior to deploying to AWS.
Make sure you configure the serverless.yml file to your necessities before deploying (i.e. region, stage, profile...)
$ sls deploy -v
Remember to run
sls removewhen finished with this demo.
Lambdas
This project features 2 Lambdas:
- A TensorFlow addition of two numbers
- A NumPy that prints properties of a matrix
Tensorflow add
File
tensorflow_test.py
Description
It uses the TensorFlow library to add two integer numbers.
Input
Two integer numbers a and b. The Lambda test event JSON looks as follows:
{
"a": 30,
"b": 90
}
Result
The Lambda will add those numbers using the tf.add function. The result looks as follows:
{
"message": "TensorFlow \"add\" function test: 30 + 90",
"result": "120"
}
NumPy matrix properties
The TensorFlow package includes NumPy, since it is a dependency.
File
numpy_test.py
Description
Creates a 2x3 matrix of integers and returns some of its NumPy properties.
Input
None.
Result
{
"message": "NumPy test",
"event": {},
"attrs": {
"ndim": 2,
"itemsize": 8,
"dtype.name": "int64",
"size": 6
}
}
Local testing of Serephem plugin
In case you want to use this example to locally test the Serephem plugin, you can do the following:
-
Open a new terminal window and go to the Serephem root folder
-
Run
npm link -
Return to the terminal window for the
tensorflow-lambdaexample -
Run
npm link serverless-ephemeral -
Run
sls package -vto see your changes