forked from aws-samples/aws-sdk-js-notes-app
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathaws-sdk-js-notes-app-stack.ts
More file actions
168 lines (154 loc) · 4.65 KB
/
aws-sdk-js-notes-app-stack.ts
File metadata and controls
168 lines (154 loc) · 4.65 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import {
Stack,
StackProps,
CfnOutput,
aws_apigateway as apigw,
aws_cognito as cognito,
aws_dynamodb as dynamodb,
aws_iam as iam,
aws_s3 as s3,
aws_cloudfront as cloudfront,
aws_cloudfront_origins as origins,
} 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
)
);
const filesBucket = new s3.Bucket(this, "files-bucket");
filesBucket.addCorsRule({
allowedOrigins: apigw.Cors.ALL_ORIGINS, // NOT recommended for production code
allowedMethods: [
s3.HttpMethods.PUT,
s3.HttpMethods.GET,
s3.HttpMethods.DELETE,
],
allowedHeaders: ["*"],
});
const identityPool = new cognito.CfnIdentityPool(this, "identity-pool", {
allowUnauthenticatedIdentities: true,
});
const unauthenticated = new iam.Role(this, "unauthenticated-role", {
assumedBy: new iam.FederatedPrincipal(
"cognito-identity.amazonaws.com",
{
StringEquals: {
"cognito-identity.amazonaws.com:aud": identityPool.ref,
},
"ForAnyValue:StringLike": {
"cognito-identity.amazonaws.com:amr": "unauthenticated",
},
},
"sts:AssumeRoleWithWebIdentity"
),
});
// NOT recommended for production code - only give read permissions for unauthenticated resources
filesBucket.grantRead(unauthenticated);
filesBucket.grantPut(unauthenticated);
filesBucket.grantDelete(unauthenticated);
// Add policy to start Transcribe stream transcription
unauthenticated.addToPolicy(
new iam.PolicyStatement({
resources: ["*"],
actions: ["transcribe:StartStreamTranscriptionWebSocket"],
})
);
// Add policy to enable Amazon Polly text-to-speech
unauthenticated.addManagedPolicy(
iam.ManagedPolicy.fromAwsManagedPolicyName("AmazonPollyFullAccess")
);
new cognito.CfnIdentityPoolRoleAttachment(this, "role-attachment", {
identityPoolId: identityPool.ref,
roles: {
unauthenticated: unauthenticated.roleArn,
},
});
const websiteBucket = new s3.Bucket(this, "WebsiteBucket", {
bucketName: "notes-app-frontend",
});
const distribution = new cloudfront.Distribution(
this,
"WebsiteDistribution",
{
defaultRootObject: "index.html",
defaultBehavior: {
origin: origins.S3BucketOrigin.withOriginAccessControl(websiteBucket),
},
errorResponses: [
{
httpStatus: 403,
responseHttpStatus: 200,
responsePagePath: "/index.html",
},
{
httpStatus: 404,
responseHttpStatus: 200,
responsePagePath: "/index.html",
},
],
}
);
new CfnOutput(this, "FilesBucket", { value: filesBucket.bucketName });
new CfnOutput(this, "GatewayId", { value: api.restApiId });
new CfnOutput(this, "IdentityPoolId", { value: identityPool.ref });
new CfnOutput(this, "Region", { value: this.region });
new CfnOutput(this, "FrontendDistributionId", {
value: distribution.distributionId,
});
}
}