Skip to content

Commit b5b3998

Browse files
authored
fix: Use correct runtime versions for Lambda packagers (#35)
The default runtime version was always used in the Lambda packager. This caused the wrong versions of Python libraries to be installed.
1 parent 62c1526 commit b5b3998

File tree

7 files changed

+92
-6
lines changed

7 files changed

+92
-6
lines changed

src/base.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ export class BaseDependencyPackager extends Construct implements iam.IGrantable,
202202
} else if (this.type == DependencyPackagerType.LAMBDA) {
203203
const lambdaProps = {
204204
description: `Turbo layer packager for ${runtime}`,
205+
runtime: runtime,
205206
timeout: Duration.minutes(15),
206207
memorySize: 1024,
207208
ephemeralStorageSize: Size.gibibytes(10),
@@ -216,6 +217,11 @@ export class BaseDependencyPackager extends Construct implements iam.IGrantable,
216217
this.provider = new PackagePythonFunction(this, 'Packager', lambdaProps);
217218
} else if (runtime.family == lambda.RuntimeFamily.NODEJS) {
218219
this.provider = new PackageNodejsFunction(this, 'Packager', lambdaProps);
220+
// we can't set the runtime from here, so we have to manually override it.
221+
// projen puts `...props` before its own `runtime` setting and so its default `runtime` always wins.
222+
// https://github.com/projen/projen/blob/564341a55309e06939c86248bc76cabc590fd835/src/awscdk/lambda-function.ts#L253-L256
223+
const func = this.provider.node.defaultChild as lambda.CfnFunction;
224+
func.runtime = runtime.name;
219225
} else if (runtime.family == lambda.RuntimeFamily.RUBY) {
220226
this.provider = new PackageRubyFunction(this, 'Packager', lambdaProps);
221227
} else {

test/default.integ.snapshot/Turbo-Layer-Test.assets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,15 @@
235235
}
236236
}
237237
},
238-
"db7d3b44e017b63db50197704b29eb97bac168a71ac559a53b3bf68db6f123aa": {
238+
"fe6521518edb0de46de9d099e5e747350e8a6dab27f5cc38112716d0a7f6b9b0": {
239239
"source": {
240240
"path": "Turbo-Layer-Test.template.json",
241241
"packaging": "file"
242242
},
243243
"destinations": {
244244
"current_account-current_region": {
245245
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
246-
"objectKey": "db7d3b44e017b63db50197704b29eb97bac168a71ac559a53b3bf68db6f123aa.json",
246+
"objectKey": "fe6521518edb0de46de9d099e5e747350e8a6dab27f5cc38112716d0a7f6b9b0.json",
247247
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
248248
}
249249
}

test/default.integ.snapshot/Turbo-Layer-Test.template.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3866,7 +3866,7 @@
38663866
},
38673867
"Handler": "index.handler",
38683868
"MemorySize": 1024,
3869-
"Runtime": "nodejs14.x",
3869+
"Runtime": "nodejs16.x",
38703870
"Timeout": 900
38713871
},
38723872
"DependsOn": [

test/default.integ.snapshot/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"validateOnSynth": false,
1818
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
1919
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
20-
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/db7d3b44e017b63db50197704b29eb97bac168a71ac559a53b3bf68db6f123aa.json",
20+
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/fe6521518edb0de46de9d099e5e747350e8a6dab27f5cc38112716d0a7f6b9b0.json",
2121
"requiresBootstrapStackVersion": 6,
2222
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
2323
"additionalDependencies": [

test/default.integ.snapshot/tree.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5319,7 +5319,7 @@
53195319
},
53205320
"handler": "index.handler",
53215321
"memorySize": 1024,
5322-
"runtime": "nodejs14.x",
5322+
"runtime": "nodejs16.x",
53235323
"timeout": 900
53245324
}
53255325
},

test/nodejs.test.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { librariesToPackageJson } from '../src';
1+
import * as cdk from 'aws-cdk-lib';
2+
import { aws_lambda as lambda } from 'aws-cdk-lib';
3+
import { Match, Template } from 'aws-cdk-lib/assertions';
4+
import { librariesToPackageJson, NodejsDependencyPackager } from '../src';
25

36
test('Inline version parsing', () => {
47
const packageJson = librariesToPackageJson(['@aws-sdk/[email protected]', 'nothing', 'library@v595']);
@@ -10,3 +13,40 @@ test('Inline version parsing', () => {
1013
},
1114
});
1215
});
16+
17+
test('Packager runtime version matches', () => {
18+
const app = new cdk.App();
19+
const stack = new cdk.Stack(app, 'test');
20+
21+
new NodejsDependencyPackager(stack, 'packager 14', {
22+
runtime: lambda.Runtime.NODEJS_14_X,
23+
});
24+
new NodejsDependencyPackager(stack, 'packager 16', {
25+
runtime: lambda.Runtime.NODEJS_16_X,
26+
});
27+
new NodejsDependencyPackager(stack, 'packager 18', {
28+
runtime: lambda.Runtime.NODEJS_18_X,
29+
});
30+
31+
const template = Template.fromStack(stack);
32+
33+
template.hasResourceProperties(
34+
'AWS::Lambda::Function',
35+
Match.objectLike({
36+
Runtime: lambda.Runtime.NODEJS_14_X.name,
37+
}),
38+
);
39+
template.hasResourceProperties(
40+
'AWS::Lambda::Function',
41+
Match.objectLike({
42+
Runtime: lambda.Runtime.NODEJS_16_X.name,
43+
}),
44+
);
45+
template.hasResourceProperties(
46+
'AWS::Lambda::Function',
47+
Match.objectLike({
48+
Runtime: lambda.Runtime.NODEJS_18_X.name,
49+
}),
50+
);
51+
});
52+

test/python.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as cdk from 'aws-cdk-lib';
2+
import { aws_lambda as lambda } from 'aws-cdk-lib';
3+
import { Match, Template } from 'aws-cdk-lib/assertions';
4+
import { PythonDependencyPackager } from '../src';
5+
6+
test('Packager runtime version matches', () => {
7+
const app = new cdk.App();
8+
const stack = new cdk.Stack(app, 'test');
9+
10+
new PythonDependencyPackager(stack, 'packager 3.7', {
11+
runtime: lambda.Runtime.PYTHON_3_7,
12+
});
13+
new PythonDependencyPackager(stack, 'packager 3.8', {
14+
runtime: lambda.Runtime.PYTHON_3_8,
15+
});
16+
new PythonDependencyPackager(stack, 'packager 3.9', {
17+
runtime: lambda.Runtime.PYTHON_3_9,
18+
});
19+
20+
const template = Template.fromStack(stack);
21+
22+
template.hasResourceProperties(
23+
'AWS::Lambda::Function',
24+
Match.objectLike({
25+
Runtime: lambda.Runtime.PYTHON_3_7.name,
26+
}),
27+
);
28+
template.hasResourceProperties(
29+
'AWS::Lambda::Function',
30+
Match.objectLike({
31+
Runtime: lambda.Runtime.PYTHON_3_8.name,
32+
}),
33+
);
34+
template.hasResourceProperties(
35+
'AWS::Lambda::Function',
36+
Match.objectLike({
37+
Runtime: lambda.Runtime.PYTHON_3_9.name,
38+
}),
39+
);
40+
});

0 commit comments

Comments
 (0)