-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlambda.ts
More file actions
93 lines (80 loc) · 2.01 KB
/
lambda.ts
File metadata and controls
93 lines (80 loc) · 2.01 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import * as aws from "@notation/aws.iac";
import * as std from "@notation/std.iac";
import crypto from "crypto";
import path from "path";
type LambdaConfig = {
id?: string;
handler: string;
code: {
type: "file" | "zip";
path: string;
};
};
export const lambda = (config: LambdaConfig) => {
const functionGroup = new aws.AwsResourceGroup("Lambda", { config });
const filePath = config.code.path;
let lambdaId = config.id;
if (!lambdaId) {
const filePathHash = crypto
.createHash("BLAKE2s256")
.update(filePath)
.digest("hex")
.slice(0, 8);
lambdaId = `${config.handler}-${filePathHash}`;
}
let zipFile: std.fs.ZipFileInstance | std.fs.FileInstance;
if (config.code.type === "file") {
zipFile = functionGroup.add(
new std.fs.Zip({
id: `${lambdaId}-zip`,
config: { sourceFilePath: filePath },
}),
);
} else {
zipFile = functionGroup.add(
new std.fs.File({
id: `${lambdaId}-zip`,
config: { filePath },
}),
);
}
const role = functionGroup.add(
new aws.lambda.LambdaIamRole({
id: `${lambdaId}-role`,
config: {
RoleName: `${lambdaId}-role`,
},
}),
);
functionGroup.add(
new aws.lambda.LambdaRolePolicyAttachment({
id: `${lambdaId}-policy`,
dependencies: { role },
}),
);
const fileName = path.parse(filePath).name;
const lambdaResource = functionGroup.add(
new aws.lambda.LambdaFunction({
id: lambdaId,
config: {
FunctionName: lambdaId,
Handler: `${fileName}.${config.handler}`,
Runtime: "nodejs18.x",
// todo: make this configurable and remove it as a default
ReservedConcurrentExecutions: 1,
},
dependencies: {
role,
zipFile,
},
}),
);
functionGroup.add(
new aws.lambda.LambdaLogGroup({
id: `${lambdaId}-log-group`,
config: { retentionInDays: 30 },
dependencies: { lambda: lambdaResource },
}),
);
return functionGroup;
};