-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
82 lines (74 loc) · 2.48 KB
/
Copy pathapp.ts
File metadata and controls
82 lines (74 loc) · 2.48 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
import { App, Duration, RemovalPolicy, Stack } from "aws-cdk-lib";
import {
AttributeType,
Billing,
TableEncryptionV2,
TableV2,
} from "aws-cdk-lib/aws-dynamodb";
import { Architecture, Runtime } from "aws-cdk-lib/aws-lambda";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { LogGroup, RetentionDays } from "aws-cdk-lib/aws-logs";
import { Schedule, ScheduleExpression } from "aws-cdk-lib/aws-scheduler";
import { LambdaInvoke } from "aws-cdk-lib/aws-scheduler-targets";
const app = new App();
const stack = new Stack(app, "CronJob");
const naiveHandler = new NodejsFunction(stack, "CronHandlerNaive", {
entry: "src/naive.ts",
timeout: Duration.minutes(2),
runtime: Runtime.NODEJS_24_X,
architecture: Architecture.ARM_64,
logGroup: new LogGroup(stack, "CronHandlerNaiveLogGroup", {
retention: RetentionDays.ONE_WEEK,
removalPolicy: RemovalPolicy.DESTROY,
}),
});
const table = new TableV2(stack, "Table", {
partitionKey: {
name: "l",
type: AttributeType.STRING,
},
billing: Billing.onDemand(),
encryption: TableEncryptionV2.awsManagedKey(),
timeToLiveAttribute: "ttl",
removalPolicy: RemovalPolicy.DESTROY,
});
const withLockHandler = new NodejsFunction(stack, "CronHandlerWithLock", {
entry: "src/withLock.ts",
timeout: Duration.minutes(2),
runtime: Runtime.NODEJS_24_X,
architecture: Architecture.ARM_64,
logGroup: new LogGroup(stack, "CronHandlerWithLockLogGroup", {
retention: RetentionDays.ONE_WEEK,
removalPolicy: RemovalPolicy.DESTROY,
}),
environment: {
TABLE_NAME: table.tableName,
},
});
table.grantReadWriteData(withLockHandler);
const smartHandler = new NodejsFunction(stack, "CronHandlerSmart", {
entry: "src/naive.ts",
timeout: Duration.minutes(2),
runtime: Runtime.NODEJS_24_X,
architecture: Architecture.ARM_64,
logGroup: new LogGroup(stack, "CronHandlerSmartLogGroup", {
retention: RetentionDays.ONE_WEEK,
removalPolicy: RemovalPolicy.DESTROY,
}),
reservedConcurrentExecutions: 1,
});
new Schedule(stack, "OneMinuteCronNaive", {
schedule: ScheduleExpression.rate(Duration.minutes(1)),
target: new LambdaInvoke(naiveHandler),
enabled: false,
});
new Schedule(stack, "OneMinuteCronWithLock", {
schedule: ScheduleExpression.rate(Duration.minutes(1)),
target: new LambdaInvoke(withLockHandler),
enabled: false,
});
new Schedule(stack, "OneMinuteCronSmart", {
schedule: ScheduleExpression.rate(Duration.minutes(1)),
target: new LambdaInvoke(smartHandler),
enabled: false,
});