Skip to main content

AWS CDK construct for packaging lambda functions with dependencies

Project description

pipeline status coverage report

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aws_cdk_lambda_poetry_asset-0.2.2.tar.gz (6.1 kB view hashes)

Uploaded Source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page