-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstack.ts
61 lines (52 loc) · 1.91 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
import { Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import * as cdk from 'aws-cdk-lib';
import { join } from "path";
export class Part02DynamoDBStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
// Provision a new REST API Gateway
const api = new cdk.aws_apigateway.RestApi(this, 'myFirstApi', {
restApiName: 'Part02Service',
});
const database = new cdk.aws_dynamodb.Table(this, 'myFirstDatabase', {
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 createNote = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'createNote', {
entry: join(__dirname, 'createNote', 'handler.ts'),
handler: 'handler',
runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
bundling: {
externalModules: ['@aws-sdk'],
},
environment: {
TABLE_NAME: database.tableName,
},
});
database.grantWriteData(createNote);
const getNote = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'getNote', {
entry: join(__dirname, 'getNote', 'handler.ts'),
handler: 'handler',
runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
bundling: {
externalModules: ['@aws-sdk'],
},
environment: {
TABLE_NAME: database.tableName,
},
});
database.grantReadData(getNote);
const usersResource = api.root.addResource('users').addResource('{userId}');
const notesResource = usersResource.addResource('notes');
notesResource.addMethod('POST', new cdk.aws_apigateway.LambdaIntegration(createNote));
notesResource.addResource('{id}').addMethod('GET', new cdk.aws_apigateway.LambdaIntegration(getNote));
}
}