The CDK Construct Library for AWS Lambda in Python
Project description
Amazon Lambda Python Library
---The APIs of higher level constructs in this module are experimental and under active development. They are subject to non-backward compatible changes or removal in any future version. These are not subject to the Semantic Versioning model and breaking changes will be announced in the release notes. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.
This library provides constructs for Python Lambda functions.
To use this module, you will need to have Docker installed.
Python Function
Define a PythonFunction
:
lambda_.PythonFunction(self, "MyFunction",
entry="/path/to/my/function", # required
runtime=Runtime.PYTHON_3_8, # required
index="my_index.py", # optional, defaults to 'index.py'
handler="my_exported_func"
)
All other properties of lambda.Function
are supported, see also the AWS Lambda construct library.
Python Layer
You may create a python-based lambda layer with PythonLayerVersion
. If PythonLayerVersion
detects a requirements.txt
or Pipfile
or poetry.lock
with the associated pyproject.toml
at the entry path, then PythonLayerVersion
will include the dependencies inline with your code in the
layer.
Define a PythonLayerVersion
:
lambda_.PythonLayerVersion(self, "MyLayer",
entry="/path/to/my/layer"
)
A layer can also be used as a part of a PythonFunction
:
lambda_.PythonFunction(self, "MyFunction",
entry="/path/to/my/function",
runtime=Runtime.PYTHON_3_8,
layers=[
lambda_.PythonLayerVersion(self, "MyLayer",
entry="/path/to/my/layer"
)
]
)
Packaging
If requirements.txt
, Pipfile
or poetry.lock
exists at the entry path, the construct will handle installing all required modules in a Lambda compatible Docker container according to the runtime
and with the Docker platform based on the target architecture of the Lambda function.
Python bundles are only recreated and published when a file in a source directory has changed. Therefore (and as a general best-practice), it is highly recommended to commit a lockfile with a list of all transitive dependencies and their exact versions. This will ensure that when any dependency version is updated, the bundle asset is recreated and uploaded.
To that end, we recommend using [pipenv
] or [poetry
] which have lockfile support.
Packaging is executed using the Packaging
class, which:
- Infers the packaging type based on the files present.
- If it sees a
Pipfile
or apoetry.lock
file, it exports it to a compatiblerequirements.txt
file with credentials (if they're available in the source files or in the bundling container). - Installs dependencies using
pip
. - Copies the dependencies into an asset that is bundled for the Lambda package.
Lambda with a requirements.txt
.
├── lambda_function.py # exports a function named 'handler'
├── requirements.txt # has to be present at the entry path
Lambda with a Pipfile
.
├── lambda_function.py # exports a function named 'handler'
├── Pipfile # has to be present at the entry path
├── Pipfile.lock # your lock file
Lambda with a poetry.lock
.
├── lambda_function.py # exports a function named 'handler'
├── pyproject.toml # your poetry project definition
├── poetry.lock # your poetry lock file has to be present at the entry path
Custom Bundling
Custom bundling can be performed by passing in additional build arguments that point to index URLs to private repos, or by using an entirely custom Docker images for bundling dependencies. The build args currently supported are:
PIP_INDEX_URL
PIP_EXTRA_INDEX_URL
HTTPS_PROXY
Additional build args for bundling that refer to PyPI indexes can be specified as:
entry = "/path/to/function"
image = DockerImage.from_build(entry)
lambda_.PythonFunction(self, "function",
entry=entry,
runtime=Runtime.PYTHON_3_8,
bundling=lambda.BundlingOptions(
build_args={"PIP_INDEX_URL": "https://your.index.url/simple/", "PIP_EXTRA_INDEX_URL": "https://your.extra-index.url/simple/"}
)
)
If using a custom Docker image for bundling, the dependencies are installed with pip
, pipenv
or poetry
by using the Packaging
class. A different bundling Docker image that is in the same directory as the function can be specified as:
entry = "/path/to/function"
image = DockerImage.from_build(entry)
lambda_.PythonFunction(self, "function",
entry=entry,
runtime=Runtime.PYTHON_3_8,
bundling=lambda.BundlingOptions(image=image)
)
Custom Bundling with Code Artifact
To use a Code Artifact PyPI repo, the PIP_INDEX_URL
for bundling the function can be customized (requires AWS CLI in the build environment):
from child_process import exec_sync
entry = "/path/to/function"
image = DockerImage.from_build(entry)
domain = "my-domain"
domain_owner = "111122223333"
repo_name = "my_repo"
region = "us-east-1"
code_artifact_auth_token = exec_sync(f"aws codeartifact get-authorization-token --domain {domain} --domain-owner {domainOwner} --query authorizationToken --output text").to_string().trim()
index_url = f"https://aws:{codeArtifactAuthToken}@{domain}-{domainOwner}.d.codeartifact.{region}.amazonaws.com/pypi/{repoName}/simple/"
lambda_.PythonFunction(self, "function",
entry=entry,
runtime=Runtime.PYTHON_3_8,
bundling=lambda.BundlingOptions(
build_args={"PIP_INDEX_URL": index_url}
)
)
This type of an example should work for pip
and poetry
based dependencies, but will not work for pipenv
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file aws-cdk.aws-lambda-python-1.138.2.tar.gz
.
File metadata
- Download URL: aws-cdk.aws-lambda-python-1.138.2.tar.gz
- Upload date:
- Size: 52.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.8.3 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3b8db1aa3f849cf0f24070b737ceea391028b191e58235c6331a53354de3f28 |
|
MD5 | 372bcd05c795df3b346c833a363e7063 |
|
BLAKE2b-256 | 0222a265228f1040f4bc16e5da37495be2059b62333ff4580ba3fe7c866a2967 |
File details
Details for the file aws_cdk.aws_lambda_python-1.138.2-py3-none-any.whl
.
File metadata
- Download URL: aws_cdk.aws_lambda_python-1.138.2-py3-none-any.whl
- Upload date:
- Size: 51.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.8.3 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f25c0ce7448f1faf10c0cd8e16078d17f783dba4b0252017d3444bb906471438 |
|
MD5 | 8bd5793c9881e33850bc9918d03d95b3 |
|
BLAKE2b-256 | df46d7bf57ca220934a4ca1e16366d9ccb9a813e6a20c1dc293247fe5c21c952 |