How to create a python lambda layer
If you have been using AWS Lambda, one of the nicest feature is that you can check, test and edit code directly from console. However, this is limited to 3 MB of package size, once you install pretty much any packages, it will exceed that quota.
AWS Lambda layer is used instead of installing many libraries for use in AWS Lambda functions. This gives you 250 MB headroom to install libraries.
This is easier that you think,
requirements:
docker, bash
- create a requirement file
lambdaLayer
|- requirements.txt
2. download the build script
for convenience, I have published the build script in here, you can select build with deps or without deps
wget 'https://gist.githubusercontent.com/thanakijwanavit/96b7597c0cdfc2833ddde4bccf48b180/raw/lambdaLayer.sh'
3. run the script
bash lambdaLayer.sh
This will create a file called package.zip for you to upload to aws console or use with sam.
4. upload the package
aws lambda publish-layer-version --layer-name 'test' --region 'us-east-1' --zip-file package.zip
5. set the permission
aws lambda add-layer-version-permission
--layer-name <your layer name>
--version-number 1
--statement-id <any string you want>
--principal '*' # this is public permission
--action lambda:GetLayerVersion
--region <us-east-1>
6. add it to your lambda function
you can add using sam, cli, or console
I have created a project template using sam, nbdev, and github action here