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

Commit 998b162

Browse files
author
Alexander Melnyk
committed
feat: merge PR to generilse layers
1 parent 5f558fe commit 998b162

10 files changed

+1076
-693
lines changed

Diff for: .projen/deps.json

+9-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .projenrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const project = new awscdk.AwsCdkConstructLibrary({
44
authorUrl: 'https://aws.amazon.com',
55
authorOrganization: true,
66
keywords: ['aws', 'cdk', 'powertools', 'python', 'layer', 'lambda', 'devax'],
7-
cdkVersion: '2.2.0',
7+
cdkVersion: '2.24.1',
88
defaultReleaseBranch: 'main',
99
majorVersion: 2,
1010
name: 'cdk-lambda-powertools-python-layer',

Diff for: API.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,15 @@ new LambdaPowertoolsLayer(scope: Construct, id: string, props?: PowertoolsLayerP
5454
```typescript
5555
import { LambdaPowertoolsLayer } from 'cdk-lambda-powertools-python-layer'
5656

57-
LambdaPowertoolsLayer.constructBuildArgs(includeExtras?: boolean, version?: string)
57+
LambdaPowertoolsLayer.constructBuildArgs(runtimeFamily: RuntimeFamily, includeExtras?: boolean, version?: string)
5858
```
5959

60+
###### `runtimeFamily`<sup>Required</sup> <a name="cdk-lambda-powertools-python-layer.LambdaPowertoolsLayer.parameter.runtimeFamily" id="cdklambdapowertoolspythonlayerlambdapowertoolslayerparameterruntimefamily"></a>
61+
62+
- *Type:* [`aws-cdk-lib.aws_lambda.RuntimeFamily`](#aws-cdk-lib.aws_lambda.RuntimeFamily)
63+
64+
---
65+
6066
###### `includeExtras`<sup>Optional</sup> <a name="cdk-lambda-powertools-python-layer.LambdaPowertoolsLayer.parameter.includeExtras" id="cdklambdapowertoolspythonlayerlambdapowertoolslayerparameterincludeextras"></a>
6167

6268
- *Type:* `boolean`
@@ -91,6 +97,7 @@ const powertoolsLayerProps: PowertoolsLayerProps = { ... }
9197
| --- | --- | --- |
9298
| [`includeExtras`](#cdklambdapowertoolspythonlayerpowertoolslayerpropspropertyincludeextras) | `boolean` | A flag for the pydantic extras dependency, used for parsing. |
9399
| [`layerVersionName`](#cdklambdapowertoolspythonlayerpowertoolslayerpropspropertylayerversionname) | `string` | the name of the layer, will be randomised if empty. |
100+
| [`runtimeFamily`](#cdklambdapowertoolspythonlayerpowertoolslayerpropspropertyruntimefamily) | [`aws-cdk-lib.aws_lambda.RuntimeFamily`](#aws-cdk-lib.aws_lambda.RuntimeFamily) | the runtime of the layer. |
94101
| [`version`](#cdklambdapowertoolspythonlayerpowertoolslayerpropspropertyversion) | `string` | The powertools package version from pypi repository. |
95102

96103
---
@@ -121,6 +128,18 @@ the name of the layer, will be randomised if empty.
121128

122129
---
123130

131+
##### `runtimeFamily`<sup>Optional</sup> <a name="cdk-lambda-powertools-python-layer.PowertoolsLayerProps.property.runtimeFamily" id="cdklambdapowertoolspythonlayerpowertoolslayerpropspropertyruntimefamily"></a>
132+
133+
```typescript
134+
public readonly runtimeFamily: RuntimeFamily;
135+
```
136+
137+
- *Type:* [`aws-cdk-lib.aws_lambda.RuntimeFamily`](#aws-cdk-lib.aws_lambda.RuntimeFamily)
138+
139+
the runtime of the layer.
140+
141+
---
142+
124143
##### `version`<sup>Optional</sup> <a name="cdk-lambda-powertools-python-layer.PowertoolsLayerProps.property.version" id="cdklambdapowertoolspythonlayerpowertoolslayerpropspropertyversion"></a>
125144

126145
```typescript
File renamed without changes.

Diff for: layer/TypeScript/Dockerfile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM public.ecr.aws/lambda/nodejs:16
2+
3+
4+
5+
ARG PACKAGE_SUFFIX=''
6+
7+
USER root
8+
WORKDIR /tmp
9+
10+
RUN yum update -y && yum install -y zip unzip wget tar gzip
11+
12+
RUN npm install --prefix /asset/nodejs @aws-lambda-powertools/metrics$PACKAGE_SUFFIX @aws-lambda-powertools/tracer$PACKAGE_SUFFIX @aws-lambda-powertools/logger$PACKAGE_SUFFIX

Diff for: package.json

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/lambda-powertools-layer.ts

+68-16
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ export interface PowertoolsLayerProps {
2020
* the name of the layer, will be randomised if empty
2121
*/
2222
readonly layerVersionName?: string;
23+
24+
/**
25+
* the runtime of the layer
26+
*/
27+
readonly runtimeFamily?: lambda.RuntimeFamily;
2328
}
2429

2530
/**
2631
* Defines a new Lambda Layer with Powertools for python library.
2732
*/
2833
export class LambdaPowertoolsLayer extends lambda.LayerVersion {
29-
30-
3134
/**
3235
* creates build argument for the Dockerfile.
3336
* There are multiple combinations between version and extras package that results in different suffix for the installation.
@@ -36,35 +39,84 @@ export class LambdaPowertoolsLayer extends lambda.LayerVersion {
3639
* For example, if we set extras=true and version=1.22.0 we get '[pydantic]==1.22.0'.
3740
*
3841
*/
39-
static constructBuildArgs(includeExtras: boolean | undefined, version: string | undefined): string {
42+
static constructBuildArgs(
43+
runtimeFamily: lambda.RuntimeFamily,
44+
includeExtras: boolean | undefined,
45+
version: string | undefined,
46+
): string {
4047
let suffix = '';
41-
if (includeExtras) {
42-
suffix = '[pydantic]';
43-
}
44-
if (version) {
45-
suffix = `${suffix}==${version}`;
48+
switch (runtimeFamily) {
49+
case lambda.RuntimeFamily.PYTHON:
50+
if (includeExtras) {
51+
suffix = '[pydantic]';
52+
}
53+
if (version) {
54+
suffix = `${suffix}==${version}`;
55+
}
56+
break;
57+
case lambda.RuntimeFamily.NODEJS:
58+
if (version) {
59+
suffix = `@${version}`;
60+
}
61+
break;
62+
default:
63+
break;
4664
}
4765
return suffix;
4866
}
4967

50-
5168
constructor(scope: Construct, id: string, props?: PowertoolsLayerProps) {
69+
const runtimeFamily = props?.runtimeFamily ?? lambda.RuntimeFamily.PYTHON;
70+
const languageName = getLanguageNameFromRuntimeFamily(runtimeFamily);
71+
const dockerFilePath = path.join(__dirname, `../layer/${languageName}`);
72+
console.log(`path ${dockerFilePath}`);
5273
super(scope, id, {
53-
code: lambda.Code.fromDockerBuild(path.join(__dirname, '../layer'), {
74+
code: lambda.Code.fromDockerBuild(dockerFilePath, {
5475
buildArgs: {
55-
PACKAGE_SUFFIX: LambdaPowertoolsLayer.constructBuildArgs(props?.includeExtras, props?.version),
76+
PACKAGE_SUFFIX: LambdaPowertoolsLayer.constructBuildArgs(
77+
runtimeFamily,
78+
props?.includeExtras,
79+
props?.version,
80+
),
5681
},
5782
}),
5883
layerVersionName: props?.layerVersionName ? props?.layerVersionName : undefined,
5984
license: 'MIT-0',
60-
compatibleRuntimes: [
85+
compatibleRuntimes: getRuntimesFromRuntimeFamily(runtimeFamily),
86+
description: `Lambda Powertools for ${languageName}${
87+
props?.includeExtras ? ' with Pydantic' : ''
88+
} ${props?.version ? `version ${props?.version}` : 'latest version'}`.trim(),
89+
});
90+
}
91+
}
92+
93+
function getRuntimesFromRuntimeFamily(runtimeFamily: lambda.RuntimeFamily): lambda.Runtime[] | undefined {
94+
switch (runtimeFamily) {
95+
case lambda.RuntimeFamily.PYTHON:
96+
return [
6197
lambda.Runtime.PYTHON_3_6,
6298
lambda.Runtime.PYTHON_3_7,
6399
lambda.Runtime.PYTHON_3_8,
64100
lambda.Runtime.PYTHON_3_9,
65-
],
66-
description: `Lambda Powertools for Python${props?.includeExtras ? ' with Pydantic' : ''} ${props?.version ? `version ${props.version}` : 'latest version'}`.trim(),
67-
});
68-
};
101+
];
102+
case lambda.RuntimeFamily.NODEJS:
103+
return [
104+
lambda.Runtime.NODEJS_12_X,
105+
lambda.Runtime.NODEJS_14_X,
106+
lambda.Runtime.NODEJS_16_X,
107+
];
108+
default:
109+
return [];
110+
}
111+
}
69112

113+
function getLanguageNameFromRuntimeFamily(runtimeFamily: lambda.RuntimeFamily): string {
114+
switch (runtimeFamily) {
115+
case lambda.RuntimeFamily.PYTHON:
116+
return 'Python';
117+
case lambda.RuntimeFamily.NODEJS:
118+
return 'TypeScript';
119+
default:
120+
return 'Unknown';
121+
}
70122
}

Diff for: test/lambda-powertools-layer.test.ts renamed to test/lambda-powertools-python-layer.test.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Stack } from 'aws-cdk-lib';
22
import { Template } from 'aws-cdk-lib/assertions';
3-
import { LambdaPowertoolsLayer } from '../lib';
3+
import { RuntimeFamily } from 'aws-cdk-lib/aws-lambda';
4+
import { LambdaPowertoolsLayer } from '../src';
45

56

67
describe('with no configuration the construct', () => {
@@ -91,25 +92,25 @@ describe('with version configuration the construct', () => {
9192

9293
describe('construct build args for Dockerfile', () => {
9394
test('returns pydantic and version', () => {
94-
const args = LambdaPowertoolsLayer.constructBuildArgs(true, '1.21.0');
95+
const args = LambdaPowertoolsLayer.constructBuildArgs(RuntimeFamily.PYTHON, true, '1.21.0');
9596

9697
expect(args).toEqual('[pydantic]==1.21.0');
9798
});
9899

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

102103
expect(args).toEqual('[pydantic]');
103104
});
104105

105106
test('returns only version when no extras flag provided', () => {
106-
const args = LambdaPowertoolsLayer.constructBuildArgs(undefined, '1.11.0');
107+
const args = LambdaPowertoolsLayer.constructBuildArgs(RuntimeFamily.PYTHON, undefined, '1.11.0');
107108

108109
expect(args).toEqual('==1.11.0');
109110
});
110111

111112
test('returns empty when no version and extras provided', () => {
112-
const args = LambdaPowertoolsLayer.constructBuildArgs(undefined, undefined);
113+
const args = LambdaPowertoolsLayer.constructBuildArgs(RuntimeFamily.PYTHON, undefined, undefined);
113114

114115
expect(args).toEqual('');
115116
});

0 commit comments

Comments
 (0)