-
Notifications
You must be signed in to change notification settings - Fork 0
/
sst.config.ts
112 lines (105 loc) · 3.42 KB
/
sst.config.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
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
/// <reference path="./.sst/platform/config.d.ts" />
import { readFileSync } from 'fs';
export default $config({
app(input) {
return {
name: 'email',
removal: input?.stage === 'production' ? 'retain' : 'remove',
home: 'aws',
providers: {
aws: true,
gcp: true,
command: true,
},
};
},
async run() {
$transform(sst.aws.Function, (args, opts) => {
args.nodejs = {
esbuild: {
external: ['@aws-sdk/*'],
},
};
args.environment = {
...args.environment,
POWERTOOLS_SERVICE_NAME: 'whiskey.email',
};
});
const bucket = new sst.aws.Bucket('EmailBucket');
const eventBus = aws.cloudwatch.EventBus.get('EventBus', 'whiskey-event-bus');
const api = new sst.aws.ApiGatewayV2('EmailApi', {
transform: {
route: {
handler: {
link: [bucket],
},
},
},
});
const newMessageSchema = new aws.schemas.Schema('NewMessageSchema', {
registryName: 'com.mattwyskiel.whiskey.events',
name: 'whiskey.email.NewMessage',
description: 'A new email message received by *@mattwyskiel.com or [email protected]',
type: 'OpenApi3',
content: readFileSync('./events/schemas/NewMessage.json', 'utf8'),
});
api.route('POST /improvmx-webhook', 'backend/functions/improvmx-webhook.handler');
const addToImprovMXDomain = new sst.aws.Function('AddToImprovMXDomain', {
handler: 'backend/functions/add-to-improvmx-domain.handler',
});
new command.local.Command('ExecuteAdd', {
create: `aws lambda invoke --function-name "$FN" --payload '{"webhook": "$WEBHOOK_URL"}' --cli-binary-format raw-in-base64-out out.txt >/dev/null && cat out.txt | tr -d '"' && rm out.txt`,
environment: {
FN: addToImprovMXDomain.arn,
WEBHOOK_URL: api.url.apply(url => `${url}/improvmx-webhook`),
},
});
const gmailTopic = new gcp.pubsub.Topic('GmailTopic');
new gcp.pubsub.TopicIAMBinding('GmailTopicBinding', {
topic: gmailTopic.id,
role: 'roles/pubsub.publisher',
members: ['serviceAccount:[email protected]'],
});
api.route('POST /gmail-webhook', 'backend/functions/gmail-webhook.handler');
const gmailSubscription = new gcp.pubsub.Subscription('GmailWebhookSubscription', {
topic: gmailTopic.id,
pushConfig: {
pushEndpoint: api.url.apply(url => `${url}/gmail-webhook`),
},
});
new sst.aws.Cron(
'GmailWatchCron',
{
schedule: 'rate(1 day)',
job: {
handler: 'backend/functions/gmail-watch.handler',
environment: {
GMAIL_TOPIC: gmailTopic.id,
},
},
},
{ dependsOn: [gmailSubscription] }
);
const bucketSubscriber = bucket.subscribe(
{
handler: 'backend/functions/s3-notifications-handler.handler',
permissions: [
{
actions: ['events:PutEvents'],
resources: [eventBus.arn],
},
],
link: [bucket],
},
{ events: ['s3:ObjectCreated:*'] }
);
// Housekeeping
const reloadRaw = new sst.aws.Function('ReloadRawEmails', {
handler: 'backend/functions/housekeeping/reload-raw.handler',
link: [bucket, bucketSubscriber.nodes.function],
environment: {
SUBSCRIBER_FUNCTION_ARN: bucketSubscriber.nodes.function.arn,
},
});
},
});