Pyjoyment.Command.Author.generate.lambda_zip - Lambda ZIP generator command
Usage: APPLICATION generate lambda-zip [OPTIONS]
./myapp.py generate lambda-zip
python -m MyApp generate lambda-zip
python -m MyApp generate lambda-zip --arch arm64
python -m MyApp generate lambda-zip --python 3.15t
python -m MyApp generate lambda-zip --no-layer
python -m MyApp generate lambda-zip --runtime python3.14
python -m MyApp generate lambda-zip --function-only
python -m MyApp generate lambda-zip --function-only --layer-package Pyjo-AWS
python -m MyApp generate lambda-zip --compress public
python -m MyApp generate lambda-zip --compress public --compress-encoding br --compress-encoding gzip
Options:
-a, --arch <name> Architecture of the function, "arm64" or
"x86_64", defaults to "x86_64"
-c, --compress <path> Write the variants of gzip, and of brotli
and zstd if the machine has them, of the
static files of a directory in the
function, can be repeated
-C, --compress-pattern <glob> Name of the files that --compress
compresses, can be repeated, defaults to
the formats of text
-e, --compress-encoding <name> Encoding of the variants that --compress
writes, "br", "zstd" or "gzip", can be
repeated, defaults to the ones that the
machine has
--function-only Only the ZIP of the function, for layers
of Python and Pyjoyment that are published
already
-h, --help Show this summary of available options
-l, --layer-package <name> Distribution of the dependencies that the
layer of Pyjoyment has too, as "Pyjo-AWS",
can be repeated
--no-layer Put Python and Pyjoyment into the ZIP of
the function, without layers
-p, --python <version> Version of Python, as "3.14" or "3.15t"
for the free-threaded build, defaults to
the version that runs the generator
-r, --runtime <name> Runtime of the function, "provided.al2023"
or one of Python, as "python3.14",
defaults to "provided.al2023"
Pyjoyment.Command.Author.generate.lambda_zip generates the ZIP files
of an event function of AWS Lambda on a custom runtime, as
provided.al2023, in dist: function.zip, with the application, its
dependencies and the bootstrap that runs it, python.zip, a layer with
Python, which Lambda extracts in /opt, so that the function runs on any
Python, as the free-threaded build or a version that the runtimes of Python of
Lambda do not have yet, and pyjoyment.zip, a layer with Pyjoyment.
The bootstrap runs the script of the application, or its package, which
is the runtime of the function itself, as Pyjoyment.Command.lambda
detects, with the runtime client of Pyjo.Server.Lambda. A function
whose Function URL streams its responses has
PYJO_LAMBDA_INVOKE_MODE=RESPONSE_STREAM in its environment, as
Pyjo.Server.Lambda.Pyjo_Server_Lambda.invoke_mode says.
The files are made with uv, which has to be on PATH. Python is the build
of python-build-standalone that uv installs, for Linux with glibc and the
architecture of --arch: the interpreter and its standard library,
without IDLE, Tkinter, turtledemo, ensurepip and pip, which a function does
not use. The dependencies are the ones of pyproject.toml, which
Pyjoyment.Command.Author.generate.pyproject generates, locked with
uv.lock if there is one, or the ones of the inline metadata of PEP 723 of
a Pyjoyment.Lite script that has it and no pyproject.toml, locked
with its .lock file if there is one, with the wheels of Amazon Linux 2023,
manylinux_2_34. Without either, the generator fails with the commands that
add them, before it runs uv. The application is the files of the current directory,
without hidden ones, __pycache__ and dist, and a file of it that starts
with #!, as the wrapper of AWS_LAMBDA_EXEC_WRAPPER, is executable.
Python cannot write its bytecode in /var/task and /opt, which are
read-only, so the generator compiles it with the Python of the same version
that uv installs for the machine that it runs on, which installs the
dependencies too, without a link in the directory of the executables of uv,
with the
invalidation mode unchecked-hash, which Python uses without checking the
source. The files of the same input are the same, as the times in the ZIP are
fixed, so that a deployment that compares their hashes, as the one of
OpenTofu, changes only what has changed. They are written anew each time.
The layer of Pyjoyment has the distribution Pyjoyment that the dependencies
install, the files that its RECORD names, with the packages Pyjo and
Pyjoyment, and the distributions of --layer-package, as Pyjo-AWS,
with their bytecode, in python/lib/python3.X/site-packages, which the
Python of python.zip and the runtime client of the runtime of Python of
Lambda of the same version both put in their path, the runtime client unless
PYTHONPATH is set, so that one layer serves the functions of both, and a
function can take a layer of Pyjoyment that is published already in its place.
A wrapper of AWS_LAMBDA_EXEC_WRAPPER runs in the place of that runtime
client, so its function names the directory in PYTHONPATH. Another
distribution of the namespace Pyjo, as Pyjo-MCP, stays in the
function, where the package Pyjo of the layer finds it, as
pkgutil.extend_path() keeps the namespace open, but a distribution that
the layer has too is the one of the layer.
With --runtime of a runtime of Python of Lambda, as python3.14, the
function runs on the Python of Lambda, of the version of the runtime, without
python.zip and without bootstrap, with the handler of the application,
as app.handler for a script app.py that ends with
handler = app.start().
With --no-layer, Python and Pyjoyment are in function.zip, Python in
the directory python, and the function needs no layer, at the cost of a
larger ZIP for each deployment.
With --compress, the function has the precompressed variants of the
static files of a directory of the application, as public, which
Pyjoyment.Static serves to the clients that take them: .gz of
gzip, and .zst of compression.zstd of Python 3.14 or of the
command zstd, and .br of the command brotli, if the machine has
them, without a variant that is not smaller than its file. It compresses the
files of
compress_patterns
of generate dockerfile, the formats of text, or the names of
--compress-pattern, and leaves out the files smaller than
min_compress_size of the
renderer.
With --compress-encoding, it writes the variants of those encodings only,
as br and gzip for a function behind CloudFront, which forwards no
other encoding of Accept-Encoding to the origin, and fails when the
machine has no compressor of one of them.
With --function-only, the generator writes function.zip alone, for the
layers of Python and of Pyjoyment that are published already, as the public
ones of Pyjoyment-Lambda-Layers, which the same
generator builds, so that the bootstrap finds the Python of the layer in
/opt/python/bin. The function leaves out Pyjoyment and the distributions
of --layer-package, as Pyjo-AWS, which the public layer of Pyjoyment
has, as with its own layer, and the generator tells their versions of its
dependencies, which the layer of Pyjoyment is to have. On a runtime of Python,
the function takes the layer of Pyjoyment alone, of the same version of
Python.
This is a core command, that means it is always enabled and its code a good
example for learning to build new commands, you’re welcome to fork it.
See Pyjoyment.Commands for a list of commands that are available by
default.