Skip to content
This repository was archived by the owner on Apr 5, 2025. It is now read-only.

Commit 3a1cf63

Browse files
committed
feat(python!): add support for v2 layer
Changes to v1 layer: * remove python 3.6 support * aggregated all extra dependencies in "extras" group * removed unecessary files from Lambda Layer to reduce size
1 parent 30f6dea commit 3a1cf63

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

layer/Python/Dockerfile

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
FROM public.ecr.aws/lambda/python:3.8
22

3-
4-
53
ARG PACKAGE_SUFFIX=''
64

75
USER root
86
WORKDIR /tmp
97

10-
11-
# PACKAGE_SUFFIX = '[pydantic]==1.23.0'
12-
# PACKAGE_SUFFIX = '[pydantic]'
8+
# PACKAGE_SUFFIX = '[extras]==1.23.0'
9+
# PACKAGE_SUFFIX = '[extras]'
1310
# PACKAGE_SUFFIX = '=='1.23.0'
1411
# PACKAGE_SUFFIX = ''
1512

16-
1713
RUN yum update -y && yum install -y zip unzip wget tar gzip
1814

19-
RUN pip install -t /asset/python aws-lambda-powertools$PACKAGE_SUFFIX
15+
RUN pip install -t /asset/python aws-lambda-powertools$PACKAGE_SUFFIX
16+
17+
# Removing nonessential files
18+
RUN cd /asset && \
19+
rm -rf python/boto* && \
20+
find python -name '*.so' -type f -exec strip "{}" \; && \
21+
find python -wholename "*/tests/*" -type f -delete && \
22+
find python -regex '^.*\(__pycache__\|\.py[co]\)$' -delete && \
23+
zip -r9 aws-lambda-powertools.zip ./python && \
24+
rm -rf ./python

src/lambda-powertools-layer.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ export interface PowertoolsLayerProps {
1010
* The powertools package version from pypi repository.
1111
*/
1212
readonly version?: string;
13+
1314
/**
14-
* A flag for the pydantic extras dependency, used for parsing.
15+
* A flag for the extras dependencies (pydantic, aws-xray-sdk, etc.)
1516
* This will increase the size of the layer significantly. If you don't use parsing, ignore it.
1617
*/
1718
readonly includeExtras?: boolean;
@@ -36,7 +37,7 @@ export class LambdaPowertoolsLayer extends lambda.LayerVersion {
3637
* There are multiple combinations between version and extras package that results in different suffix for the installation.
3738
* With and without version, with and without extras flag.
3839
* We construct one suffix here because it is easier to do in code than inside the Dockerfile with bash commands.
39-
* For example, if we set extras=true and version=1.22.0 we get '[pydantic]==1.22.0'.
40+
* For example, if we set extras=true and version=1.22.0 we get '[extras]==1.22.0'.
4041
*
4142
*/
4243
static constructBuildArgs(
@@ -48,7 +49,7 @@ export class LambdaPowertoolsLayer extends lambda.LayerVersion {
4849
switch (runtimeFamily) {
4950
case lambda.RuntimeFamily.PYTHON:
5051
if (includeExtras) {
51-
suffix = '[pydantic]';
52+
suffix = '[extras]';
5253
}
5354
if (version) {
5455
suffix = `${suffix}==${version}`;
@@ -84,7 +85,7 @@ export class LambdaPowertoolsLayer extends lambda.LayerVersion {
8485
license: 'MIT-0',
8586
compatibleRuntimes: getRuntimesFromRuntimeFamily(runtimeFamily),
8687
description: `Lambda Powertools for ${languageName}${
87-
props?.includeExtras ? ' with Pydantic' : ''
88+
props?.includeExtras ? ' with Extras' : ''
8889
} ${props?.version ? `version ${props?.version}` : 'latest version'}`.trim(),
8990
});
9091
}
@@ -94,7 +95,6 @@ function getRuntimesFromRuntimeFamily(runtimeFamily: lambda.RuntimeFamily): lamb
9495
switch (runtimeFamily) {
9596
case lambda.RuntimeFamily.PYTHON:
9697
return [
97-
lambda.Runtime.PYTHON_3_6,
9898
lambda.Runtime.PYTHON_3_7,
9999
lambda.Runtime.PYTHON_3_8,
100100
lambda.Runtime.PYTHON_3_9,

test/lambda-powertools-python-layer.test.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ describe('with no configuration the construct', () => {
2323
test('matches the python 3.x runtimes', () => {
2424
template.hasResourceProperties('AWS::Lambda::LayerVersion', {
2525
CompatibleRuntimes: [
26-
'python3.6',
2726
'python3.7',
2827
'python3.8',
2928
'python3.9',
@@ -73,34 +72,34 @@ describe('with version configuration the construct', () => {
7372
});
7473

7574
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::LayerVersion', {
76-
Description: 'Lambda Powertools for Python with Pydantic version 1.22.0',
75+
Description: 'Lambda Powertools for Python with Extras version 1.22.0',
7776
});
7877

7978
});
8079

81-
test('synthesizes with pyndatic and latest version', () => {
80+
test('synthesizes with extras and latest version', () => {
8281
const stack = new Stack();
8382
new LambdaPowertoolsLayer(stack, 'LayerExtrasNoVersion', {
8483
includeExtras: true,
8584
});
8685

8786
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::LayerVersion', {
88-
Description: 'Lambda Powertools for Python with Pydantic latest version',
87+
Description: 'Lambda Powertools for Python with Extras latest version',
8988
});
9089
});
9190
});
9291

9392
describe('construct build args for Dockerfile', () => {
94-
test('returns pydantic and version', () => {
93+
test('returns extras and version', () => {
9594
const args = LambdaPowertoolsLayer.constructBuildArgs(RuntimeFamily.PYTHON, true, '1.21.0');
9695

97-
expect(args).toEqual('[pydantic]==1.21.0');
96+
expect(args).toEqual('[extras]==1.21.0');
9897
});
9998

100-
test('returns only pydantic when no version provided', () => {
99+
test('returns only extras when no version provided', () => {
101100
const args = LambdaPowertoolsLayer.constructBuildArgs(RuntimeFamily.PYTHON, true, undefined);
102101

103-
expect(args).toEqual('[pydantic]');
102+
expect(args).toEqual('[extras]');
104103
});
105104

106105
test('returns only version when no extras flag provided', () => {

0 commit comments

Comments
 (0)