-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathexample-function.ts
77 lines (69 loc) · 2.37 KB
/
example-function.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { custom_resources, aws_iam, Stack } from 'aws-cdk-lib';
import { Events } from '@aws-lambda-powertools/commons';
import { Construct } from 'constructs';
import { NodejsFunction, NodejsFunctionProps } from 'aws-cdk-lib/aws-lambda-nodejs';
import { Tracing, Runtime, LayerVersion } from 'aws-cdk-lib/aws-lambda';
interface ExampleFunctionProps {
readonly functionName: string
readonly tracingActive?: boolean
readonly useLayer?: boolean
readonly invocations?: number
readonly fnProps?: Partial<NodejsFunctionProps>
}
class ExampleFunction extends Construct {
public constructor(scope: Construct, id: string, props: ExampleFunctionProps) {
super(scope, id);
const { functionName, tracingActive, invocations, fnProps } = Object.assign(
{
tracingActive: false,
invocations: 2,
},
props
);
const fnOptions = {
tracing: tracingActive ? Tracing.ACTIVE : Tracing.DISABLED,
runtime: Runtime.NODEJS_16_X,
...fnProps,
};
if (props.useLayer) {
fnOptions.bundling = {
externalModules: [
'@aws-lambda-powertools/commons',
'@aws-lambda-powertools/logger',
'@aws-lambda-powertools/metrics',
'@aws-lambda-powertools/tracer',
],
};
fnOptions.layers = [
LayerVersion.fromLayerVersionArn(
this,
'AWSLambdaPowertoolsTypeScript',
`arn:aws:lambda:${Stack.of(this).region}:094274105915:layer:AWSLambdaPowertoolsTypeScript:1`
),
];
}
const fn = new NodejsFunction(this, functionName, fnOptions);
for (let i = 0; i < invocations; i++) {
new custom_resources.AwsCustomResource(this, `Invoke-${functionName}-${i}`, {
onUpdate: {
service: 'Lambda',
action: 'invoke',
physicalResourceId: custom_resources.PhysicalResourceId.of(`${functionName}-${i}`),
parameters: {
FunctionName: fn.functionName,
InvocationType: 'RequestResponse',
Payload: JSON.stringify(Events.Custom.CustomEvent),
},
},
policy: custom_resources.AwsCustomResourcePolicy.fromStatements([
new aws_iam.PolicyStatement({
effect: aws_iam.Effect.ALLOW,
resources: [fn.functionArn],
actions: ['lambda:InvokeFunction'],
}),
]),
});
}
}
}
export { ExampleFunction };