-
Notifications
You must be signed in to change notification settings - Fork 1
/
layer-functions.ts
61 lines (55 loc) · 2.52 KB
/
layer-functions.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
import { RemovalPolicy, StackProps, aws_logs as logs } from 'aws-cdk-lib';
import { LambdaIntegration, RestApi } from 'aws-cdk-lib/aws-apigateway';
import { Architecture, Code, Function, LayerVersion, Runtime } from 'aws-cdk-lib/aws-lambda';
import { AwsStack } from './aws-cdk-js-dev-guide-stack';
export class LayerFunctions {
constructor(stack: AwsStack, id: string, props?: StackProps, customOptions?: any) {
const layer = new LayerVersion(stack, 'sample-layer', {
// Code.fromAsset must reference the build folder
code: Code.fromAsset('./layers/build/sample-layer'),
compatibleArchitectures: [Architecture.ARM_64],
compatibleRuntimes: [Runtime.NODEJS_20_X, Runtime.PYTHON_3_10],
license: 'MIT',
description: 'A sample layer for the node, python and dynamodb test functions',
});
stack.layer = layer;
// layer test function: node
const layerFunctionNode = new Function(stack, 'layer-function-node', {
runtime: Runtime.NODEJS_20_X,
architecture: Architecture.ARM_64,
handler: 'index.handler',
code: Code.fromAsset('./handlers/layer'),
layers: [layer],
logGroup: new logs.LogGroup(stack, `layer-function-node-logs`, {
logGroupName: `${id}-layer-function-node`,
removalPolicy: RemovalPolicy.DESTROY,
retention: logs.RetentionDays.THREE_MONTHS,
}),
});
// layer test function: python
const layerFunctionPython = new Function(stack, 'layer-function-python', {
runtime: Runtime.PYTHON_3_10,
architecture: Architecture.ARM_64,
handler: 'main.handler',
code: Code.fromAsset('./handlers/python'),
layers: [layer],
logGroup: new logs.LogGroup(stack, `layer-function-python-logs`, {
logGroupName: `${id}-layer-function-python`,
removalPolicy: RemovalPolicy.DESTROY,
retention: logs.RetentionDays.THREE_MONTHS,
}),
});
// layer api
const layerApi = new RestApi(stack, 'layer-api', {
defaultCorsPreflightOptions: stack.cors.corsOptions
});
layerApi
.root
.addResource('node')
.addMethod('GET', new LambdaIntegration(layerFunctionNode));
layerApi
.root
.addResource('python')
.addMethod('GET', new LambdaIntegration(layerFunctionPython));
}
};