-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstack.ts
98 lines (85 loc) · 3.5 KB
/
stack.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { join } from 'path';
// Use the Node18 runtime which provides the aws-sdk v3 natively
// This way our Lambda functions bundles will be smaller
const sharedLambdaConfig = {
runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
bundling: {
externalModules: ['@aws-sdk'],
},
};
export class Part14MasterDynamoDBStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const api = new cdk.aws_apigateway.RestApi(this, 'Part14Service');
const table = new cdk.aws_dynamodb.Table(this, 'DdbTable', {
partitionKey: { name: 'PK', type: cdk.aws_dynamodb.AttributeType.STRING },
sortKey: { name: 'SK', type: cdk.aws_dynamodb.AttributeType.STRING },
billingMode: cdk.aws_dynamodb.BillingMode.PAY_PER_REQUEST,
});
const classicRoute = api.root.addResource('classic');
const classicCreateUser = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'ClassicCreateUser', {
entry: join(__dirname, 'classic.ts'),
handler: 'createUser',
environment: {
TABLE_NAME: table.tableName,
},
...sharedLambdaConfig,
});
table.grantWriteData(classicCreateUser);
classicRoute.addMethod('POST', new cdk.aws_apigateway.LambdaIntegration(classicCreateUser));
const classicListUsers = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'ClassicListUsers', {
entry: join(__dirname, 'classic.ts'),
handler: 'listUsers',
environment: {
TABLE_NAME: table.tableName,
},
...sharedLambdaConfig,
});
table.grantReadData(classicListUsers);
classicRoute.addMethod('GET', new cdk.aws_apigateway.LambdaIntegration(classicListUsers));
const documentRoute = api.root.addResource('document');
const documentCreateUser = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'DocumentCreateUser', {
entry: join(__dirname, 'document.ts'),
handler: 'createUser',
environment: {
TABLE_NAME: table.tableName,
},
...sharedLambdaConfig,
});
table.grantWriteData(documentCreateUser);
documentRoute.addMethod('POST', new cdk.aws_apigateway.LambdaIntegration(documentCreateUser));
const documentListUsers = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'DocumentListUsers', {
entry: join(__dirname, 'document.ts'),
handler: 'listUsers',
environment: {
TABLE_NAME: table.tableName,
},
...sharedLambdaConfig,
});
table.grantReadData(documentListUsers);
documentRoute.addMethod('GET', new cdk.aws_apigateway.LambdaIntegration(documentListUsers));
const toolboxRoute = api.root.addResource('toolbox');
const toolboxCreateUser = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'ToolboxCreateUser', {
entry: join(__dirname, 'toolbox.ts'),
handler: 'createUser',
environment: {
TABLE_NAME: table.tableName,
},
...sharedLambdaConfig,
});
table.grantWriteData(toolboxCreateUser);
toolboxRoute.addMethod('POST', new cdk.aws_apigateway.LambdaIntegration(toolboxCreateUser));
const toolboxListUsers = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'ToolboxListUsers', {
entry: join(__dirname, 'toolbox.ts'),
handler: 'listUsers',
environment: {
TABLE_NAME: table.tableName,
},
...sharedLambdaConfig,
});
table.grantReadData(toolboxListUsers);
toolboxRoute.addMethod('GET', new cdk.aws_apigateway.LambdaIntegration(toolboxListUsers));
}
}