-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
305 lines (277 loc) · 8.74 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
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";
const providerConfig = new pulumi.Config("aws");
const region = providerConfig.require("region");
const config = new pulumi.Config();
const appName = config.require("appName");
const cidrBlock = config.get("cidrBlock") || "10.0.0.0/16";
const billingId = config.get("billingId") || "123456";
const bucketName = config.get("bucketName") || `${appName}-bucket-${billingId}`;
const instanceType = config.get("instanceType") || "t2.micro";
const sshKeyPairName = config.get("sshKeyPairName"); // Manually create a key pair in the AWS Console
const domainName = config.get("domainName");
const asgMinSize = config.getNumber("asgMinSize") || 1;
const asgMaxSize = config.getNumber("asgMaxSize") || 5;
const asgDesiredCapacity = config.getNumber("asgDesiredCapacity") || 2;
const cpuHighThreshold = config.getNumber("cpuHighThreshold") || 80;
const cpuLowThreshold = config.getNumber("cpuLowThreshold") || 20;
const myIP = config.get("myIP") || "192.168.1.1";
const httpsEnabled = config.getBoolean("httpsEnabled") || false;
// Virtual Private Cloud
const vpc = new awsx.ec2.Vpc(`${appName}-vpc`, {
cidrBlock: cidrBlock,
numberOfAvailabilityZones: 2,
tags: {
Name: `${appName}-vpc`,
BillingId: billingId
},
});
// S3 bucket
const bucket = new aws.s3.Bucket(`${appName}-bucket`, {
bucket: bucketName,
tags: {
Name: `${appName}-bucket`,
BillingId: billingId
}
});
// IAM role and policy for EC2 instances to access the S3 bucket
const role = new aws.iam.Role(`${appName}-instances-role`, {
assumeRolePolicy: aws.iam.assumeRolePolicyForPrincipal({ Service: "ec2.amazonaws.com" }),
tags: {
Name: `${appName}-instances-role`,
BillingId: billingId
}
});
new aws.iam.RolePolicy(`${appName}-bucket-access-policy`, {
role: role.id,
policy: pulumi.interpolate`{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:ListBucket",
"s3:PutObject",
"s3:DeleteObject"
],
"Resource": [
"${bucket.arn}",
"${bucket.arn}/*"
]
}
]
}`,
});
// IAM instance profile
const instanceIAMProfile = new aws.iam.InstanceProfile(`${appName}-instance-iam-profile`, {
role: role.name,
tags: {
Name: `${appName}-instance-iam-profile`,
BillingId: billingId
}
});
// Security Group for the Load Balancer
const lbSecurityGroup = new aws.ec2.SecurityGroup(`${appName}-lb-sg`, {
vpcId: vpc.vpcId,
ingress: [
{ protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] },
{ 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"] },
],
tags: {
Name: `${appName}-lb-sg`,
BillingId: billingId
}
});
// Security group for the EC2 instances
const instanceSecurityGroup = new aws.ec2.SecurityGroup(`${appName}-instance-sg`, {
vpcId: vpc.vpcId,
description: "Allow all inbound traffic from the VPC",
ingress: [
{ protocol: "-1", fromPort: 0, toPort: 0, cidrBlocks: [vpc.vpc.cidrBlock] },
{ protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: [`${myIP}/32`] }
],
egress: [{ protocol: "-1", fromPort: 0, toPort: 0, cidrBlocks: ["0.0.0.0/0"] }],
tags: {
Name: `${appName}-instance-sg`,
BillingId: billingId
},
});
// Load Balancer
const lb = new aws.lb.LoadBalancer(`${appName}-lb`, {
securityGroups: [lbSecurityGroup.id],
subnets: vpc.publicSubnetIds,
tags: {
Name: `${appName}-lb`,
BillingId: billingId
}
});
// Target Group
const tg = new aws.lb.TargetGroup(`${appName}-lb-tg`, {
port: 80,
protocol: "HTTP",
targetType: "instance",
vpcId: vpc.vpcId,
tags: {
Name: `${appName}-tg`,
BillingId: billingId
}
});
// HTTP Listener
new aws.lb.Listener(`${appName}-http-listener`, {
loadBalancerArn: lb.arn,
port: 80,
defaultActions: [
{
type: "forward",
targetGroupArn: tg.arn,
},
],
});
const userData = Buffer.from(`#!/bin/bash
INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
echo "<h1>Hello from instance \${INSTANCE_ID}</h1>" > index.html
nohup python -m SimpleHTTPServer 80 &`).toString('base64');
// Launch Template for the EC2 instances
const launchTemplate = new aws.ec2.LaunchTemplate(`${appName}-instance-launch-template`, {
imageId: aws.ec2.getAmi({
filters: [
{ name: "name", values: ["amzn2-ami-hvm-*-x86_64-gp2"] },
],
owners: ["amazon"],
mostRecent: true,
}).then(result => result.id),
instanceType: instanceType,
vpcSecurityGroupIds: [instanceSecurityGroup.id],
iamInstanceProfile: { arn: instanceIAMProfile.arn },
userData: userData,
keyName: sshKeyPairName
});
// Auto Scaling Group
const asg = new aws.autoscaling.Group(`${appName}-asg`, {
vpcZoneIdentifiers: vpc.privateSubnetIds,
launchTemplate: { id: launchTemplate.id, version: "$Latest" },
minSize: asgMinSize,
maxSize: asgMaxSize,
desiredCapacity: asgDesiredCapacity,
targetGroupArns: [tg.arn],
healthCheckType: "ELB",
healthCheckGracePeriod: 300,
tags: [
{ key: "Name", value: `${appName}-asg`, propagateAtLaunch: true },
{ key: "BillingId", value: billingId, propagateAtLaunch: true }
],
});
// CPU Utilization Alarms and Actions
const scaleUpPolicy = new aws.autoscaling.Policy(`${appName}-scale-up`, {
autoscalingGroupName: asg.name,
adjustmentType: "ChangeInCapacity",
scalingAdjustment: 1,
cooldown: 300,
});
const scaleDownPolicy = new aws.autoscaling.Policy(`${appName}-scale-down`, {
autoscalingGroupName: asg.name,
adjustmentType: "ChangeInCapacity",
scalingAdjustment: -1,
cooldown: 300,
});
new aws.cloudwatch.MetricAlarm(`${appName}-cpu-high-alarm`, {
comparisonOperator: "GreaterThanOrEqualToThreshold",
evaluationPeriods: 2,
metricName: "CPUUtilization",
namespace: "AWS/EC2",
period: 60,
statistic: "Average",
threshold: cpuHighThreshold,
dimensions: { AutoScalingGroupName: asg.name },
alarmDescription: `Triggers a scale-out when CPU > ${cpuHighThreshold}%`,
alarmActions: [scaleUpPolicy.arn],
tags: {
Name: `${appName}-cpu-high-alarm`,
BillingId: billingId
}
});
new aws.cloudwatch.MetricAlarm(`${appName}-cpu-low-alarm`, {
comparisonOperator: "LessThanOrEqualToThreshold",
evaluationPeriods: 2,
metricName: "CPUUtilization",
namespace: "AWS/EC2",
period: 60,
statistic: "Average",
threshold: cpuLowThreshold,
dimensions: { AutoScalingGroupName: asg.name },
alarmDescription: `Triggers a scale-in when CPU < ${cpuLowThreshold}%`,
alarmActions: [scaleDownPolicy.arn],
tags: {
Name: `${appName}-cpu-low-alarm`,
BillingId: billingId
}
});
// HTTPS CONFIGURATION (OPTIONAL) *********************************************************************************
if (httpsEnabled && domainName) {
// Certificate
const certificate = new aws.acm.Certificate(`${appName}-certificate`, {
domainName: domainName,
validationMethod: "DNS",
tags: {
Name: `${appName}-certificate`,
BillingId: billingId
},
});
// Route 53 Record
const zone = new aws.route53.Zone(`${appName}-dns-zone`, {
name: domainName,
tags: {
Name: `${appName}-dns-zone`,
BillingId: billingId
}
});
const validationRecord = new aws.route53.Record(`${appName}-validation-record`, {
name: certificate.domainValidationOptions[0].resourceRecordName,
zoneId: zone.id,
type: certificate.domainValidationOptions[0].resourceRecordType,
records: [certificate.domainValidationOptions[0].resourceRecordValue],
ttl: 60,
});
// Validate the ACM certificate
const validation = new aws.acm.CertificateValidation(`${appName}-validation`, {
certificateArn: certificate.arn,
validationRecordFqdns: [validationRecord.fqdn],
});
// Route 53 Record for the Load Balancer
new aws.route53.Record(`${appName}-lb-record`, {
name: domainName,
zoneId: zone.id,
type: "A",
aliases: [
{
name: lb.dnsName,
zoneId: lb.zoneId,
evaluateTargetHealth: true,
},
],
});
// HTTPS Listener
new aws.lb.Listener(`${appName}-https-listener`, {
loadBalancerArn: lb.arn,
port: 443,
certificateArn: certificate.arn,
defaultActions: [
{
type: "forward",
targetGroupArn: tg.arn,
},
],
tags: {
Name: `${appName}-https-listener`,
BillingId: billingId
}
}, { dependsOn: validation });
}
// ********************************************************************************************************************
// Exports
export const LoadBalancerDNSName = lb.dnsName;