|
1 | | -## My Project |
| 1 | +# Lambda powertools python layer |
2 | 2 |
|
3 | | -TODO: Fill this README out! |
| 3 | +## Why this project exists |
| 4 | +This is a custom construct that will create AWS Lambda Layer with AWS Powertools for Python library. |
| 5 | +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. |
| 6 | +With this construct you don't have to care about packaging and dependency management, just create a construct and add it to your function. |
| 7 | +The construct is an extension of the existing [`LayerVersion`](https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-lambda.LayerVersion.html) construct from the CDK library, so you have access to all fields and methods. |
4 | 8 |
|
5 | | -Be sure to: |
| 9 | +```typescript |
| 10 | +import { LambdaPowertoolsLayer } from 'cdk-lambda-powertools-python-layer'; |
6 | 11 |
|
7 | | -* Change the title in this README |
8 | | -* Edit your repository description on GitHub |
| 12 | +const powertoolsLayer = new LambdaPowertoolsLayer(this, 'TestLayer'); |
| 13 | +``` |
9 | 14 |
|
10 | | -## Security |
| 15 | +## How to test |
11 | 16 |
|
12 | | -See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information. |
| 17 | +This module is not published yet, therefore you need to install few tools to test it. |
| 18 | +This section will be deleted after the construct is released to a public repository. |
13 | 19 |
|
14 | | -## License |
| 20 | +### Requirements |
15 | 21 |
|
16 | | -This library is licensed under the MIT-0 License. See the LICENSE file. |
| 22 | +* cdk v2 |
| 23 | +* docker |
| 24 | +* npm or yarn, whatever you prefer |
17 | 25 |
|
| 26 | +### Build construct |
| 27 | + |
| 28 | +After you have checked out the repo: |
| 29 | + |
| 30 | +```shell |
| 31 | +npm i |
| 32 | +npm run build |
| 33 | +``` |
| 34 | + |
| 35 | +This will create a tgz file in `dist/js` directory. You can copy this file to your test project then install this module |
| 36 | +with |
| 37 | + |
| 38 | +```shell |
| 39 | +npm i file:PATH_TO_PACKAGE/cdk-lambda-powertools-python-layer@0.0.0.jsii.tgz |
| 40 | +``` |
| 41 | + |
| 42 | +## Install |
| 43 | + |
| 44 | +TypeSript/JavaScript: |
| 45 | + |
| 46 | +```shell |
| 47 | +npm i cdk-lambda-powertools-python-layer |
| 48 | +``` |
| 49 | + |
| 50 | +Python: |
| 51 | + |
| 52 | +```shell |
| 53 | +pip install cdk-lambda-powertools-python-layer |
| 54 | +``` |
| 55 | + |
| 56 | +## Usage |
| 57 | + |
| 58 | +A single line will create a layer with powertools for python: |
| 59 | + |
| 60 | +```typescript |
| 61 | +import { LambdaPowertoolsLayer } from 'cdk-lambda-powertools-python-layer'; |
| 62 | + |
| 63 | +const powertoolsLayer = new LambdaPowertoolsLayer(this, 'TestLayer', { |
| 64 | + version: '1.22.0', |
| 65 | +}); |
| 66 | +``` |
| 67 | + |
| 68 | +You can then add the layer to your funciton: |
| 69 | + |
| 70 | +```typescript |
| 71 | +new Function(this, 'LambdaFunction', { |
| 72 | + code: Code.fromAsset(path.join('./function')), |
| 73 | + handler: 'app.handler', |
| 74 | + runtime: Runtime.PYTHON_3_9, |
| 75 | + layers: [powertoolsLayer], |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +You can specify the powertools version by passing the optional `version` paramter, otherwise the construct will take the latest |
| 80 | +version from pypi repository. |
| 81 | + |
| 82 | +```typescript |
| 83 | +new LambdaPowertoolsLayer(this, 'PowertoolsLayer', { |
| 84 | + version: '1.21.0' |
| 85 | +}); |
| 86 | +``` |
| 87 | + |
| 88 | +Additionally, powertools have extras depenedncies such as Pydantic, [documented here](https://awslabs.github.io/aws-lambda-powertools-python/latest/#lambda-layer). |
| 89 | +This is not included by default, and you have to set this option in the construct definition if you need it: |
| 90 | + |
| 91 | +```typescript |
| 92 | +new LambdaPowertoolsLayer(this, 'PowertoolsLayer', { |
| 93 | + includeExtras: true |
| 94 | +}); |
| 95 | +``` |
| 96 | + |
| 97 | +Full example: |
| 98 | + |
| 99 | +```typescript |
| 100 | +import { Stack, StackProps } from 'aws-cdk-lib'; |
| 101 | +import { Construct } from 'constructs'; |
| 102 | +import { LambdaPowertoolsLayer } from 'cdk-lambda-powertools-python-layer'; |
| 103 | +import { Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda'; |
| 104 | +import * as path from 'path'; |
| 105 | + |
| 106 | +export class CdkPowertoolsExampleStack extends Stack { |
| 107 | + constructor(scope: Construct, id: string, props?: StackProps) { |
| 108 | + super(scope, id, props); |
| 109 | + |
| 110 | + const powertoolsLayer = new LambdaPowertoolsLayer(this, 'TestLayer', { |
| 111 | + version: '1.22.0', |
| 112 | + includeExtras: true |
| 113 | + }); |
| 114 | + |
| 115 | + new Function(this, 'LambdaFunction', { |
| 116 | + code: Code.fromAsset(path.join('./function')), |
| 117 | + handler: 'app.handler', |
| 118 | + runtime: Runtime.PYTHON_3_9, |
| 119 | + layers: [powertoolsLayer], |
| 120 | + }); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +``` |
0 commit comments