-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsst.config.ts
118 lines (106 loc) · 3.76 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
113
114
115
116
117
118
import type { SSTConfig } from "sst";
import { SvelteKitSite, Config, Bucket, Table, Function } from "sst/constructs";
import * as cdk from "aws-cdk-lib";
import * as dynamodb from "aws-cdk-lib/aws-dynamodb";
import { DynamoDBClient, DescribeTableCommand } from "@aws-sdk/client-dynamodb";
async function getExistingUserTableArn(tableName: string, region: string) {
const dynamoDBClient = new DynamoDBClient({ region });
try {
const response = await dynamoDBClient.send(new DescribeTableCommand({ TableName: tableName }));
return response.Table?.TableArn;
} catch (error) {
if (error.name !== "ResourceNotFoundException") {
throw error;
}
return undefined; // If the table doesn't exist, return undefined
}
}
export default {
config(_input) {
return {
name: "app-geo-ca-v2",
region: "ca-central-1",
};
},
async stacks(app) {
const tableName = `${app.stage}-app-geo-ca-v2-users`;
const existingUserTableArn = await getExistingUserTableArn(tableName, app.region);
app.stack(function Site({ stack }) {
/*** User Table ***/
// Check if an existing user table exists. If it does, use it instead of
// creating a new one.
const USER_TABLE = existingUserTableArn
? dynamodb.Table.fromTableArn(stack, "users", existingUserTableArn)
: new Table(stack, "users", {
fields: { uuid: "string" },
primaryIndex: { partitionKey: "uuid" },
});
/*** Other Resources ***/
const OIDC_CLIENT_ID = new Config.Parameter(stack, "OIDC_CLIENT_ID", {
value: process.env.OIDC_CLIENT_ID,
});
const COGNITO_USERPOOL_ID = new Config.Parameter(stack, "COGNITO_USERPOOL_ID", {
value: process.env.COGNITO_USERPOOL_ID,
});
const OIDC_CLIENT_SECRET = new Config.Secret(stack, "OIDC_CLIENT_SECRET");
const OIDC_CUSTOM_DOMAIN = new Config.Parameter(stack, "OIDC_CUSTOM_DOMAIN", {
value: process.env.OIDC_CUSTOM_DOMAIN,
});
const GEOCORE_API_DOMAIN = new Config.Parameter(stack, "GEOCORE_API_DOMAIN", {
value: process.env.GEOCORE_API_DOMAIN,
});
const FEATURE_SIGN_IN = new Config.Parameter(stack, "FEATURE_SIGN_IN", {
value: "false",
});
const GEOCORE_BUCKET = new Bucket(stack, "geocore", {
cdk: {
bucket: {
autoDeleteObjects: true,
removalPolicy: cdk.RemovalPolicy.DESTROY,
},
},
});
let HNAP_BUCKET = new Bucket(stack, "hnap", {
cdk: {
bucket: {
autoDeleteObjects: true,
removalPolicy: cdk.RemovalPolicy.DESTROY,
},
},
});
const HNAP_BRIDGE = new Function(stack, "hnap-bridge", {
handler: "packages/hnap-bridge/index.handler",
timeout: 30,
permissions: ["s3"],
bind: [GEOCORE_BUCKET, HNAP_BUCKET],
});
HNAP_BUCKET.addNotifications(stack, {
myNotification1: {
function: HNAP_BRIDGE,
events: ["object_created"],
},
});
// Wrap the user table in SST’s Config.Parameter so that it can be referenced in SST functions
// This is necessary when an existing table is imported using the Table.fromTableArn method
const userTableConfig = new Config.Parameter(stack, "USER_TABLE_NAME", {
value: tableName,
});
const site = new SvelteKitSite(stack, "site", {
path: "packages/web-app/",
bind: [
OIDC_CLIENT_ID,
COGNITO_USERPOOL_ID,
OIDC_CLIENT_SECRET,
OIDC_CUSTOM_DOMAIN,
GEOCORE_API_DOMAIN,
GEOCORE_BUCKET,
userTableConfig,
FEATURE_SIGN_IN,
],
});
stack.addOutputs({
url: site.url,
});
});
},
} satisfies SSTConfig;