Skip to content

Instantly share code, notes, and snippets.

@mcclory
Last active March 8, 2021 17:27
Show Gist options
  • Save mcclory/e63f97a8bee14b8a6e7e5303f36a3efb to your computer and use it in GitHub Desktop.
Save mcclory/e63f97a8bee14b8a6e7e5303f36a3efb to your computer and use it in GitHub Desktop.
Python + Poetry in serverless - A few things to make sure you have in your serverless.yml file
# Distribution / packaging
.Python
.DS_Store
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
.venv # venv from poetry w/
# Serverless directories and stuff
.serverless
_dev
requirements.txt
[virtualenvs]
in-project = true
frameworkVersion: '2'
custom:
pythonRequirements:
dockerizePip: non-linux
scripts:
hooks:
'package:initialize': poetry export -f requirements.txt > requirements.txt
provider:
name: aws
runtime: python3.8
lambdaHashingVersion: 20201221
tracing:
lambda: true
package:
# include:
# - include-me.py
# - include-me-dir/**
exclude:
- node_modules
- .env
- package-lock.json
- package.json
- poetry.toml
- poetry.lock
- .flake8
- .git
- .gitignore
- .pylintrc
- _data
- docker-compose.yml
- .github
- .pytest_cache
- .venv
- tests
useDotenv: true
plugins:
- serverless-python-requirements
@mcclory
Copy link
Author

mcclory commented Mar 8, 2021

So I use Poetry because it's awesome, but the way serverless is setup, it still looks for a good 'ol requirements.txt... so... easiest way around this is twofold:

  • Add a custom.scripts.hooks.package:initialize script to dump out your requirements.txt from your poetry setup
  • Add requirements.txt to your .gitignore file

I've also thrown in some excludes to make sure my local stuff doesn't overload the lambda env (since there's a 75MB limit)...

Generally... I tend to:

  • Set up poetry with the in-project set to true which generates a .venv directory in the project (I like to keep things together)
  • given that, my .gitignore file has some extra stuff in it to prevent uploading common stuff that shouldn't go to the repo (dependencies and such...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment