{"id":15354656,"url":"https://github.com/kikuomax/cdk-python-library-layer","last_synced_at":"2026-01-06T19:06:10.182Z","repository":{"id":85896893,"uuid":"433673835","full_name":"kikuomax/cdk-python-library-layer","owner":"kikuomax","description":"Turns a private Python package into an AWS Lambda layer","archived":false,"fork":false,"pushed_at":"2023-03-23T03:39:21.000Z","size":340,"stargazers_count":0,"open_issues_count":4,"forks_count":0,"subscribers_count":3,"default_branch":"v2.x","last_synced_at":"2025-02-01T22:16:24.997Z","etag":null,"topics":["aws-cdk","aws-lambda","aws-lambda-layer","python"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kikuomax.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-01T03:48:22.000Z","updated_at":"2021-12-01T16:01:39.000Z","dependencies_parsed_at":"2024-10-16T03:01:11.840Z","dependency_job_id":null,"html_url":"https://github.com/kikuomax/cdk-python-library-layer","commit_stats":{"total_commits":19,"total_committers":2,"mean_commits":9.5,"dds":0.4736842105263158,"last_synced_commit":"26da54995bbf57670637055ed12337ce3475f076"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kikuomax%2Fcdk-python-library-layer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kikuomax%2Fcdk-python-library-layer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kikuomax%2Fcdk-python-library-layer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kikuomax%2Fcdk-python-library-layer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kikuomax","download_url":"https://codeload.github.com/kikuomax/cdk-python-library-layer/tar.gz/refs/heads/v2.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245908745,"owners_count":20692159,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws-cdk","aws-lambda","aws-lambda-layer","python"],"created_at":"2024-10-01T12:20:19.382Z","updated_at":"2026-01-06T19:06:10.147Z","avatar_url":"https://github.com/kikuomax.png","language":"TypeScript","readme":"English / [日本語](./README_ja.md)\n\n# CDK Python Library Layer for CDK v2\n\n`cdk2-python-library-layer` turns your private Python package into a Lambda layer.\nThis library provides a [CDK Construct](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Construct.html) that you can incorporate into your CDK script.\n\n**NOTE**: The branch for CDK v1 (`main`) is no longer maintained.\n\n## What this library solves\n\nThis library turns a private Python package that `pip` cannot resolve into an [AWS Lambda layer](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html).\n\n## Installing the library\n\nPlease run the following command,\n\n```sh\nnpm install https://github.com/kikuomax/cdk-python-library-layer.git#v0.2.1-v2\n```\n\n## Using the library\n\nJust import `PythonLibraryLayer` and `new` it.\n`PythonLibraryLayer` implements [`ILayerVersion`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.ILayerVersion.html).\n\nHere is an example that makes a Lambda layer from a package defined in a `lambda/libexample` folder.\n\n```js\nimport * as path from 'path';\nimport { aws_lambda as lambda } from 'aws-cdk-lib';\nimport { Construct } from 'constructs';\n\nimport { PythonLibraryLayer } from 'cdk2-python-library-layer';\n\nclass YourCdkConstruct extends Construct {\n    constructor(scope: Construct, id: string) {\n        super(scope, id);\n        this.layer = new PythonLibraryLayer(this, 'libexample', {\n            description: 'Example Lambda layer',\n            runtime: lambda.Runtime.PYTHON_3_8,\n            entry: path.resolve('lambda', 'libexample'),\n            compatibleArchitectures: [\n                lambda.Architecture.ARM_64,\n                lambda.Architecture.X86_64,\n            ],\n        });\n    }\n}\n```\n\nSo far, a package must be configured for [`setuptools`](https://setuptools.pypa.io/en/latest/index.html) and have a structure similar to the following (`src/` layout),\n\n```\nyour_package/\n  pyproject.toml\n  setup.cfg\n  src/\n    your_package/\n```\n\nThere is a working example in the [`example`](./example) folder.\n\n## Background\n\nI had a project that had a lot of Python Lambda functions that shared some code among them.\nNot to duplicate the shared code, I packaged them as a Python package and planned to reuse it as a Lambda layer.\nSince the package was specific to the project, I did not want to publish the package to any package repository.\n\nFirst, I tried [`PythonLayerVersion`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda-python.PythonLayerVersion.html), but it did not work as I intended; more preceisely, I could not figure out how to achieve what I wanted to do with it.\nAs far as I looked into the [source code](https://github.com/aws/aws-cdk/tree/v1.134.0/packages/%40aws-cdk/aws-lambda-python/lib), it looked that it just downloads packages listed in `requirements.txt` and copies them under a `python` folder.\nIt did not look that it handles any scripts in an `entry` folder.\n\nThus, I had to somehow make a Lambda layer from my private package.\n\n## Trouble shooting\n\n### Docker failing with a cross-platform error\n\nIf the platform of your machine running Docker is different from the target platform (`compatibleArchitectures`) of the layer, you may face an error message similar to the following:\n```\nWARNING: The requested image's platform (linux/arm64) does not match the detected host platform (linux/amd64/v3) and no specific platform was requested\nexec /usr/bin/bash: exec format error\n\n/home/ubuntu/cdk-python-library-layer/example/node_modules/aws-cdk-lib/core/lib/asset-staging.ts:395\n      throw new Error(`Failed to bundle asset ${this.node.path}, bundle output is located at ${bundleErrorDir}: ${err}`);\n```\n\nIf you are building a layer compatible with multiple platforms, change the order of `compatibleArchitectures` so that the first item matches your machine's platform; e.g., suppose your machine is based on x86_64:\n```ts\ncompatibleArchitectures: [\n    lambda.Architecture.X86_64,\n    lambda.Architecture.ARM_64,\n]\n```\n\nOr allow Docker to build a cross-platform image.\nHow to do it depends on your environment, though, [this page](https://docs.docker.com/build/building/multi-platform/) would be helpful.\nOn Ubuntu 22.04, I was able to solve this issue by installing `qemu-user-static`.\n```sh\nsudo apt-get install qemu-user-static\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkikuomax%2Fcdk-python-library-layer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkikuomax%2Fcdk-python-library-layer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkikuomax%2Fcdk-python-library-layer/lists"}