-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleStack.ts
More file actions
67 lines (59 loc) · 2.24 KB
/
ExampleStack.ts
File metadata and controls
67 lines (59 loc) · 2.24 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
// Import main CDK library as cdk
import { RemovalPolicy, Stack, StackProps } from 'aws-cdk-lib';
import { IManagedPolicy, ManagedPolicy } from 'aws-cdk-lib/aws-iam';
import { LayerVersion } from 'aws-cdk-lib/aws-lambda';
import { Construct } from 'constructs';
import { readdirSync } from 'fs';
import { NodetsLayer, NodetsFunction, NodetsFunctionProps } from '@aws-abaschen/cdk-typescript';
interface ExampleStackProps extends StackProps {
readonly app: string
readonly defaultRemovalPolicy?: RemovalPolicy
}
export class ExampleStack extends Stack {
layers: { [key: string]: LayerVersion }
app: string
lambdaManagedPolicy?: IManagedPolicy
constructor(scope: Construct, id: string, props: ExampleStackProps) {
super(scope, id, props);
this.app = props.app;
// retrieve the minimal managed policy for the lambda functions to provide VPC Access, remove this line if unwanted
this.lambdaManagedPolicy = ManagedPolicy.fromManagedPolicyArn(this, 'AWSLambdaVPCAccessExecutionRole', 'arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole');
//build layers
this.buildLayers();
/**
* Example of lambda creation
*/
const lambdaDefault: Partial<NodetsFunctionProps> = {
// vpc,
// vpcSubnets: { subnetType: SubnetType.PRIVATE_WITH_EGRESS },
// securityGroups: [lambdaSecurityGroup],
};
const name = 'discord-pong';
new NodetsFunction(this, name, {
...lambdaDefault,
description: 'A lambda example with a layer to answer to discord bot interaction with pong',
layers: [this.layers['discord-authorizer']],
parameters: ['DB-USER'],
environment: {
TEST_1: 'test Value 1'
}
});
new NodetsFunction(this, 'return-200', {
...lambdaDefault,
description: 'always return 200',
});
}
buildLayers() {
this.layers = readdirSync('src/layers', { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name)
.reduce<{ [key: string]: LayerVersion }>((map, name) => {
/*Create Layer for each directory*/
const layer = new NodetsLayer(this, `${name}-layer`, { name });
return {
...map,
[name]: layer
};
}, {});
}
}