-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
444 lines (399 loc) · 11.7 KB
/
index.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";
// The following vars are not allowed to be undefined, hence the `${...}` magic
let route53Zone = `${process.env["ROUTE_53_ZONE"]}`;
let domain = `scorer.${process.env["DOMAIN"]}`;
let SCORER_SERVER_SSM_ARN = `${process.env["SCORER_SERVER_SSM_ARN"]}`;
let dbUsername = `${process.env["DB_USER"]}`;
let dbPassword = pulumi.secret(`${process.env["DB_PASSWORD"]}`);
let dbName = `${process.env["DB_NAME"]}`;
export const dockerGtcPassportScorerImage = `${process.env["DOCKER_GTC_PASSPORT_SCORER_IMAGE"]}`;
//////////////////////////////////////////////////////////////
// Set up VPC
//////////////////////////////////////////////////////////////
const vpc = new awsx.ec2.Vpc("scorer", {
subnets: [{ type: "public" }, { type: "private", mapPublicIpOnLaunch: true }],
});
export const vpcID = vpc.id;
export const vpcPrivateSubnetIds = vpc.privateSubnetIds;
export const vpcPublicSubnetIds = vpc.publicSubnetIds;
export const vpcPrivateSubnetId1 = vpcPrivateSubnetIds.then(
(values) => values[0]
);
export const vpcPublicSubnetId1 = vpcPublicSubnetIds.then(
(values) => values[0]
);
export const vpcPrivateSubnetId2 = vpcPrivateSubnetIds.then(
(values) => values[1]
);
export const vpcPublicSubnetId2 = vpcPublicSubnetIds.then(
(values) => values[1]
);
export const vpcPublicSubnet1 = vpcPublicSubnetIds.then((subnets) => {
return subnets[0];
});
//////////////////////////////////////////////////////////////
// Set up RDS instance
//////////////////////////////////////////////////////////////
let dbSubnetGroup = new aws.rds.SubnetGroup(`scorer-db-subnet`, {
subnetIds: vpcPrivateSubnetIds,
});
const db_secgrp = new aws.ec2.SecurityGroup(`scorer-db-secgrp`, {
description: "Security Group for DB",
vpcId: vpc.id,
ingress: [
{
protocol: "tcp",
fromPort: 5432,
toPort: 5432,
cidrBlocks: ["0.0.0.0/0"],
},
],
egress: [
{
protocol: "-1",
fromPort: 0,
toPort: 0,
cidrBlocks: ["0.0.0.0/0"],
},
],
});
// TODO: enable delete protection for the DB
const postgresql = new aws.rds.Instance(`scorer-db`, {
allocatedStorage: 10,
engine: "postgres",
// engineVersion: "5.7",
instanceClass: "db.t3.micro",
name: dbName,
password: dbPassword,
username: dbUsername,
skipFinalSnapshot: true,
dbSubnetGroupName: dbSubnetGroup.id,
vpcSecurityGroupIds: [db_secgrp.id],
});
export const rdsEndpoint = postgresql.endpoint;
export const rdsArn = postgresql.arn;
export const rdsConnectionUrl = pulumi.interpolate`psql://${dbUsername}:${dbPassword}@${rdsEndpoint}/${dbName}`;
export const rdsId = postgresql.id;
//////////////////////////////////////////////////////////////
// Set up ALB and ECS cluster
//////////////////////////////////////////////////////////////
const cluster = new awsx.ecs.Cluster("scorer", { vpc });
// export const clusterInstance = cluster;
export const clusterId = cluster.id;
// Generate an SSL certificate
const certificate = new aws.acm.Certificate("cert", {
domainName: domain,
tags: {
Environment: "review",
},
validationMethod: "DNS",
});
const certificateValidationDomain = new aws.route53.Record(
`${domain}-validation`,
{
name: certificate.domainValidationOptions[0].resourceRecordName,
zoneId: route53Zone,
type: certificate.domainValidationOptions[0].resourceRecordType,
records: [certificate.domainValidationOptions[0].resourceRecordValue],
ttl: 600,
}
);
const certificateValidation = new aws.acm.CertificateValidation(
"certificateValidation",
{
certificateArn: certificate.arn,
validationRecordFqdns: [certificateValidationDomain.fqdn],
},
{ customTimeouts: { create: "30s", update: "30s" } }
);
// Creates an ALB associated with our custom VPC.
const alb = new awsx.lb.ApplicationLoadBalancer(`scorer-service`, { vpc });
// Listen to HTTP traffic on port 80 and redirect to 443
const httpListener = alb.createListener("web-listener", {
port: 80,
protocol: "HTTP",
defaultAction: {
type: "redirect",
redirect: {
protocol: "HTTPS",
port: "443",
statusCode: "HTTP_301",
},
},
});
// Target group with the port of the Docker image
const target = alb.createTargetGroup("scorer-target", {
vpc,
port: 80,
healthCheck: { path: "/health", unhealthyThreshold: 5 },
});
// Listen to traffic on port 443 & route it through the target group
const httpsListener = target.createListener("scorer-listener", {
port: 443,
certificateArn: certificateValidation.certificateArn,
});
// Create a DNS record for the load balancer
const www = new aws.route53.Record("scorer", {
zoneId: route53Zone,
name: domain,
type: "A",
aliases: [
{
name: httpsListener.endpoint.hostname,
zoneId: httpsListener.loadBalancer.loadBalancer.zoneId,
evaluateTargetHealth: true,
},
],
});
const dpoppEcsRole = new aws.iam.Role("dpoppEcsRole", {
assumeRolePolicy: JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Action: "sts:AssumeRole",
Effect: "Allow",
Sid: "",
Principal: {
Service: "ecs-tasks.amazonaws.com",
},
},
],
}),
inlinePolicies: [
{
name: "allow_exec",
policy: JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Action: [
"ssmmessages:CreateControlChannel",
"ssmmessages:CreateDataChannel",
"ssmmessages:OpenControlChannel",
"ssmmessages:OpenDataChannel",
],
Resource: "*",
},
],
}),
},
{
name: "allow_iam_secrets_access",
policy: JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Action: ["secretsmanager:GetSecretValue"],
Effect: "Allow",
Resource: SCORER_SERVER_SSM_ARN,
},
],
}),
},
],
managedPolicyArns: [
"arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy",
],
tags: {
dpopp: "",
},
});
const secrets = [
{
name: "SECRET_KEY",
valueFrom: `${SCORER_SERVER_SSM_ARN}:SECRET_KEY::`,
},
];
const environment = [
{
name: "DEBUG",
value: "on",
},
{
name: "DATABASE_URL",
value: rdsConnectionUrl,
},
{
name: "ALLOWED_HOSTS",
value: JSON.stringify([domain, "*"]),
},
{
name: "CSRF_TRUSTED_ORIGINS",
value: JSON.stringify([`https://${domain}`]),
},
];
//////////////////////////////////////////////////////////////
// Set up the Scorer ECS service
//////////////////////////////////////////////////////////////
const service = new awsx.ecs.FargateService("scorer", {
cluster,
desiredCount: 1,
subnets: vpc.privateSubnetIds,
taskDefinitionArgs: {
executionRole: dpoppEcsRole,
containers: {
scorer: {
image: dockerGtcPassportScorerImage,
memory: 1024,
portMappings: [httpsListener],
command: [
"gunicorn",
"-w",
"4",
"-k",
"uvicorn.workers.UvicornWorker",
"scorer.asgi:application",
"-b",
"0.0.0.0:80",
],
links: [],
secrets: secrets,
environment: environment,
linuxParameters: {
initProcessEnabled: true,
},
},
},
},
});
//////////////////////////////////////////////////////////////
// Set up task to run migrations
//////////////////////////////////////////////////////////////
const taskMigrate = new awsx.ecs.FargateTaskDefinition(`scorer-run-migrate`, {
executionRole: dpoppEcsRole,
containers: {
web: {
image: dockerGtcPassportScorerImage,
command: ["python", "manage.py", "migrate"],
memory: 4096,
cpu: 2000,
portMappings: [],
secrets: secrets.concat([
{
name: "DJANGO_SUPERUSER_USERNAME",
valueFrom: `${SCORER_SERVER_SSM_ARN}:DJANGO_SUPERUSER_USERNAME::`,
},
{
name: "DJANGO_SUPERUSER_EMAIL",
valueFrom: `${SCORER_SERVER_SSM_ARN}:DJANGO_SUPERUSER_EMAIL::`,
},
{
name: "DJANGO_SUPERUSER_PASSWORD",
valueFrom: `${SCORER_SERVER_SSM_ARN}:DJANGO_SUPERUSER_PASSWORD::`,
},
]),
environment: environment,
dependsOn: [],
links: [],
},
},
});
export const taskMigrateDefinition = taskMigrate.taskDefinition.id;
//////////////////////////////////////////////////////////////
// Set up task to create superuser
//////////////////////////////////////////////////////////////
const task = new awsx.ecs.FargateTaskDefinition(`scorer-run-createsuperuser`, {
executionRole: dpoppEcsRole,
containers: {
web: {
image: dockerGtcPassportScorerImage,
command: ["python", "manage.py", "createsuperuser", "--noinput"],
memory: 4096,
cpu: 2000,
portMappings: [],
secrets: secrets.concat([
{
name: "DJANGO_SUPERUSER_USERNAME",
valueFrom: `${SCORER_SERVER_SSM_ARN}:DJANGO_SUPERUSER_USERNAME::`,
},
{
name: "DJANGO_SUPERUSER_EMAIL",
valueFrom: `${SCORER_SERVER_SSM_ARN}:DJANGO_SUPERUSER_EMAIL::`,
},
{
name: "DJANGO_SUPERUSER_PASSWORD ",
valueFrom: `${SCORER_SERVER_SSM_ARN}:DJANGO_SUPERUSER_PASSWORD::`,
},
]),
environment: environment,
dependsOn: [],
links: [],
},
},
});
export const taskDefinition = task.taskDefinition.id;
const secgrp = new aws.ec2.SecurityGroup(`scorer-run-migrations-task`, {
description: "gitcoin-ecs-task",
vpcId: vpc.id,
ingress: [
{ protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: ["0.0.0.0/0"] },
{ protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] },
],
egress: [
{
protocol: "-1",
fromPort: 0,
toPort: 0,
cidrBlocks: ["0.0.0.0/0"],
},
],
});
export const securityGroupForTaskDefinition = secgrp.id;
//////////////////////////////////////////////////////////////
// Set up EC2 instance
// - it is intended to be used for troubleshooting
//////////////////////////////////////////////////////////////
const ubuntu = aws.ec2.getAmi({
mostRecent: true,
filters: [
{
name: "name",
values: ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"],
},
{
name: "virtualization-type",
values: ["hvm"],
},
],
owners: ["099720109477"],
});
// Script to install docker in ec2 instance
const ec2InitScript = `#!/bin/bash
# Installing docker in ubuntu
# Instructions taken from here: https://docs.docker.com/engine/install/ubuntu/
mkdir /var/log/gitcoin
echo $(date) "Starting installation of docker" >> /var/log/gitcoin/init.log
apt-get remove docker docker-engine docker.io containerd runc
apt-get update
apt-get install -y \
ca-certificates \
curl \
gnupg \
lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io
mkdir /var/log/gitcoin
echo $(date) "Finished installation of docker" >> /var/log/gitcoin/init.log
`
const web = new aws.ec2.Instance("Web", {
ami: ubuntu.then(ubuntu => ubuntu.id),
associatePublicIpAddress: true,
instanceType: "t3.medium",
subnetId: vpcPublicSubnetId1.then(),
vpcSecurityGroupIds: [secgrp.id],
rootBlockDevice: {
volumeSize: 50
},
tags: {
Name: "Troubleshooting instance",
},
userData: ec2InitScript,
});
export const ec2PublicIp = web.publicIp;