-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
68 lines (58 loc) · 2.29 KB
/
index.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
import events = require("@aws-cdk/aws-events");
import targets = require("@aws-cdk/aws-events-targets");
import lambda = require("@aws-cdk/aws-lambda");
import * as dynamodb from "@aws-cdk/aws-dynamodb";
import cdk = require("@aws-cdk/core");
import * as sns from "@aws-cdk/aws-sns";
import * as subs from "@aws-cdk/aws-sns-subscriptions";
import iam = require("@aws-cdk/aws-iam");
import { Effect } from "@aws-cdk/aws-iam";
export class SaleNotifierStack extends cdk.Stack {
constructor(app: cdk.App, id: string) {
super(app, id);
const table = new dynamodb.Table(this, "SaleItem", {
partitionKey: { name: "id", type: dynamodb.AttributeType.STRING },
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
});
//Create detection lambda
const saleDetectionFn = new lambda.Function(this, "SaleDetectionHandler", {
code: lambda.Code.fromAsset("lambda"),
handler: "sale-detector.handler",
runtime: lambda.Runtime.NODEJS_10_X,
environment: {
TABLE_NAME: table.tableName,
},
});
table.grantFullAccess(saleDetectionFn);
//Create notification lambda
const saleNotifierFn = new lambda.Function(this, "SaleNotifierHandler", {
code: lambda.Code.fromAsset("lambda"),
handler: "sale-notifier.handler",
runtime: lambda.Runtime.NODEJS_10_X,
});
//Grant notifier lambda permission to send email
const iAmStatementNotifier = new iam.PolicyStatement({
effect: Effect.ALLOW,
});
iAmStatementNotifier.addActions("ses:SendEmail");
iAmStatementNotifier.addResources("*");
saleNotifierFn.addToRolePolicy(iAmStatementNotifier);
//Create pub/sub notification topic
const topic = new sns.Topic(this, "Sale", {
displayName: "New sale topic",
topicName: "saleTopic",
});
//Subscribe the notification lambda to the pub/sub rule
topic.addSubscription(new subs.LambdaSubscription(saleNotifierFn));
topic.grantPublish(saleDetectionFn);
//Create scheduled event
const rule = new events.Rule(this, "Rule", {
schedule: events.Schedule.rate(cdk.Duration.days(1)),
});
//Add the sale detection lambda to the scheduled event
rule.addTarget(new targets.LambdaFunction(saleDetectionFn));
}
}
const app = new cdk.App();
new SaleNotifierStack(app, "SaleNotifierApp");
app.synth();