Repository navigation
Expand file tree
/
Copy pathbookstore-api-stack.ts
More file actions
128 lines (117 loc) · 4.55 KB
/
Copy pathbookstore-api-stack.ts
File metadata and controls
128 lines (117 loc) · 4.55 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import * as cdk from 'aws-cdk-lib';
import * as apigw from 'aws-cdk-lib/aws-apigateway';
import { Runtime } from "aws-cdk-lib/aws-lambda";
import * as lambda from "aws-cdk-lib/aws-lambda-nodejs";
import { Construct } from 'constructs';
import * as path from "path";
export class BookstoreApiStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const api = new apigw.RestApi(this, `BookstoreApiGateway`, {
restApiName: `bookstore-api`,
deployOptions: {
metricsEnabled: true,
loggingLevel: apigw.MethodLoggingLevel.INFO,
dataTraceEnabled: true,
},
cloudWatchRole: true,
defaultCorsPreflightOptions: {
allowOrigins: apigw.Cors.ALL_ORIGINS,
allowMethods: ["OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE"],
allowHeaders: apigw.Cors.DEFAULT_HEADERS,
},
});
const addBookFn = new lambda.NodejsFunction(this, `AddBookFunction`, {
entry: path.resolve(__dirname, '../src/handlers/add-book.ts'),
functionName: `ghast-api-add-book`,
handler: 'handler',
memorySize: 512,
environment: {
DATABASE_HOST: process.env.DATABASE_HOST,
DATABASE_USERNAME: process.env.DATABASE_USERNAME,
DATABASE_PASSWORD: process.env.DATABASE_PASSWORD,
},
runtime: Runtime.NODEJS_20_X,
timeout: cdk.Duration.seconds(15),
bundling: {
target: 'es2023',
}
});
const listBooksFn = new lambda.NodejsFunction(this, `ListBooksFunction`, {
entry: path.resolve(__dirname, '../src/handlers/list-books.ts'),
functionName: `bookstore-api-list-books`,
handler: 'handler',
memorySize: 512,
environment: {
DATABASE_HOST: process.env.DATABASE_HOST,
DATABASE_USERNAME: process.env.DATABASE_USERNAME,
DATABASE_PASSWORD: process.env.DATABASE_PASSWORD,
},
runtime: Runtime.NODEJS_20_X,
timeout: cdk.Duration.seconds(15),
bundling: {
target: 'es2023',
}
});
const getBookFn = new lambda.NodejsFunction(this, `GetBookFunction`, {
entry: path.resolve(__dirname, '../src/handlers/get-book.ts'),
functionName: `bookstore-api-get-book`,
handler: 'handler',
memorySize: 512,
environment: {
DATABASE_HOST: process.env.DATABASE_HOST,
DATABASE_USERNAME: process.env.DATABASE_USERNAME,
DATABASE_PASSWORD: process.env.DATABASE_PASSWORD,
},
runtime: Runtime.NODEJS_20_X,
timeout: cdk.Duration.seconds(15),
bundling: {
target: 'es2023',
}
});
const updateBookFn = new lambda.NodejsFunction(this, `UpdateBookFunction`, {
entry: path.resolve(__dirname, '../src/handlers/update-book.ts'),
functionName: `bookstore-api-update-book`,
handler: 'handler',
memorySize: 512,
environment: {
DATABASE_HOST: process.env.DATABASE_HOST,
DATABASE_USERNAME: process.env.DATABASE_USERNAME,
DATABASE_PASSWORD: process.env.DATABASE_PASSWORD,
},
runtime: Runtime.NODEJS_20_X,
timeout: cdk.Duration.seconds(15),
bundling: {
target: 'es2023',
}
});
const deleteBookFn = new lambda.NodejsFunction(this, `DeleteBookFunction`, {
entry: path.resolve(__dirname, '../src/handlers/delete-book.ts'),
functionName: `bookstore-api-delete-book`,
handler: 'handler',
memorySize: 512,
environment: {
DATABASE_HOST: process.env.DATABASE_HOST,
DATABASE_USERNAME: process.env.DATABASE_USERNAME,
DATABASE_PASSWORD: process.env.DATABASE_PASSWORD,
},
runtime: Runtime.NODEJS_20_X,
timeout: cdk.Duration.seconds(15),
bundling: {
target: 'es2023',
}
});
const addBookLambdaIntegration = new apigw.LambdaIntegration(addBookFn);
const booksResource = api.root.addResource("books");
booksResource.addMethod("POST", addBookLambdaIntegration);
const listBooksLambdaIntegration = new apigw.LambdaIntegration(listBooksFn);
booksResource.addMethod("GET", listBooksLambdaIntegration);
const getBookLambdaIntegration = new apigw.LambdaIntegration(getBookFn);
const updateBookLambdaIntegration = new apigw.LambdaIntegration(updateBookFn);
const deleteBookLambdaIntegration = new apigw.LambdaIntegration(deleteBookFn);
const bookResource = booksResource.addResource("{id}");
bookResource.addMethod("GET", getBookLambdaIntegration);
bookResource.addMethod("PATCH", updateBookLambdaIntegration);
bookResource.addMethod("DELETE", deleteBookLambdaIntegration);
}
}