Skip to content

Latest commit

 

History

History

serverless

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Deploying a PyTorch Lambda Function with Serverless Framework

Serverless Framework

This isn't meant to be a full tutorial about Serverless Framework, just a demonstration of how to deploy this Lambda Layer using the tool. If you want more details about Serverless in general I recommend reading through their docs and working through their Hello World examples to start.

Serverless Configuration

Deploying a Lambda function utilizing the PyTorch Lambda Layer is pretty straight-forward with Serverless. You just need to create a layers block that points to the zip artifact created by this script (with optional compatible runtimes):

layers:
  PyTorch:
    package:
      artifact: layers/PyTorch.zip
    compatibleRuntimes:
      - python3.8

and reference the layer inside your function configuration as {configuredLayerName}LambdaLayer, where configuredLayerName is the name of the yaml block configuring your layer above. In this case, "PyTorch":

functions:
  pytorch-example:
    handler: main.main
    name: PyTorch-Example
    timeout: 60
    memorySize: 1024
    description: Demonstrates usage of the PyTorch Lambda Layer
    layers:
      - {Ref: PyTorchLambdaLayer}

You can find the full Serverless configuration for this example here

Deploying

These steps assume you've already built the Lambda Layer using this repo and have the zipped layer stored as layers/PyTorch.zip. You can point this to another artifact in serverless.yml if needed.

  1. Install dependencies:

    This will install Serverless Framework iteself as well as the serverless-deployment-bucket plugin. This plugin will create the S3 bucket for Serverless deployment artifacts to be stored. If you already have a bucket you are using then this isn't necessary and you can remove the plugin from the config file and point the deploymentBucket to your existing bucket.

    npm install
  2. Configure AWS provider:

    Rename the deploymentBucket in the provider block of serverless.yml to the name of the S3 bucket you want to deploy. Note this name must be globably unique. Also, feel free to change the region of deployment for all of these resources.

  3. Configure your AWS credentials to point to your account. There are several ways to do this.

  4. Run Serverless deploy:

    sls deploy