Template Project to help you creating your python lambda Layer using serverless framework
- Create your own repo by clicking on [use this template] button
- Name your repo as python-YOUR-LAYER-NAME-layer [prefix and suffix are required]
- clone your repo to your local environment
WARNING: You should have pip and virtualenv installed
./initialize-layer.sh python-YOUR-LAYER-NAME-layer [module1 module2 ... moduleN]or if your repo has the name of the layer
./initialize-layer.sh $(basename $PWD) [module1 module2 ... moduleN]or if you have a requirements.txt file, just add it to the project and run
./initialize-layer.sh $(basename $PWD)example:
./initialize-layer.sh python-numpy-pandas-layer numpy pandasOnce initialized, because the serverless.yml file comes with no account information, you can deploy it right away by passing the account as a parameter
sls deploy --account 012345678900But you can also edit the file and set your account number to it
The default region in the file is us-west-2 but if you want to deploy it to another region you can pass it as a parameter as well
sls deploy --account 012345678900 --region us-east-1When deploying your lambda layer will have python3X as a prefix where X is the minor versions of your python3. This will be important when you want to use the published layers within your serverless project
You might want to add new packages to the same layer later down the road. The way you do that is by loading the virtualenv from the local python directory and then installing the package you want using pip
# load the virtualenv
source ./python/bin/activate
# install your package
$VIRTUAL_ENV/bin/pip install requests
# re-deploy the layer
sls deploy --account 012345678900 --region xx-xxxx-1On your serverless.yml file you just add the layers to the /functions?/ using the cloud formation reference like this:
If you are using Python 3.7
functions:
yourFunc:
layers:
- ${cf:python37-YOUR-LAYER-NAME-layer.latestVersionARN}If you are using Python 3.8
functions:
yourFunc:
layers:
- ${cf:python38-YOUR-LAYER-NAME-layer.latestVersionARN}