forked from storacha/w3infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarpark-stack.js
98 lines (87 loc) · 2.22 KB
/
carpark-stack.js
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
import {
Bucket,
Function,
Queue,
use
} from 'sst/constructs'
import * as sqs from 'aws-cdk-lib/aws-sqs'
import { BusStack } from './bus-stack.js'
import { CARPARK_EVENT_BRIDGE_SOURCE_EVENT } from '../carpark/event-bus/source.js'
import { getBucketConfig, setupSentry } from './config.js'
import { mustGetEnv } from '../lib/env.js'
/**
* @param {import('sst/constructs').StackContext} properties
*/
export function CarparkStack({ stack, app }) {
// Setup app monitoring with Sentry
setupSentry(app, stack)
// Get eventBus reference
const { eventBus } = use(BusStack)
const { EIPFS_INDEXER_SQS_ARN, EIPFS_INDEXER_SQS_URL } = getEnv()
const carparkBucket = new Bucket(stack, 'car-store', {
cors: true,
cdk: {
bucket: getBucketConfig('carpark', app.stage)
}
})
// Elastic IPFS event for indexing
const indexerTopicQueue = new Queue(stack, 'indexer-topic-queue', {
cdk: {
queue: sqs.Queue.fromQueueArn(
stack,
'indexer-topic',
EIPFS_INDEXER_SQS_ARN
),
},
})
// Event Bus targets
const eIpfsIndexTarget = {
function: {
environment: {
EIPFS_INDEXER_SQS_URL
},
permissions: [indexerTopicQueue],
handler: 'carpark/event-bus/eipfs-indexer.handler',
},
}
eventBus.addRules(stack, {
newCar: {
pattern: {
source: [CARPARK_EVENT_BRIDGE_SOURCE_EVENT],
},
targets: {
eIpfsIndexTarget
}
}
})
// Trigger carpark events when a CAR is put into the bucket.
const carparkPutEventConsumer = new Function(stack, 'carpark-consumer', {
environment: {
EVENT_BUS_ARN: eventBus.eventBusArn,
},
permissions: [eventBus],
handler: 'carpark/functions/carpark-bucket-event.carparkBucketConsumer',
})
carparkBucket.addNotifications(stack, {
newCarPut: {
function: carparkPutEventConsumer,
events: ['object_created'],
}
})
stack.addOutputs({
BucketName: carparkBucket.bucketName,
Region: stack.region
})
return {
carparkBucket
}
}
/**
* Get Env validating it is set.
*/
function getEnv() {
return {
EIPFS_INDEXER_SQS_ARN: mustGetEnv('EIPFS_INDEXER_SQS_ARN'),
EIPFS_INDEXER_SQS_URL: mustGetEnv('EIPFS_INDEXER_SQS_URL'),
}
}