Coder Social home page Coder Social logo

aws-samples / cdk-lambda-powertools-python-layer Goto Github PK

View Code? Open in Web Editor NEW
14.0 9.0 1.0 599 KB

CDK Construct library to create Lambda layer with Powertools for Python library.

License: MIT No Attribution

Dockerfile 5.67% TypeScript 86.10% JavaScript 8.23%
aws cdk lambda powertools pythons layer devax construct

cdk-lambda-powertools-python-layer's Introduction

Lambda powertools python layer

⚠️ This repository is archived: use the new repository to submit issues and follow updates: https://github.com/awslabs/cdk-aws-lambda-powertools-layer

Why this project exists

This is a custom construct that will create AWS Lambda Layer with AWS Powertools for Python library. There are different ways how to create a layer and when working with CDK you need to install the library, create a zip file and wire it correctly. With this construct you don't have to care about packaging and dependency management, just create a construct and add it to your function. The construct is an extension of the existing LayerVersion construct from the CDK library, so you have access to all fields and methods.

See the API for details.

import { LambdaPowertoolsLayer } from 'cdk-lambda-powertools-python-layer';

const powertoolsLayer = new LambdaPowertoolsLayer(this, 'TestLayer');

Python

from cdk_lambda_powertools_python_layer import LambdaPowertoolsLayer

powertoolsLayer = LambdaPowertoolsLayer(self, 'PowertoolsLayer')

The layer will be created during the CDK synth step and thus requires Docker.

Install

TypeSript/JavaScript:

npm i cdk-lambda-powertools-python-layer

Python:

pip install cdk-lambda-powertools-python-layer

Usage

Python

A single line will create a layer with powertools for python:

from cdk_lambda_powertools_python_layer import LambdaPowertoolsLayer

powertoolsLayer = LambdaPowertoolsLayer(self, 'PowertoolsLayer')

You can then add the layer to your funciton:

from aws_cdk import aws_lambda

aws_lambda.Function(self, 'LambdaFunction',
                            code=aws_lambda.Code.from_asset('function'),
                            handler='app.handler',
                            runtime=aws_lambda.Runtime.PYTHON_3_9,
                            layers=[powertoolsLayer])

You can specify the powertools version by passing the optional version paramter, otherwise the construct will take the latest version from pypi repository.

LambdaPowertoolsLayer(self, 'PowertoolsLayer', version='1.24.0')

Additionally, powertools have extras depenedncies such as Pydantic, documented here. This is not included by default, and you have to set this option in the construct definition if you need it:

LambdaPowertoolsLayer(self, 'PowertoolsLayer', include_extras=True)

Full example:

from aws_cdk import Stack, aws_lambda
from cdk_lambda_powertools_python_layer import LambdaPowertoolsLayer
from constructs import Construct


class LayerTestStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)
        
        powertoolsLayer = LambdaPowertoolsLayer(
            self, 'PowertoolsLayer', include_extras=True, version='1.24.0')

        aws_lambda.Function(self, 'LambdaFunction',
                            code=aws_lambda.Code.from_asset('function'),
                            handler='app.handler',
                            runtime=aws_lambda.Runtime.PYTHON_3_9,
                            layers=[powertoolsLayer])

TypeScript

Full example for TypeScript:

import { Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { LambdaPowertoolsLayer } from 'cdk-lambda-powertools-python-layer';
import { Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda';
import * as path from 'path';

export class CdkPowertoolsExampleStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const powertoolsLayer = new LambdaPowertoolsLayer(this, 'TestLayer', {
      version: '1.22.0',
      includeExtras: true
    });

    new Function(this, 'LambdaFunction', {
      code: Code.fromAsset(path.join('./function')),
      handler: 'app.handler',
      runtime: Runtime.PYTHON_3_9,
      layers: [powertoolsLayer],
    });
  }
}

cdk-lambda-powertools-python-layer's People

Contributors

am29d avatar amazon-auto avatar dependabot[bot] avatar github-actions[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

flochaz

cdk-lambda-powertools-python-layer's Issues

Feature: provide option to retain the previous layer version

The default update and deletion policy of the CDK layer construct is DELETE. This is a default behaviour of the CDK library. In some cases we want to keep the previous layer versions, for example if we want to provide a public layer version. In this case deleting the previous version will impact customers who are using previously published versions.

chore: add dependabot to update packages based on CDK release

The current construct version is only compatible with CDK v2 and the CDK version is fixed. The major release version will be 2 based on the convention for CDK construct users: 1.x.x for CDK v1 and 2.x.x for CDK v2. I would like to have a dependabot that triggers a PR each time a new CDK v2 version is released.

bug: directory structure inside layer zip is wrong

We currently use Code.fromDockerBuild to build the layer during synth step and package the code. As a result everything that is inside /asset folder during docker build step will be zipped. This mean the zip step inside the Dockerfile is unnecessary, because it will be zipped again by fromDockerBuild method:

RUN zip -qr /asset/layer.zip /python --exclude \*/tests/\* \*/doc/\* \*/__pycache__/\* \*.pyc

As a fix we can simply run pip install with a target /assets/python and the python directory will be zipped.

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.