We are moving towards using Poetry for dependency management and isolating our Python environments, instead of using pipenv. Our previous setup to deploy Lambda functions involved pipenv and Zappa. Since we’re revisiting this setup, I found myself struggling deploying Python functions on AWS Lambda. This was mainly because I tried too hard to manually package and deploy my Python script (within a Poetry virtual env) for AWS Lambda following the AWS documentation, without a framework like Zappa or Serverless. I didn’t manage to get it to work without any of these frameworks.

There are already many guidelines available on how to deploy Python Lambda functions, using Serverless Framework, with different types of dependency managers and frameworks that isolate your Python environments. The available guidelines don’t really cover step by step instructions to use Serverless in combination with Poetry, so that’s why I’ve written this article and actually it’s quite simple!

The steps are based on this article, but they’re using virtualenv for isolating their Python environment.

Prerequisites

  • You’ve installed Poetry
  • You’ve installed Node.js
  • You’ve installed awscli and configured it with a (default) profile
  • You’ve installed serverless globally (I chose to install through npm)
  • You have a project or script that you want to deploy to Lambda (using Poetry for isolation and as dependency manager)

If you don’t have a project yet and you want to code along, you can create a new folder, init poetry (poetry init) in that folder and add the handler.py file that is provided in the article I referred to. You also want to add numpy as dependency in your environment (poetry add numpy==1.18.1). Then you’ll have a somewhat same file structure as below:

numpy-test/
    handler.py
    pyproject.toml

#deployment #aws-lambda #dependency-management #python

Using Poetry with Serverless Framework to Deploy Python Lambda Functions
25.50 GEEK