-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsst.config.ts
169 lines (158 loc) · 5.44 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { SSTConfig } from 'sst';
import { RemovalPolicy, Duration } from 'aws-cdk-lib';
import { ResponseHeadersPolicy } from 'aws-cdk-lib/aws-cloudfront';
import { Alarm } from 'aws-cdk-lib/aws-cloudwatch';
import { SnsAction } from 'aws-cdk-lib/aws-cloudwatch-actions';
import { RetentionDays } from 'aws-cdk-lib/aws-logs';
import { Topic } from 'aws-cdk-lib/aws-sns';
import { EmailSubscription } from 'aws-cdk-lib/aws-sns-subscriptions';
import { Config, Cron, NextjsSite, Table } from 'sst/constructs';
export default {
config(_input) {
return {
name: 'ec2-leaser',
region: _input.stage === 'prod' ? 'eu-central-1' : 'us-east-1'
};
},
stacks(app) {
app.setDefaultFunctionProps({
logRetention: 'one_year',
runtime: 'nodejs20.x'
});
app.stack(
function API({ stack }) {
// Config
const azureClientId = new Config.Secret(stack, 'AZURE_CLIENT_ID');
const azureClientSecret = new Config.Secret(
stack,
'AZURE_CLIENT_SECRET'
);
const azureTenantId = new Config.Secret(stack, 'AZURE_TENANT_ID');
const authSecret = new Config.Secret(stack, 'AUTH_SECRET');
// Create a table for the cost center list
const table = new Table(stack, `config`, {
fields: {
PK: 'string',
SK: 'string'
},
primaryIndex: { partitionKey: 'PK', sortKey: 'SK' },
cdk: {
table: {
removalPolicy: RemovalPolicy.DESTROY
}
}
});
// Create the Cron tasks to destroy old resources
const destroyEc2Cron = new Cron(stack, 'DestroyEc2', {
schedule: 'rate(20 minutes)',
job: 'src/cron/terminate-instances.handler'
});
// Cron permissions
destroyEc2Cron.attachPermissions([
'ec2:DescribeInstances',
'ec2:TerminateInstances'
]);
const domain =
stack.stage === 'prod'
? 'wiiisdom.com'
: `${stack.stage}.wiiisdom.com`;
const site = new NextjsSite(stack, 'Site', {
bind: [
// see https://github.com/sst/sst/issues/3270#issuecomment-2218550203
// if you add a bind here, it will required to undeploy/re-deploy the
// NextjsSite construct in prod
table,
azureClientId,
azureClientSecret,
azureTenantId,
authSecret
],
permissions: [
'ec2:DescribeLaunchTemplates',
'ec2:DescribeLaunchTemplateVersions',
'iam:CreateServiceLinkedRole',
'ec2:RunInstances',
'ec2:CreateTags',
'ec2:DescribeInstances',
'ec2:DescribeSnapshots',
'ec2:CreateSnapshot',
'ec2:DeleteSnapshot',
'ec2:CreateReplaceRootVolumeTask'
],
customDomain: {
domainName: app.name + '.' + domain,
hostedZone: domain
},
cdk: {
distribution: {
defaultBehavior: {
responseHeadersPolicy: ResponseHeadersPolicy.SECURITY_HEADERS
}
},
server: {
logRetention: RetentionDays.ONE_YEAR
}
}
});
// alarms
if (!app.local) {
const alarmTopic = new Topic(stack, `${stack.stackName}-AlarmTopic`);
alarmTopic.addSubscription(new EmailSubscription('[email protected]'));
if (site.cdk?.function) {
const alarm = new Alarm(stack, 'NextFunctionAlarm', {
metric: site.cdk?.function?.metricErrors(),
alarmName: `${stack.stackName} NextFunction`,
threshold: 1,
evaluationPeriods: 1
});
alarm.addAlarmAction(new SnsAction(alarmTopic));
}
const destroyEc2Alarm = new Alarm(stack, `FunctionAlarm-DestroyEC2`, {
metric: destroyEc2Cron.jobFunction.metricErrors({
period: Duration.minutes(15)
}),
alarmName: `${stack.stackName} Destroy EC2 Job`,
threshold: 1,
evaluationPeriods: 1
});
destroyEc2Alarm.addAlarmAction(new SnsAction(alarmTopic));
const tableReadCapacity = new Alarm(
stack,
`FunctionAlarm-DynamoRead`,
{
metric: table.cdk.table.metricConsumedReadCapacityUnits({
period: Duration.minutes(15)
}),
alarmName: `${stack.stackName} DynamoDB Consumed Read Capacity`,
threshold: 5,
evaluationPeriods: 1
}
);
tableReadCapacity.addAlarmAction(new SnsAction(alarmTopic));
const tableWriteCapacity = new Alarm(
stack,
`FunctionAlarm-DynamoWrite`,
{
metric: table.cdk.table.metricConsumedWriteCapacityUnits({
period: Duration.minutes(15)
}),
alarmName: `${stack.stackName} DynamoDB Consumed Write Capacity`,
threshold: 5,
evaluationPeriods: 1
}
);
tableWriteCapacity.addAlarmAction(new SnsAction(alarmTopic));
}
},
{
id: 'backend-stack',
tags: {
costcenter: 'eng:lab',
project: 'ec2-leaser',
owner: '[email protected]',
management: 'sst'
}
}
);
}
} satisfies SSTConfig;