forked from mbonig/website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.ts
More file actions
83 lines (65 loc) · 2.42 KB
/
backend.ts
File metadata and controls
83 lines (65 loc) · 2.42 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
import { defineBackend } from '@aws-amplify/backend';
import { auth } from './auth/resource';
import { data } from './data/resource';
import { storage } from './storage/resource';
import { TableNotifications } from './constructs/table-notifications';
import * as sns from 'aws-cdk-lib/aws-sns';
import * as snsSubscriptions from 'aws-cdk-lib/aws-sns-subscriptions';
import * as iam from 'aws-cdk-lib/aws-iam';
import { ProcessLinks } from './extract/resource';
/**
* @see https://docs.amplify.aws/react/build-a-backend/ to add storage, functions, and more
*/
const backend = defineBackend({
auth,
data,
storage
});
const dataResources = backend.data.resources;
dataResources.cfnResources.cfnGraphqlApi.xrayEnabled = true;
// Object.values(dataResources.cfnResources.amplifyDynamoDbTables).forEach((table) => {
// table.pointInTimeRecoveryEnabled = true;
// });
// === Subscribers ===
const subscribers = backend.createStack('subscribers')
const topic = new sns.Topic(subscribers, 'DdbToSnsTopic', {
displayName: 'New Post on cdk.dev',
});
new TableNotifications(subscribers, 'TableNotifications', {
table: dataResources.tables['Post'],
topic,
message: 'A new post with the title "<$.dynamodb.NewImage.title.S>" has been added to https://cdk.dev - check it out now'
})
const policy = new iam.Policy(subscribers, 'SubscribersPolicy', {
document: new iam.PolicyDocument({
statements: [new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
resources: [topic.topicArn],
actions: ['sns:Subscribe'],
})]
}),
});
backend.auth.resources.authenticatedUserIamRole.attachInlinePolicy(policy)
backend.auth.resources.unauthenticatedUserIamRole.attachInlinePolicy(policy);
backend.addOutput({
custom: {
subscribersTopicArn: topic.topicArn,
}
})
// === Link Notifications ===
const links = backend.createStack('links')
const t = new sns.Topic(links, 'DdbToSnsTopic', {
displayName: 'New Link for cdk.dev',
});
t.addSubscription(new snsSubscriptions.EmailSubscription('sebastian@korfmann.net'));
new TableNotifications(links, 'TableNotifications', {
table: dataResources.tables['LinkSuggestion'],
topic: t,
message: 'A new link with the url "<$.dynamodb.NewImage.url.S>" has been added to https://cdk.dev - check it out now'
})
export default backend;
// === Scraper ===
const scraper = backend.createStack('scraper')
new ProcessLinks(scraper, 'ProcessLinks', {
table: dataResources.tables['LinkSuggestion'],
})