Skip to content

Commit e5375c7

Browse files
committed
Extract web server constructor logic to private methods
1 parent 19d6997 commit e5375c7

File tree

1 file changed

+127
-65
lines changed

1 file changed

+127
-65
lines changed

src/components/web-server.ts

Lines changed: 127 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ const defaults = {
121121
};
122122

123123
export class WebServer extends pulumi.ComponentResource {
124+
name: string;
124125
certificate: AcmCertificate;
125126
logGroup: aws.cloudwatch.LogGroup;
126127
lbSecurityGroup: aws.ec2.SecurityGroup;
127128
lb: aws.lb.LoadBalancer;
128129
lbTargetGroup: aws.lb.TargetGroup;
129130
lbHttpListener: aws.lb.Listener;
130131
lbTlsListener: aws.lb.Listener;
131-
serviceSecurityGroup: aws.ec2.SecurityGroup;
132132
taskDefinition: aws.ecs.TaskDefinition;
133133
service: aws.ecs.Service;
134134

@@ -139,30 +139,66 @@ export class WebServer extends pulumi.ComponentResource {
139139
) {
140140
super('studion:WebServer', name, {}, opts);
141141

142-
const argsWithDefaults = Object.assign({}, defaults, args);
142+
this.name = name;
143+
const { domain, hostedZoneId, vpc, port, healtCheckPath } = args;
144+
this.certificate = this.createTlsCertificate({ domain, hostedZoneId });
145+
this.logGroup = this.createLogGroup();
146+
const {
147+
lb,
148+
lbTargetGroup,
149+
lbHttpListener,
150+
lbTlsListener,
151+
lbSecurityGroup,
152+
} = this.createLoadBalancer({ vpc, port, healtCheckPath });
153+
this.lb = lb;
154+
this.lbTargetGroup = lbTargetGroup;
155+
this.lbHttpListener = lbHttpListener;
156+
this.lbTlsListener = lbTlsListener;
157+
this.lbSecurityGroup = lbSecurityGroup;
158+
this.taskDefinition = this.createTaskDefinition(args);
159+
this.service = this.createEcsService(args);
160+
this.createDnsRecord({ domain, hostedZoneId });
161+
this.enableAutoscaling(args);
162+
163+
this.registerOutputs();
164+
}
143165

144-
this.certificate = new AcmCertificate(
145-
`${argsWithDefaults.domain}-acm-certificate`,
166+
private createTlsCertificate({
167+
domain,
168+
hostedZoneId,
169+
}: Pick<WebServerArgs, 'domain' | 'hostedZoneId'>) {
170+
const certificate = new AcmCertificate(
171+
`${domain}-acm-certificate`,
146172
{
147-
domain: argsWithDefaults.domain,
148-
hostedZoneId: argsWithDefaults.hostedZoneId,
173+
domain,
174+
hostedZoneId,
149175
},
150176
{ parent: this },
151177
);
178+
return certificate;
179+
}
152180

153-
this.logGroup = new aws.cloudwatch.LogGroup(
154-
`${name}-log-group`,
181+
private createLogGroup() {
182+
const logGroup = new aws.cloudwatch.LogGroup(
183+
`${this.name}-log-group`,
155184
{
156185
retentionInDays: 14,
157-
name: `/ecs/${name}`,
186+
namePrefix: `/ecs/${this.name}-`,
158187
},
159188
{ parent: this },
160189
);
190+
return logGroup;
191+
}
161192

162-
this.lbSecurityGroup = new aws.ec2.SecurityGroup(
163-
`${name}-lb-security-group`,
193+
private createLoadBalancer({
194+
vpc,
195+
port,
196+
healtCheckPath,
197+
}: Pick<WebServerArgs, 'vpc' | 'port' | 'healtCheckPath'>) {
198+
const lbSecurityGroup = new aws.ec2.SecurityGroup(
199+
`${this.name}-lb-security-group`,
164200
{
165-
vpcId: argsWithDefaults.vpc.vpcId,
201+
vpcId: vpc.vpcId,
166202
ingress: [
167203
{
168204
protocol: 'tcp',
@@ -189,40 +225,40 @@ export class WebServer extends pulumi.ComponentResource {
189225
{ parent: this },
190226
);
191227

192-
this.lb = new aws.lb.LoadBalancer(
193-
`${name}-lb`,
228+
const lb = new aws.lb.LoadBalancer(
229+
`${this.name}-lb`,
194230
{
195-
name: `${name}-lb`,
231+
namePrefix: `${this.name}-lb-`,
196232
loadBalancerType: 'application',
197-
subnets: argsWithDefaults.vpc.publicSubnetIds,
198-
securityGroups: [this.lbSecurityGroup.id],
233+
subnets: vpc.publicSubnetIds,
234+
securityGroups: [lbSecurityGroup.id],
199235
internal: false,
200236
ipAddressType: 'ipv4',
201237
},
202238
{ parent: this },
203239
);
204240

205-
this.lbTargetGroup = new aws.lb.TargetGroup(
206-
`${name}-lb-tg`,
241+
const lbTargetGroup = new aws.lb.TargetGroup(
242+
`${this.name}-lb-tg`,
207243
{
208-
name: `${name}-lb-tg`,
209-
port: argsWithDefaults.port,
244+
namePrefix: `${this.name}-lb-tg-`,
245+
port,
210246
protocol: 'HTTP',
211247
targetType: 'ip',
212-
vpcId: argsWithDefaults.vpc.vpcId,
248+
vpcId: vpc.vpcId,
213249
healthCheck: {
214250
healthyThreshold: 3,
215251
unhealthyThreshold: 2,
216252
interval: 60,
217253
timeout: 5,
218-
path: argsWithDefaults.healtCheckPath,
254+
path: healtCheckPath || defaults.healtCheckPath,
219255
},
220256
},
221257
{ parent: this, dependsOn: [this.lb] },
222258
);
223259

224-
this.lbHttpListener = new aws.lb.Listener(
225-
`${name}-lb-listener-80`,
260+
const lbHttpListener = new aws.lb.Listener(
261+
`${this.name}-lb-listener-80`,
226262
{
227263
loadBalancerArn: this.lb.arn,
228264
port: 80,
@@ -240,8 +276,8 @@ export class WebServer extends pulumi.ComponentResource {
240276
{ parent: this },
241277
);
242278

243-
this.lbTlsListener = new aws.lb.Listener(
244-
`${name}-lb-listener-443`,
279+
const lbTlsListener = new aws.lb.Listener(
280+
`${this.name}-lb-listener-443`,
245281
{
246282
loadBalancerArn: this.lb.arn,
247283
port: 443,
@@ -258,25 +294,20 @@ export class WebServer extends pulumi.ComponentResource {
258294
{ parent: this },
259295
);
260296

261-
const albAliasRecord = new aws.route53.Record(
262-
`${name}-route53-record`,
263-
{
264-
type: 'A',
265-
name: argsWithDefaults.domain,
266-
zoneId: argsWithDefaults.hostedZoneId,
267-
aliases: [
268-
{
269-
name: this.lb.dnsName,
270-
zoneId: this.lb.zoneId,
271-
evaluateTargetHealth: true,
272-
},
273-
],
274-
},
275-
{ parent: this },
276-
);
297+
return {
298+
lb,
299+
lbTargetGroup,
300+
lbHttpListener,
301+
lbTlsListener,
302+
lbSecurityGroup,
303+
};
304+
}
305+
306+
private createTaskDefinition(args: WebServerArgs) {
307+
const argsWithDefaults = Object.assign({}, defaults, args);
277308

278309
const secretManagerSecretsInlinePolicy = {
279-
name: `${name}-secret-manager-access`,
310+
name: `${this.name}-secret-manager-access`,
280311
policy: JSON.stringify({
281312
Version: '2012-10-17',
282313
Statement: [
@@ -291,9 +322,9 @@ export class WebServer extends pulumi.ComponentResource {
291322
};
292323

293324
const taskExecutionRole = new aws.iam.Role(
294-
`${name}-ecs-task-exec-role`,
325+
`${this.name}-ecs-task-exec-role`,
295326
{
296-
name: `${name}-ecs-task-exec-role`,
327+
namePrefix: `${this.name}-ecs-task-exec-role-`,
297328
assumeRolePolicy,
298329
managedPolicyArns: [
299330
'arn:aws:iam::aws:policy/CloudWatchFullAccess',
@@ -308,7 +339,7 @@ export class WebServer extends pulumi.ComponentResource {
308339
);
309340

310341
const execCmdInlinePolicy = {
311-
name: `${name}-ecs-exec`,
342+
name: `${this.name}-ecs-exec`,
312343
policy: JSON.stringify({
313344
Version: '2012-10-17',
314345
Statement: [
@@ -328,9 +359,9 @@ export class WebServer extends pulumi.ComponentResource {
328359
};
329360

330361
const taskRole = new aws.iam.Role(
331-
`${name}-ecs-task-role`,
362+
`${this.name}-ecs-task-role`,
332363
{
333-
name: `${name}-ecs-task-role`,
364+
namePrefix: `${this.name}-ecs-task-role-`,
334365
assumeRolePolicy,
335366
inlinePolicies: [
336367
execCmdInlinePolicy,
@@ -354,10 +385,10 @@ export class WebServer extends pulumi.ComponentResource {
354385
throw Error('Incorrect EcsService size argument');
355386
});
356387

357-
this.taskDefinition = new aws.ecs.TaskDefinition(
358-
`${name}-task-definition`,
388+
const taskDefinition = new aws.ecs.TaskDefinition(
389+
`${this.name}-task-definition`,
359390
{
360-
family: `${name}-task-definition`,
391+
family: `${this.name}-task-definition`,
361392
networkMode: 'awsvpc',
362393
executionRoleArn: taskExecutionRole.arn,
363394
taskRoleArn: taskRole.arn,
@@ -366,7 +397,7 @@ export class WebServer extends pulumi.ComponentResource {
366397
requiresCompatibilities: ['FARGATE'],
367398
containerDefinitions: pulumi
368399
.all([
369-
name,
400+
this.name,
370401
argsWithDefaults.image,
371402
argsWithDefaults.port,
372403
argsWithDefaults.environment,
@@ -415,8 +446,14 @@ export class WebServer extends pulumi.ComponentResource {
415446
{ parent: this },
416447
);
417448

418-
this.serviceSecurityGroup = new aws.ec2.SecurityGroup(
419-
`${name}-security-group`,
449+
return taskDefinition;
450+
}
451+
452+
private createEcsService(args: WebServerArgs) {
453+
const argsWithDefaults = Object.assign({}, defaults, args);
454+
455+
const serviceSecurityGroup = new aws.ec2.SecurityGroup(
456+
`${this.name}-security-group`,
420457
{
421458
vpcId: argsWithDefaults.vpc.vpcId,
422459
ingress: [
@@ -439,26 +476,26 @@ export class WebServer extends pulumi.ComponentResource {
439476
{ parent: this },
440477
);
441478

442-
this.service = new aws.ecs.Service(
443-
`${name}-service`,
479+
const service = new aws.ecs.Service(
480+
`${this.name}-service`,
444481
{
445-
name,
482+
name: this.name,
446483
cluster: argsWithDefaults.cluster.id,
447484
launchType: 'FARGATE',
448485
desiredCount: argsWithDefaults.desiredCount,
449486
taskDefinition: this.taskDefinition.arn,
450487
enableExecuteCommand: true,
451488
loadBalancers: [
452489
{
453-
containerName: name,
490+
containerName: this.name,
454491
containerPort: argsWithDefaults.port,
455492
targetGroupArn: this.lbTargetGroup.arn,
456493
},
457494
],
458495
networkConfiguration: {
459496
assignPublicIp: true,
460497
subnets: argsWithDefaults.vpc.publicSubnetIds,
461-
securityGroups: [this.serviceSecurityGroup.id],
498+
securityGroups: [serviceSecurityGroup.id],
462499
},
463500
tags: argsWithDefaults.tags,
464501
},
@@ -472,9 +509,36 @@ export class WebServer extends pulumi.ComponentResource {
472509
],
473510
},
474511
);
512+
return service;
513+
}
514+
515+
private createDnsRecord({
516+
domain,
517+
hostedZoneId,
518+
}: Pick<WebServerArgs, 'domain' | 'hostedZoneId'>) {
519+
const albAliasRecord = new aws.route53.Record(
520+
`${this.name}-route53-record`,
521+
{
522+
type: 'A',
523+
name: domain,
524+
zoneId: hostedZoneId,
525+
aliases: [
526+
{
527+
name: this.lb.dnsName,
528+
zoneId: this.lb.zoneId,
529+
evaluateTargetHealth: true,
530+
},
531+
],
532+
},
533+
{ parent: this },
534+
);
535+
}
536+
537+
private enableAutoscaling(args: WebServerArgs) {
538+
const argsWithDefaults = Object.assign({}, defaults, args);
475539

476540
const autoscalingTarget = new aws.appautoscaling.Target(
477-
`${name}-autoscale-target`,
541+
`${this.name}-autoscale-target`,
478542
{
479543
minCapacity: argsWithDefaults.minCount,
480544
maxCapacity: argsWithDefaults.maxCount,
@@ -486,7 +550,7 @@ export class WebServer extends pulumi.ComponentResource {
486550
);
487551

488552
const memoryAutoscalingPolicy = new aws.appautoscaling.Policy(
489-
`${name}-memory-autoscale-policy`,
553+
`${this.name}-memory-autoscale-policy`,
490554
{
491555
policyType: 'TargetTrackingScaling',
492556
resourceId: autoscalingTarget.resourceId,
@@ -503,7 +567,7 @@ export class WebServer extends pulumi.ComponentResource {
503567
);
504568

505569
const cpuAutoscalingPolicy = new aws.appautoscaling.Policy(
506-
`${name}-cpu-autoscale-policy`,
570+
`${this.name}-cpu-autoscale-policy`,
507571
{
508572
policyType: 'TargetTrackingScaling',
509573
resourceId: autoscalingTarget.resourceId,
@@ -518,7 +582,5 @@ export class WebServer extends pulumi.ComponentResource {
518582
},
519583
{ parent: this },
520584
);
521-
522-
this.registerOutputs();
523585
}
524586
}

0 commit comments

Comments
 (0)