Serverless Python

1 min read serverless, python

Lab note What I wrote down while learning this by building with it. Short by design; the code is in pfeilbr/serverless-python-playground.

experiment with the serverless framework using python

based on https://serverless.com/blog/serverless-python-packaging/

session

# install `serverless framework` if not already installed
npm install serverless -g

# create serverless python project
serverless create --template aws-python3  --name numpy-test --path numpy-test

cd numpy-test

# create isolated virtualenv
virtualenv venv --python=python3
touch handler.py
code .
# add code for `handler.py`

# activate virtualenv
source venv/bin/activate

# run to test
python handler.py

pip install numpy

# save dependencies
pip freeze > requirements.txt
cat requirements.txt

# install dependencies (don't need to execute if starting from scratch)
pip install -r requirements.txt

python handler.py
npm init --force

# add serverless `serverless-python-requirements` plugin
npm install --save serverless-python-requirements

# deploy
sls deploy

# invoke lambda and output log
sls invoke -f numpy --log

# cleanup
sls remove

Keep learning

Get new guides and courses by email

An occasional email when I publish a new guide, course or post. No spam, and you can unsubscribe any time.

Sent with Buttondown. I never share your address. Prefer a feed? Use the RSS feed.