-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathpowertools-typescript-layer.ts
63 lines (56 loc) · 1.98 KB
/
powertools-typescript-layer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import * as path from 'path';
import { aws_lambda as lambda } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { execSync } from 'child_process';
import { Md5 } from 'ts-md5';
export interface PowerToolsTypeScriptLayerProps {
/**
* The powertools package version from npm repository.
*/
readonly version?: string
/**
* the name of the layer, will be randomised by cdk if empty
*/
readonly layerVersionName?: string
}
export class PowerToolsTypeScriptLayer extends lambda.LayerVersion {
public constructor(scope: Construct, id: string, props?: PowerToolsTypeScriptLayerProps) {
const version = props?.version ?? 'latest';
console.log(`publishing layer ${props?.layerVersionName} version : ${version}`);
const commands = [
'mkdir nodejs',
'cd nodejs',
'npm init -y',
`npm install --save \
@aws-lambda-powertools/commons@${version} \
@aws-lambda-powertools/logger@${version} \
@aws-lambda-powertools/metrics@${version} \
@aws-lambda-powertools/tracer@${version}`,
'rm -rf node_modules/@types',
'rm package.json package-lock.json',
];
const commandJoined = commands.join(' && ');
super(scope, id, {
layerVersionName: props?.layerVersionName,
description: `Lambda Powertools for TypeScript version ${props?.version}`,
compatibleRuntimes: [ lambda.Runtime.NODEJS_12_X, lambda.Runtime.NODEJS_14_X, lambda.Runtime.NODEJS_16_X ],
code: lambda.Code.fromAsset(path.join(__dirname, '.'), {
assetHash: Md5.hashStr(commandJoined),
bundling: {
image: lambda.Runtime.NODEJS_12_X.bundlingImage,
local: {
tryBundle(outputDir: string) {
try {
execSync('npm --version && zip --version');
} catch {
return false;
}
execSync(commandJoined, { cwd: outputDir });
return true;
},
},
},
}),
});
}
}