-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
469 lines (407 loc) · 15.2 KB
/
index.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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
const pulumi = require("@pulumi/pulumi");
const aws = require("@pulumi/aws");
const gcp = require("@pulumi/gcp");
const mysql = require("@pulumi/aws/rds");
const route53 = require("@pulumi/aws/route53");
const iam = require("@pulumi/aws/iam");
const sns = require("@pulumi/aws/sns");
const config = new pulumi.Config();
const vpcCidrBlock = config.require("vpcCidrBlock");
const destinationCidrBlock = config.require("destinationCidrBlock");
const amiId = config.require("amiId");
const currentRegion = aws.getRegion();
const engine = config.require("engine");
const instanceClass = config.require("instanceClass");
const dbName = config.require("dbName");
const identifier = config.require("identifier");
const username = config.require("username");
const dbPassword = config.requireSecret("dbPassword");
const instanceType = config.require("instanceType");
const keyName = config.require("keyName");
const domainName = config.require("domainName");
const hostedZoneId = config.require("hostedZoneId");
const applicationPort = config.require("applicationPort");
const mailgunApiKey = config.requireSecret("mailgunApiKey");
const mailgunDomain = config.require("mailgunDomain");
const gcpConfig = new pulumi.Config("gcp");
const gcpProjectId = gcpConfig.require("project");
const certificateArn = config.require("certificateArn");
const availableZonesPromise = aws.getAvailabilityZones({ region: currentRegion });
availableZonesPromise.then(availableZones => {
const numberOfAZs = availableZones.names.length;
const vpc = new aws.ec2.Vpc("myVpc", {
cidrBlock: vpcCidrBlock,
enableDnsSupport: true,
enableDnsHostnames: true,
tags: {
Name: "myVpc",
},
});
const loadBalancerSecurityGroup = new aws.ec2.SecurityGroup("loadBalancerSecurityGroup", {
vpcId: vpc.id,
description: "Security group for the load balancer",
ingress: [
{ protocol: "tcp", fromPort: 443, toPort: 443, cidrBlocks: ["0.0.0.0/0"] },
],
egress: [
{ protocol: "-1", fromPort: 0, toPort: 0, cidrBlocks: ["0.0.0.0/0"], ipv6CidrBlocks: ["::/0"], },
],
});
const appSecurityGroup = new aws.ec2.SecurityGroup("appSecurityGroup", {
vpcId: vpc.id,
description: "Security group for application servers",
ingress: [
{ protocol: "tcp", fromPort: applicationPort, toPort: applicationPort, securityGroups: [loadBalancerSecurityGroup.id] },
{ protocol: "tcp", fromPort: 80, toPort: 80, securityGroups: [loadBalancerSecurityGroup.id] },
],
egress: [
{ protocol: "-1", fromPort: 0, toPort: 0, cidrBlocks: ["0.0.0.0/0"], ipv6CidrBlocks: ["::/0"], },
],
tags: {
Name: "applicationSecurityGroup",
},
});
const igw = new aws.ec2.InternetGateway("myIgw", {
vpcId: vpc.id,
tags: {
Name: "myIgw",
},
});
const publicRouteTable = new aws.ec2.RouteTable("publicRouteTable", {
vpcId: vpc.id,
tags: {
Name: "publicRouteTable",
},
});
const publicRoute = new aws.ec2.Route("publicRoute", {
routeTableId: publicRouteTable.id,
destinationCidrBlock: destinationCidrBlock,
gatewayId: igw.id,
});
const privateRouteTable = new aws.ec2.RouteTable("privateRouteTable", {
vpcId: vpc.id,
tags: {
Name: "privateRouteTable",
},
});
const resources = {
vpcId: vpc.id,
internetGatewayId: igw.id,
publicSubnets: [],
privateSubnets: [],
routeTables: [publicRouteTable.id, privateRouteTable.id],
};
let thirdOctet = 1;
for (let i = 0; i < numberOfAZs; i++) {
const az = availableZones.names[i];
['public', 'private'].forEach(type => {
const subnetCidrBlock = `10.0.${thirdOctet}.0/24`;
const subnet = new aws.ec2.Subnet(`${type}Subnet${thirdOctet}`, {
vpcId: vpc.id,
cidrBlock: subnetCidrBlock,
availabilityZone: az,
mapPublicIpOnLaunch: type === "public",
tags: {
Name: `${type}Subnet${thirdOctet}`,
},
});
new aws.ec2.RouteTableAssociation(`${type}RouteTableAssociation${thirdOctet}`, {
subnetId: subnet.id,
routeTableId: type === "public" ? publicRouteTable.id : privateRouteTable.id,
});
if (type === "public") {
resources.publicSubnets.push(subnet.id);
} else {
resources.privateSubnets.push(subnet.id);
}
thirdOctet += 2;
});
}
const dbSecurityGroup = new aws.ec2.SecurityGroup("dbSecurityGroup", {
vpcId: vpc.id,
ingress: [
{ protocol: "tcp", fromPort: 3306, toPort: 3306, securityGroups: [appSecurityGroup.id] },
],
tags: {
Name: "databaseSecurityGroup",
},
});
const dbParameterGroup = new mysql.ParameterGroup("db-parameter-group", {
family: "mariadb10.6",
parameters: [{ name: "character_set_client", value: "utf8" }],
});
const dbSubnetGroup = new aws.rds.SubnetGroup("db-subnet-group", {
subnetIds: resources.privateSubnets,
});
const rdsInstance = new aws.rds.Instance("myRdsInstance", {
engine: engine,
instanceClass: instanceClass,
dbName: dbName,
identifier: identifier,
username: username,
password: dbPassword,
dbSubnetGroupName: dbSubnetGroup.name,
vpcSecurityGroupIds: [dbSecurityGroup.id],
skipFinalSnapshot: true,
parameterGroupName: dbParameterGroup.name,
allocatedStorage: 5,
multiAz: false,
publiclyAccessible: false,
});
const cloudWatchRole = new iam.Role("cloudWatchRole", {
assumeRolePolicy: JSON.stringify({
Version: "2012-10-17",
Statement: [{
Action: "sts:AssumeRole",
Effect: "Allow",
Principal: {
Service: "ec2.amazonaws.com",
},
}],
}),
path: "/",
});
const cloudWatchPolicyAttachment = new iam.RolePolicyAttachment("cloudWatchPolicyAttachment", {
role: cloudWatchRole.name,
policyArn: "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy",
});
const mySnsTopic = new sns.Topic("mySnsTopic", {
displayName: "My SNS Topic for Lambda Notifications",
});
const snsPublishPolicy = new aws.iam.Policy("snsPublishPolicy", {
description: "Policy to allow publishing to the SNS topic",
policy: {
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Action: "sns:Publish",
Resource: mySnsTopic.arn,
},
],
},
});
const snsPublishPolicyAttachment = new aws.iam.PolicyAttachment("snsPublishPolicyAttachment", {
policyArn: snsPublishPolicy.arn,
roles: [cloudWatchRole.name],
});
const cloudWatchInstanceProfile = new iam.InstanceProfile("cloudWatchInstanceProfile", {
role: cloudWatchRole.name,
});
const bucket = new gcp.storage.Bucket("myBucket", {
name: "csye6225-pavancloud-bucket",
location: "US",
forceDestroy: true,
});
const serviceAccount = new gcp.serviceaccount.Account("myServiceAccount", {
accountId: "my-service-account",
displayName: "My Service Account",
});
const iamBinding = new gcp.projects.IAMBinding("serviceAccountStorageAdmin", {
project: gcpProjectId,
members: [pulumi.interpolate`serviceAccount:${serviceAccount.email}`],
role: "roles/storage.objectAdmin",
});
const serviceAccountKeys = new gcp.serviceaccount.Key("myServiceAccountKeys", {
serviceAccountId: serviceAccount.email,
});
const dynamoTable = new aws.dynamodb.Table("myDynamoTable", {
attributes: [
{ name: "email", type: "S" },
],
hashKey: "email",
billingMode: "PAY_PER_REQUEST",
});
const lambdaRole = new aws.iam.Role("lambdaRole", {
assumeRolePolicy: JSON.stringify({
Version: "2012-10-17",
Statement: [{
Action: "sts:AssumeRole",
Effect: "Allow",
Principal: {
Service: "lambda.amazonaws.com",
},
}],
}),
});
const lambdaPolicy = new aws.iam.Policy("lambdaPolicy", {
policy: JSON.stringify({
Version: "2012-10-17",
Statement: [{
Action: [
"sns:Publish",
"sns:Subscribe",
"dynamodb:*",
"s3:*",
],
Effect: "Allow",
Resource: "*",
}],
}),
});
const lambdaPolicyAttachment = new aws.iam.RolePolicyAttachment("lambdaPolicyAttachment", {
role: lambdaRole.name,
policyArn: lambdaPolicy.arn,
});
const lambdaFunction = new aws.lambda.Function("myLambdaFunction", {
runtime: aws.lambda.Runtime.NodeJS18dX,
role: lambdaRole.arn,
handler: "index.handler",
code: new pulumi.asset.AssetArchive({
".": new pulumi.asset.FileArchive("/Users/pavansomashekar/Desktop/serverless.zip")
}),
timeout: 30,
environment: {
variables: {
GOOGLE_CLOUD_BUCKET: bucket.name,
MAILGUN_API_KEY: mailgunApiKey,
MAILGUN_DOMAIN: mailgunDomain,
DYNAMODB_TABLE_NAME: dynamoTable.name,
GCP_SERVICE_ACCOUNT_KEY: serviceAccountKeys.privateKey.apply(key => key),
},
},
});
const lambdaPermission = new aws.lambda.Permission("snsLambdaPermission", {
action: "lambda:InvokeFunction",
function: lambdaFunction.arn,
principal: "sns.amazonaws.com",
sourceArn: mySnsTopic.arn,
});
const lambdaSubscription = new aws.sns.TopicSubscription("myLambdaSubscription", {
topic: mySnsTopic.arn,
protocol: "lambda",
endpoint: lambdaFunction.arn,
});
const aws_region = availableZones.names[0].slice(0, -1);
const userData = pulumi.all([rdsInstance.endpoint, rdsInstance.port, rdsInstance.dbName, rdsInstance.username, rdsInstance.password, aws_region, mySnsTopic.arn]).apply(([endpoint, port, dbName, username, password, region, snsTopicArn]) => {
const [hostname] = endpoint.split(":");
return `#!/bin/bash
echo "DB_HOST=${hostname}" >> /opt/AssignmentsAPI/AssignmentsAPI/.env
echo "DB_PORT=${port}" >> /opt/AssignmentsAPI/AssignmentsAPI/.env
echo "DB_USER=${username}" >> /opt/AssignmentsAPI/AssignmentsAPI/.env
echo "DB_PASSWORD=${password}" >> /opt/AssignmentsAPI/AssignmentsAPI/.env
echo "DB_DATABASE=${dbName}" >> /opt/AssignmentsAPI/AssignmentsAPI/.env
echo "AWS_REGION=${region}" >> /opt/AssignmentsAPI/AssignmentsAPI/.env
echo "SNS_TOPIC_ARN=${snsTopicArn}" >> /opt/AssignmentsAPI/AssignmentsAPI/.env
sudo systemctl daemon-reload
sudo systemctl enable assignments-api
sudo systemctl start assignments-api
sudo systemctl restart assignments-api
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent.json -s
`;
});
const userDataEncoded = userData.apply(ud => Buffer.from(ud).toString('base64'));
const launchTemplate = new aws.ec2.LaunchTemplate("appLaunchTemplate", {
imageId: amiId,
instanceType: instanceType,
keyName: keyName,
userData: userDataEncoded,
iamInstanceProfile: {
name: cloudWatchInstanceProfile.name,
},
networkInterfaces: [{
associatePublicIpAddress: true,
deleteOnTermination: true,
deviceIndex: 0,
securityGroups: [appSecurityGroup.id],
}],
});
const targetGroup = new aws.lb.TargetGroup("appTargetGroup", {
port: applicationPort,
protocol: "HTTP",
vpcId: vpc.id,
targetType: "instance",
healthCheck: {
enabled: true,
interval: 30,
path: "/healthz/",
port: "8080",
protocol: "HTTP",
healthyThreshold: 2,
unhealthyThreshold: 2,
timeout: 5,
},
});
const autoScalingGroup = new aws.autoscaling.Group("autoScalingGroup", {
launchTemplate: {
id: launchTemplate.id,
version: "$Latest",
},
minSize: 1,
maxSize: 3,
desiredCapacity: 1,
vpcZoneIdentifiers: resources.publicSubnets,
cooldown: 60,
targetGroupArns: [targetGroup.arn],
tags: [
{ key: "Name", value: "AutoScalingGroup", propagateAtLaunch: true },
],
});
const scaleUpPolicy = new aws.autoscaling.Policy("scaleUpPolicy", {
autoscalingGroupName: autoScalingGroup,
adjustmentType: "ChangeInCapacity",
scalingAdjustment: 1,
cooldown: 60,
metricAggregationType: "Average",
});
const scaleDownPolicy = new aws.autoscaling.Policy("scaleDownPolicy", {
autoscalingGroupName: autoScalingGroup,
adjustmentType: "ChangeInCapacity",
scalingAdjustment: -1,
cooldown: 60,
metricAggregationType: "Average",
});
const scaleUpAlarm = new aws.cloudwatch.MetricAlarm("scaleUpAlarm", {
comparisonOperator: "GreaterThanThreshold",
evaluationPeriods: 1,
metricName: "CPUUtilization",
namespace: "AWS/EC2",
period: 60,
statistic: "Average",
threshold: 5,
alarmActions: [scaleUpPolicy.arn],
dimensions: {
AutoScalingGroupName: autoScalingGroup.name,
},
});
const scaleDownAlarm = new aws.cloudwatch.MetricAlarm("scaleDownAlarm", {
comparisonOperator: "LessThanThreshold",
evaluationPeriods: 1,
metricName: "CPUUtilization",
namespace: "AWS/EC2",
period: 60,
statistic: "Average",
threshold: 3,
alarmActions: [scaleDownPolicy.arn],
dimensions: {
AutoScalingGroupName: autoScalingGroup.name,
},
});
const appLoadBalancer = new aws.lb.LoadBalancer("appLoadBalancer", {
internal: false,
loadBalancerType: "application",
securityGroups: [loadBalancerSecurityGroup.id],
subnets: resources.publicSubnets,
});
const listener = new aws.lb.Listener("appListener", {
loadBalancerArn: appLoadBalancer.arn,
port: 443,
protocol: "HTTPS",
sslPolicy: "ELBSecurityPolicy-2016-08",
certificateArn: certificateArn,
defaultActions: [{
type: "forward",
targetGroupArn: targetGroup.arn,
}],
});
const appAliasRecord = new route53.Record("appAliasRecord", {
zoneId: hostedZoneId,
name: domainName,
type: "A",
aliases: [{
name: appLoadBalancer.dnsName,
zoneId: appLoadBalancer.zoneId,
evaluateTargetHealth: true,
}],
});
});