Skip to content

Amazon Web Services

AWS Lambda

Preparing a layer for Python runtime

For packages that does not involve C extensions, you can just install them in a python directory and zip it up.

pip3 install -t python/ -r requirements.txt
zip -9r layer.zip python/

Things get trickier when you need to install packages with native C extensions, like regex and numpy.

AWS provides the runtime as Docker images on ECR: https://gallery.ecr.aws/lambda/python. To use one, you need to run pip install inside such an environment. Note that in order to run shell commands, you must clear the entrypoint:

docker run --rm -it \
  -v "$PWD":/var/task \
  --entrypoint="" \
  public.ecr.aws/lambda/python:3.8 bash

Then you can install packages with native C extensions:

pip3 install -t python/ -r requirements.txt

The image does not contain the zip command, so you need to zip it up outside the container. After the zip package is created, you can upload it to AWS Lambda as a layer.

iBug's layers

iBug has a single layer that bundled requests (latest), python-telegram-bot (v13.15) and lxml (latest). The python-telegram-bot library has an additional patch (PR #3055) applied:

python/telegram/utils/helpers.py
--- python/telegram/utils/helpers.py
+++ python/telegram/utils/helpers.py
@@ -167,7 +167,7 @@
         elif entity_type == 'text_link':
             escape_chars = r'\)'
         else:
-            escape_chars = r'_*[]()~`>#+-=|{}.!'
+            escape_chars = r'\_*[]()~`>#+-=|{}.!'
     else:
         raise ValueError('Markdown version must be either 1 or 2!')

AWS Simple Email Service

Example Lambda event payload via SNS: https://docs.aws.amazon.com/ses/latest/dg/receiving-email-action-lambda-event.html

Email size limit: 40 MB if stored to S3, 150 KB if published to SNS (Source)