Try out a Lambda function locally before you upload it to the cloud.
A Python-based implementation of trylam.
Pylam runs as a small Python module. It can be installed as a development dependency, so that it adds nothing to the production runtime.
A typical setup involves doing the following:
-
Add a development dependency to the Lambda Python project:
pip install -f requirements-dev.txt -
Once installed, start up Pylam as follows:
python -m pylam lambda_function.lambda_handlerWhere
lambda_function.lambda_handleris the Lambda file name and function name respectively.NOTE: Two additional optional parameters can be specified:
- Argument 2: The path to the Lambda entrypoint. This can be useful if the function is not in the root directory of the project. Do not include leading or trailing slashes (/)
- Arrgument 3: The file extension. By default
.pyis used. To set to blank, pass the string "NONE".
A full invocation may look like the following:
python -m pylam lambda_function.lambda_handler src/lambdas .py -
To invoke Pylam, make an HTTP call on port 9000 (the default port). For example:
curl --request POST --url http://localhost:9000/ --data '{ "key1": "value1", "key2": [ "item21","item22"]}'NOTE: The
--datawill be used as theeventparameter to the Lambda function.After running, you should see the results of the invocation on the console. Example:
{"statusCode":200,"body":"\"Hello from Lambda!\""} -
To Use Visual Studio Code to debug the function: Add a launch configuration:
{ "version": "0.2.0", "configurations": [ { "name": "Hot dog debug", "type": "debugpy", "request": "launch", "module": "pylam", "args": ["lambda_function.lambda_handler", "."], "console": "integratedTerminal" } ] }NOTE:: The above configuration assumes the Lambda entry point is named
lambda_handler()and the name of the Lambda file islambda_function.py.Set a breakpoint in the Lambda handler, and invoke the function via a curl call:
curl --request POST --url http://localhost:9000/2015-03-31/functions/function/invocations --data '{ "key1": "value1", "key2": [ "item21","item22"]}'
From the root directory:
- Bump the version number in
pyproject.tomlandsrc/pylam/__init__.pySet it to something like 0.9.0 - Run
python -m build - Run
pip install --force-reinstall dist/pylam-0.9.0-py3-none-any.whl
This is a Python-based implementation of trylam, so for any questions, please open an issue in that repository.