Skip to content
Lets you import Tensorflow + Keras from an AWS lambda
Branch: master
Clone or download
Latest commit 2aefdc8 Jan 18, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
arn_tables us-east-1 picked up an old (inaccurate) version Jan 18, 2019
build Upgrade to multi-target builds Jan 13, 2019
build_targets Publish across regions; generate arn tables; load sample h5 model in … Jan 18, 2019
src Publish across regions; generate arn tables; load sample h5 model in … Jan 18, 2019
.gitignore Upgrade to multi-target builds Jan 13, 2019
LICENSE Create LICENSE Jan 18, 2019
README.md

README.md

Tensorflow-Lambda-Layer

Lets you import Tensorflow + Keras from an AWS lambda

What is this?

It's a lambda layer that includes Tensorflow, Keras, and Numpy. You can use it to deploy serverless machine learning models.

Serverless is especially nice for when you want to serve a model that will be accessed infrequently, without paying for an always-on ec2 instance.

If you're a single developer or small org and all you want to do is show off your binary classifier, it's usually possible to stay within the free tier limits if you set things up right.

And even if you're larger, serverless brings a lot of benefits, like transparent scaling and the ability to mostly ignore the hardware.

The problem is, some packages (like Tensorflow) end up hard to use. This repo is an attempt to alleviate that problem.

How do I use it?

Pick an ARN from the tables for the region and Tensorflow version you want (for example, arn:aws:lambda:us-west-2:347034527139:layer:tf_1_11_keras:1)

Tables:

In the AWS lambda management console, create a new function you want to use Tensorflow in, or pick an existing function.

Click

  • layers
    • add layer
      • provide a layer version ARN

Paste the ARN in, add the layer, and you should be able to use the libraries as normal.

Build it yourself

The code involved in generating these layers is all included in src and build_targets. The code is a collection of shell scripts that constructs, uploads, and publishes the lambda zipfiles. It reads AWS credentials from $HOME/.aws, and spawns an instance to actually run the build process (note: does not shut it off automatically).

If you have a set of dependencies you'd like built into a layer, you should add a new directory, following this structure:

  • requirements.txt: The pip packages to install
  • description.txt: The description that will be attached to the published layer
  • hook.sh: Extra commands that run as the last phase of the build step
  • test.py: A python file that should trigger an access of every file used by the library in the course of its execution

If you send this in a pull request and it seems like something people will use, I'll run the build the next chance I get and add it here.

Or you can run it yourself with aws-build-lambda.sh. Make sure you know what this script is doing before you run it!

I think you should build a layer with <packages x,y,z>

Let me know! It's fairly low cost to add a new layer. It doesn't even need to involve tensorflow.

Caveats

This repo will minimize a deployment package by:

  • Only copying source files and files accessed when test.py is run
  • Stripping symbols from shared objects

These steps usually produce good results, but they may end up leaving out something essential. If you see an error that you're not expecting, file an issue including the error and the code that generates it, and I'll see what I can fix.

Tensorflow 1.12 + Keras clocks in at 282M, which is too big to fit into a lambda layer. Unless I can find a way to further reduce the size, I can't support this combination.

You can’t perform that action at this time.