-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathapp-lambdas.ts
43 lines (36 loc) · 1.19 KB
/
app-lambdas.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { Construct } from "constructs";
import { Code, Function, FunctionProps } from "aws-cdk-lib/aws-lambda";
export interface AppFunctionConfig extends Omit<FunctionProps, "code"> {
name: string;
codeAsset(): Code;
}
export interface AppFunction extends AppFunctionConfig {
fn: Function;
}
export class AppLambdas extends Construct {
readonly functions: Record<string, AppFunction> = {};
constructor(scope: Construct, id: string, appFunctions: AppFunctionConfig[]) {
super(scope, id);
this.functions = appFunctions.reduce((fns, nextFn) => {
fns[nextFn.name] = {
...nextFn,
fn: new Function(this, nextFn.name, {
...nextFn,
code: nextFn.codeAsset(),
}),
};
return fns;
}, this.functions);
}
addPermission(
fnName: string,
...permissions: Parameters<Function["addPermission"]>
) {
this.functions[fnName].fn.addPermission(...permissions);
}
grantInvokeAll(...grantee: Parameters<Function["grantInvoke"]>) {
Object.values(this.functions).forEach((appFn) => appFn.fn.grantInvoke(...grantee));
}
}