Git Product home page Git Product logo

cdk-python-library-layer's Introduction

English / 日本語

CDK Python Library Layer for CDK v2

cdk2-python-library-layer turns your private Python package into a Lambda layer. This library provides a CDK Construct that you can incorporate into your CDK script.

NOTE: The branch for CDK v1 (main) is no longer maintained.

What this library solves

This library turns a private Python package that pip cannot resolve into an AWS Lambda layer.

Installing the library

Please run the following command,

npm install https://github.com/kikuomax/cdk-python-library-layer.git#v0.2.1-v2

Using the library

Just import PythonLibraryLayer and new it. PythonLibraryLayer implements ILayerVersion.

Here is an example that makes a Lambda layer from a package defined in a lambda/libexample folder.

import * as path from 'path';
import { aws_lambda as lambda } from 'aws-cdk-lib';
import { Construct } from 'constructs';

import { PythonLibraryLayer } from 'cdk2-python-library-layer';

class YourCdkConstruct extends Construct {
    constructor(scope: Construct, id: string) {
        super(scope, id);
        this.layer = new PythonLibraryLayer(this, 'libexample', {
            description: 'Example Lambda layer',
            runtime: lambda.Runtime.PYTHON_3_8,
            entry: path.resolve('lambda', 'libexample'),
            compatibleArchitectures: [
                lambda.Architecture.ARM_64,
                lambda.Architecture.X86_64,
            ],
        });
    }
}

So far, a package must be configured for setuptools and have a structure similar to the following (src/ layout),

your_package/
  pyproject.toml
  setup.cfg
  src/
    your_package/

There is a working example in the example folder.

Background

I had a project that had a lot of Python Lambda functions that shared some code among them. Not to duplicate the shared code, I packaged them as a Python package and planned to reuse it as a Lambda layer. Since the package was specific to the project, I did not want to publish the package to any package repository.

First, I tried PythonLayerVersion, 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. As far as I looked into the source code, it looked that it just downloads packages listed in requirements.txt and copies them under a python folder. It did not look that it handles any scripts in an entry folder.

Thus, I had to somehow make a Lambda layer from my private package.

Trouble shooting

Docker failing with a cross-platform error

If 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:

WARNING: The requested image's platform (linux/arm64) does not match the detected host platform (linux/amd64/v3) and no specific platform was requested
exec /usr/bin/bash: exec format error

/home/ubuntu/cdk-python-library-layer/example/node_modules/aws-cdk-lib/core/lib/asset-staging.ts:395
      throw new Error(`Failed to bundle asset ${this.node.path}, bundle output is located at ${bundleErrorDir}: ${err}`);

If 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:

compatibleArchitectures: [
    lambda.Architecture.X86_64,
    lambda.Architecture.ARM_64,
]

Or allow Docker to build a cross-platform image. How to do it depends on your environment, though, this page would be helpful. On Ubuntu 22.04, I was able to solve this issue by installing qemu-user-static.

sudo apt-get install qemu-user-static

cdk-python-library-layer's People

Contributors

kikuomax avatar

Watchers

 avatar  avatar  avatar

cdk-python-library-layer's Issues

Support CDK v2.x

Please support CDK v2.x. Maybe in a dedicated branch like v2.x.

Docker platform is not specified

I got a report that cdk2-python-library-layer tried to build a Docker image for ARM64 on an x86_64 machine and impeded the CDK process. This may have been caused because the platform is not specified to the Docker process.

How about to follow what PythonLayerVersion does?
https://github.com/aws/aws-cdk/blob/ce18037b2ba2b872dd1ed21fda71fd2ee4701353/packages/%40aws-cdk/aws-lambda-python/lib/bundling.ts#L98-L104

    this.image = image ?? DockerImage.fromBuild(path.join(__dirname, '../lib'), {
      buildArgs: {
        ...props.buildArgs,
        IMAGE: runtime.bundlingImage.image,
      },
      platform: architecture.dockerPlatform,
    });

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.