Skip to content
This repository was archived by the owner on Sep 8, 2022. It is now read-only.

Commit 3b4d081

Browse files
author
Alexander Melnyk
committed
init
1 parent d799f14 commit 3b4d081

File tree

11 files changed

+6697
-9
lines changed

11 files changed

+6697
-9
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lib
2+
coverage

API.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# API Reference <a name="API Reference" id="api-reference"></a>
2+
3+
## Constructs <a name="Constructs" id="constructs"></a>
4+
5+
### LambdaPowertoolsLayer <a name="cdk-lambda-powertools-python-layer.LambdaPowertoolsLayer" id="cdklambdapowertoolspythonlayerlambdapowertoolslayer"></a>
6+
7+
Defines a new Lambda Layer with Powertools for python library.
8+
9+
#### Initializers <a name="cdk-lambda-powertools-python-layer.LambdaPowertoolsLayer.Initializer" id="cdklambdapowertoolspythonlayerlambdapowertoolslayerinitializer"></a>
10+
11+
```typescript
12+
import { LambdaPowertoolsLayer } from 'cdk-lambda-powertools-python-layer'
13+
14+
new LambdaPowertoolsLayer(scope: Construct, id: string, props?: PowertoolsLayerProps)
15+
```
16+
17+
| **Name** | **Type** | **Description** |
18+
| --- | --- | --- |
19+
| [`scope`](#cdklambdapowertoolspythonlayerlambdapowertoolslayerparameterscope)<span title="Required">*</span> | [`constructs.Construct`](#constructs.Construct) | *No description.* |
20+
| [`id`](#cdklambdapowertoolspythonlayerlambdapowertoolslayerparameterid)<span title="Required">*</span> | `string` | *No description.* |
21+
| [`props`](#cdklambdapowertoolspythonlayerlambdapowertoolslayerparameterprops) | [`cdk-lambda-powertools-python-layer.PowertoolsLayerProps`](#cdk-lambda-powertools-python-layer.PowertoolsLayerProps) | *No description.* |
22+
23+
---
24+
25+
##### `scope`<sup>Required</sup> <a name="cdk-lambda-powertools-python-layer.LambdaPowertoolsLayer.parameter.scope" id="cdklambdapowertoolspythonlayerlambdapowertoolslayerparameterscope"></a>
26+
27+
- *Type:* [`constructs.Construct`](#constructs.Construct)
28+
29+
---
30+
31+
##### `id`<sup>Required</sup> <a name="cdk-lambda-powertools-python-layer.LambdaPowertoolsLayer.parameter.id" id="cdklambdapowertoolspythonlayerlambdapowertoolslayerparameterid"></a>
32+
33+
- *Type:* `string`
34+
35+
---
36+
37+
##### `props`<sup>Optional</sup> <a name="cdk-lambda-powertools-python-layer.LambdaPowertoolsLayer.parameter.props" id="cdklambdapowertoolspythonlayerlambdapowertoolslayerparameterprops"></a>
38+
39+
- *Type:* [`cdk-lambda-powertools-python-layer.PowertoolsLayerProps`](#cdk-lambda-powertools-python-layer.PowertoolsLayerProps)
40+
41+
---
42+
43+
44+
#### Static Functions <a name="Static Functions" id="static-functions"></a>
45+
46+
| **Name** | **Description** |
47+
| --- | --- |
48+
| [`constructBuildArgs`](#cdklambdapowertoolspythonlayerlambdapowertoolslayerconstructbuildargs) | creates build argument for the Dockerfile. |
49+
50+
---
51+
52+
##### `constructBuildArgs` <a name="cdk-lambda-powertools-python-layer.LambdaPowertoolsLayer.constructBuildArgs" id="cdklambdapowertoolspythonlayerlambdapowertoolslayerconstructbuildargs"></a>
53+
54+
```typescript
55+
import { LambdaPowertoolsLayer } from 'cdk-lambda-powertools-python-layer'
56+
57+
LambdaPowertoolsLayer.constructBuildArgs(includeExtras?: boolean, version?: string)
58+
```
59+
60+
###### `includeExtras`<sup>Optional</sup> <a name="cdk-lambda-powertools-python-layer.LambdaPowertoolsLayer.parameter.includeExtras" id="cdklambdapowertoolspythonlayerlambdapowertoolslayerparameterincludeextras"></a>
61+
62+
- *Type:* `boolean`
63+
64+
---
65+
66+
###### `version`<sup>Optional</sup> <a name="cdk-lambda-powertools-python-layer.LambdaPowertoolsLayer.parameter.version" id="cdklambdapowertoolspythonlayerlambdapowertoolslayerparameterversion"></a>
67+
68+
- *Type:* `string`
69+
70+
---
71+
72+
73+
74+
## Structs <a name="Structs" id="structs"></a>
75+
76+
### PowertoolsLayerProps <a name="cdk-lambda-powertools-python-layer.PowertoolsLayerProps" id="cdklambdapowertoolspythonlayerpowertoolslayerprops"></a>
77+
78+
Properties necessary to create Powertools layer for python.
79+
80+
#### Initializer <a name="[object Object].Initializer" id="object-objectinitializer"></a>
81+
82+
```typescript
83+
import { PowertoolsLayerProps } from 'cdk-lambda-powertools-python-layer'
84+
85+
const powertoolsLayerProps: PowertoolsLayerProps = { ... }
86+
```
87+
88+
#### Properties <a name="Properties" id="properties"></a>
89+
90+
| **Name** | **Type** | **Description** |
91+
| --- | --- | --- |
92+
| [`includeExtras`](#cdklambdapowertoolspythonlayerpowertoolslayerpropspropertyincludeextras) | `boolean` | A flag to decide wether to include the extras package, used for parsing. |
93+
| [`version`](#cdklambdapowertoolspythonlayerpowertoolslayerpropspropertyversion) | `string` | The powertools package version form pypi repository. |
94+
95+
---
96+
97+
##### `includeExtras`<sup>Optional</sup> <a name="cdk-lambda-powertools-python-layer.PowertoolsLayerProps.property.includeExtras" id="cdklambdapowertoolspythonlayerpowertoolslayerpropspropertyincludeextras"></a>
98+
99+
```typescript
100+
public readonly includeExtras: boolean;
101+
```
102+
103+
- *Type:* `boolean`
104+
105+
A flag to decide wether to include the extras package, used for parsing.
106+
107+
This will increase the size of the layer significantly. If you don't use parsing, ignore it.
108+
109+
---
110+
111+
##### `version`<sup>Optional</sup> <a name="cdk-lambda-powertools-python-layer.PowertoolsLayerProps.property.version" id="cdklambdapowertoolspythonlayerpowertoolslayerpropspropertyversion"></a>
112+
113+
```typescript
114+
public readonly version: string;
115+
```
116+
117+
- *Type:* `string`
118+
119+
The powertools package version form pypi repository.
120+
121+
---
122+
123+
124+

README.md

Lines changed: 116 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,124 @@
1-
## My Project
1+
# Lambda powertools python layer
22

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.
48

5-
Be sure to:
9+
```typescript
10+
import { LambdaPowertoolsLayer } from 'cdk-lambda-powertools-python-layer';
611

7-
* Change the title in this README
8-
* Edit your repository description on GitHub
12+
const powertoolsLayer = new LambdaPowertoolsLayer(this, 'TestLayer');
13+
```
914

10-
## Security
15+
## How to test
1116

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.
1319

14-
## License
20+
### Requirements
1521

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
1725

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+
```

layer/Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM public.ecr.aws/lambda/python:3.8
2+
3+
4+
5+
ARG PACKAGE_SUFFIX=''
6+
7+
USER root
8+
WORKDIR /tmp
9+
10+
11+
# PACKAGE_SUFFIX = '[pydantic]==1.23.0'
12+
# PACKAGE_SUFFIX = '[pydantic]'
13+
# PACKAGE_SUFFIX = '=='1.23.0'
14+
# PACKAGE_SUFFIX = ''
15+
16+
17+
RUN yum update -y && yum install -y zip unzip wget tar gzip
18+
19+
RUN pip install -t /python aws-lambda-powertools$PACKAGE_SUFFIX
20+
21+
22+
RUN mkdir -p /asset
23+
RUN zip -qr /asset/layer.zip /python --exclude \*/tests/\* \*/doc/\* \*/__pycache__/\* \*.pyc

0 commit comments

Comments
 (0)