forked from aws-samples/aws-sdk-js-notes-app
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathaws-sdk-js-notes-app-stack.ts
79 lines (74 loc) · 1.92 KB
/
aws-sdk-js-notes-app-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
import {
Stack,
StackProps,
CfnOutput,
aws_apigateway as apigw,
aws_cognito as cognito,
aws_dynamodb as dynamodb,
aws_iam as iam,
aws_s3 as s3,
} from "aws-cdk-lib";
import { Construct } from "constructs";
import { NotesApi } from "./notes-api";
export class AwsSdkJsNotesAppStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const table = new dynamodb.Table(this, "notes", {
partitionKey: { name: "noteId", type: dynamodb.AttributeType.STRING },
});
const api = new apigw.RestApi(this, "endpoint");
const notes = api.root.addResource("notes");
notes.addMethod(
"GET",
new apigw.LambdaIntegration(
new NotesApi(this, "listNotes", {
table,
grantActions: ["dynamodb:Scan"],
}).handler
)
);
notes.addMethod(
"POST",
new apigw.LambdaIntegration(
new NotesApi(this, "createNote", {
table,
grantActions: ["dynamodb:PutItem"],
}).handler
)
);
const note = notes.addResource("{id}", {
defaultCorsPreflightOptions: {
allowOrigins: apigw.Cors.ALL_ORIGINS,
},
});
note.addMethod(
"GET",
new apigw.LambdaIntegration(
new NotesApi(this, "getNote", {
table,
grantActions: ["dynamodb:GetItem"],
}).handler
)
);
note.addMethod(
"PUT",
new apigw.LambdaIntegration(
new NotesApi(this, "updateNote", {
table,
grantActions: ["dynamodb:UpdateItem"],
}).handler
)
);
note.addMethod(
"DELETE",
new apigw.LambdaIntegration(
new NotesApi(this, "deleteNote", {
table,
grantActions: ["dynamodb:DeleteItem"],
}).handler
)
);
new CfnOutput(this, "GatewayUrl", { value: api.url });
new CfnOutput(this, "Region", { value: this.region });
}
}