Skip to main content

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
        )

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

Create a release

This project will automatically create a github release when a PR is merged into the main branch. The title of the PR must adhere to angular commit message format in order for it to calculate a new version number.

Example PR titles:

feat: mindblowing feature

fix: bug thats been around for ever

Types

From angular documentation

Must be one of the following:

  • build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
  • ci: Changes to our CI configuration files and scripts (example scopes: Circle, BrowserStack, SauceLabs)
  • docs: Documentation only changes
  • feat: A new feature
  • fix: A bug fix
  • perf: A code change that improves performance
  • refactor: A code change that neither fixes a bug nor adds a feature
  • test: Adding missing tests or correcting existing tests

License

This code is released under MIT license.

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.5.2.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file aws_cdk_lambda_poetry_asset-0.5.2.tar.gz.

File metadata

  • Download URL: aws_cdk_lambda_poetry_asset-0.5.2.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.28.2 requests-toolbelt/0.10.1 urllib3/1.26.15 tqdm/4.65.0 importlib-metadata/6.1.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.10

File hashes

Hashes for aws_cdk_lambda_poetry_asset-0.5.2.tar.gz
Algorithm Hash digest
SHA256 a6afe52acb57153554eec7cadf344574df41220298a9dc5bb0d8e82bb79cc06d
MD5 0a124aa75555a668adf1491109adc332
BLAKE2b-256 6b2893d3b5a36cf796a0d5ee8b38e519dcfbb485b55349322dac457b1787857b

See more details on using hashes here.

File details

Details for the file aws_cdk_lambda_poetry_asset-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: aws_cdk_lambda_poetry_asset-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.28.2 requests-toolbelt/0.10.1 urllib3/1.26.15 tqdm/4.65.0 importlib-metadata/6.1.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.10

File hashes

Hashes for aws_cdk_lambda_poetry_asset-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 136c421354ddd805420dcdd12430f8b095a302775f508ade63436dd9cb9ab426
MD5 3c3db4b5cd63f90a1fad8ab8abb91d8e
BLAKE2b-256 82446921d447b1e0016b960d6dd4282bc1b8b80b38e4dbfffbfaeb3eac256852

See more details on using hashes here.

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