Repository navigation
Expand file tree
/
Copy pathWebAnalytics.ts
More file actions
152 lines (139 loc) · 3.76 KB
/
Copy pathWebAnalytics.ts
File metadata and controls
152 lines (139 loc) · 3.76 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
import { Construct } from "constructs";
import { AttributeType, BillingMode, Table } from "aws-cdk-lib/aws-dynamodb";
import { Duration, RemovalPolicy } from "aws-cdk-lib";
import {
ApiKey,
JsonSchema,
LambdaIntegration,
RestApi,
UsagePlan,
} from "aws-cdk-lib/aws-apigateway";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { Runtime } from "aws-cdk-lib/aws-lambda";
import { analyticsEventInputSchema } from "./model/AnalyticsEventInput";
import { ICertificate } from "aws-cdk-lib/aws-certificatemanager";
interface Props {
publicApiKey: string;
certificate: ICertificate;
origin: string;
domainName: string;
}
export class WebAnalytics extends Construct {
public readonly api: RestApi;
constructor(
scope: Construct,
id: string,
{ certificate, origin, domainName, publicApiKey }: Props
) {
super(scope, id);
const timeToLiveAttribute = "ttl";
const dynamoDBPartitionKey = "scatter";
const dynamoDBSortKey = "timestamp";
const events = new Table(this, "Events", {
billingMode: BillingMode.PAY_PER_REQUEST,
partitionKey: {
name: dynamoDBPartitionKey,
type: AttributeType.NUMBER,
},
sortKey: {
name: dynamoDBSortKey,
type: AttributeType.STRING,
},
removalPolicy: RemovalPolicy.DESTROY,
timeToLiveAttribute,
});
const handlerEnvironment = {
DYNAMODB_TABLE: events.tableName,
DYNAMODB_TTL_ATTRIBUTE: timeToLiveAttribute,
DYNAMODB_PARTITION_KEY: dynamoDBPartitionKey,
DYNAMODB_SORT_KEY: dynamoDBSortKey,
MAX_SCATTER: "1",
};
const collectEventHandler = new NodejsFunction(
this,
"CollectEventHandler",
{
runtime: Runtime.NODEJS_24_X,
environment: handlerEnvironment,
}
);
const getEventsByMonthHandler = new NodejsFunction(
this,
"GetEventsByMonthHandler",
{
runtime: Runtime.NODEJS_24_X,
environment: handlerEnvironment,
timeout: Duration.minutes(1),
}
);
const api = new RestApi(this, "Api", {
domainName: {
domainName,
certificate,
},
defaultCorsPreflightOptions: {
allowOrigins: [
`https://${origin}`,
`http://localhost:3000`,
`http://localhost:6006`,
],
},
});
const analyticsEventModel = api.addModel("AnalyticsEvent", {
modelName: "AnalyticsEvent",
contentType: "application/json",
schema: analyticsEventInputSchema as unknown as JsonSchema,
});
const collectEventMethod = api.root.addMethod(
"POST",
new LambdaIntegration(collectEventHandler),
{
apiKeyRequired: true,
requestModels: {
"application/json": analyticsEventModel,
},
requestValidatorOptions: {
validateRequestBody: true,
},
}
);
const getEventsByMonthMethod = api.root.addMethod(
"GET",
new LambdaIntegration(getEventsByMonthHandler),
{
apiKeyRequired: true,
}
);
const plan = new UsagePlan(this, "ReadUsagePlan", {
throttle: {
rateLimit: 10,
burstLimit: 100,
},
});
plan.addApiStage({
stage: api.deploymentStage,
throttle: [
{
method: getEventsByMonthMethod,
throttle: {
rateLimit: 1,
burstLimit: 1,
},
},
{
method: collectEventMethod,
throttle: {
rateLimit: 10,
burstLimit: 100,
},
},
],
});
plan.addApiKey(
new ApiKey(this, "WebAnalyticsApiKey", { value: publicApiKey })
);
events.grantReadData(getEventsByMonthHandler);
events.grantWriteData(collectEventHandler);
this.api = api;
}
}