AWS CDK construct for packaging lambda functions with dependencies
Project description
AWS CDK Lambda Poetry Asset
About
This is the cdk v2 version of the original asset, which is available at gitlab.
AWS CDK currently supports 3 kinds of "Assets":
- InlineCode - useful for one-line-lambdas
- AssetCode - one-file lambdas without dependencies
- S3Code - existing lambda packages already uploaded in an S3 bucket
There is, however, no support for more complex lambda function which require third party dependencies. This repository presents one possible approach to lambda packaging.
The construct is aware of libraries bundled in the AWS lambda runtime and automatically removes those for you to save space.
It also counts with compiled C dependencies such as NumPy and takes care of library stripping.
By setting the create_file_if_exists
to False
you can use it with a caching system, like Github Actions actions/cache@v3
. It will only run the build if the file doesnt exist at the output path already.
Usage
Suppose your project's directory structure looks like this:
my-project
├── business_logic
│ └── backend.py
└── functions
└── my_lambda.py
Then your stack would be:
from pathlib import Path
from aws_cdk import aws_lambda
from aws_cdk_lambda_poetry_asset.zip_asset_code import ZipAssetCode
class MyStack(core.Stack):
def __init__(self, app: core.App, id: str, **kwargs) -> None:
super().__init__(app, id, **kwargs)
work_dir = Path(__file__).parents[1]
aws_lambda.Function(
scope=self,
id='MyLambda',
code=ZipAssetCode(
work_dir=work_dir,
include=['functions', 'business_logic'],
file_name='my-lambda.zip',
create_file_if_exists=False
)
handler='functions/my_lambda.lambda_handler',
runtime=aws_lambda.Runtime.PYTHON_3_9
)
Poetry file hash
License
This code is released under MIT license.
Setup
Install poetry
pip install poetry
Install dependencies
poetry update
Run tests
Start docker first.
poetry run pytest --cov-report term-missing --cov=aws_cdk_lambda_poetry_asset tests
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
Hashes for aws_cdk_lambda_poetry_asset-0.2.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 808eb27c1361f9eecf3271b97ecf1ee925efcf71b25b3035ce2bb5769c13b300 |
|
MD5 | 317e109cb7498d7992db57c231bb4589 |
|
BLAKE2b-256 | ea076ff12d5545390f8000ca8c9368ed1903eb7c628b546cc93e2eeeb887281c |
Hashes for aws_cdk_lambda_poetry_asset-0.2.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ca28feb43c75cec297214beda49a2cd7baa55aaba2db33e437ccad6eb33179f |
|
MD5 | 53244cf2c220d1e04daa91ef7d3f9838 |
|
BLAKE2b-256 | 75fec26f4806222ac07af39aac77e34f79bac52631a39b8e595357b80317fd93 |