Python Poetry for AWS Lambda Functions

on
mei 23, 2022
in

If you are familiar with Poetry, you might want to use it for your AWS Lambda Functions.
Creating an AWS Lambda Function ZIP Archive for your function code can be as simple as pip install -t lambda . ; cd lambda ; zip -x '*.pyc' -r ../lambda.zip .
This is all you need to know really, but below we will elaborate.

(We assume you already have Poetry installed.)

The Lambda Function

As an example, we provide a Lambda Function that and returns the AWS Account ID. To warrant the use of Poetry for dependency management, we use the qrcode package to return the result as an ASCII-encoded QR code.

import boto3  
import qrcode  
import io  


sts = boto3.client("sts")  


def text_qr(data):  
    qr = qrcode.QRCode()  
    qr.add_data(data)  
    f = io.StringIO()  
    qr.print_ascii(out=f)  
    f.seek(0)  
    return f.read()  


def handle(event, context):  
    response = sts.get_caller_identity()  
    account = response.get('Account', '???')  
    return text_qr(account)  


if __name__ == "__main__":  
    print(handle(None, None))

Bootstrap the Project

In an empty directory:

  • create an empty my_poetry_lambda/__init__.py file,
  • create a my_poetry_lambda/handler.py file with the example Lambda Function code shown above,
  • and create a pyproject.toml file with the following contents:
[tool.poetry]
name = "my_poetry_lambda"  # no hypens `-`!
version = "0.1.0"
description = "Example AWS Lambda Function using Poetry"
authors = ["Author <author@example.com>"]

[tool.poetry.dependencies]
python = "^3.8"
boto3 = "^1.23.5"
qrcode = "^7.3.1"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

You can also run poetry init to interactively create the pyproject.toml file.

Run poetry install to generate a poetry.lock file and install all dependencies.

Finally, we can test the function by running:

export AWS_PROFILE=xxxxxxxxxxxx  # your profile here
poetry run python my_poetry_lambda/handler.py

Package the Function

We use pip to install our function and its dependencies in the dist/lambda directory. Then we create a ZIP archive from its contents:

pip install -t dist/lambda .
cd dist/lambda
zip -x '*.pyc' -r ../lambda.zip .

Excluding the compiled bytecode files *.pyc from the archive, just reduces its size. The overhead of regenerating them is small, and they might need to be regenerated anyway if the bytecode magic number does not match that of the Python version in the Lambda runtime.

Using the Zip Archive in AWS Lambda

We have created a package my_poetry_lambda with a module handler containing the function handle. The value for the Handler in the Runtime Setting should thus be my_poetry_lambda.handler.handle.

Cloud Consultant with a passion for everything Cloud. Likes to automate all the things. Believes security is everyone's responsibility!
Share this article: Tweet this post / Post on LinkedIn