/
application-load-balancer.ts
688 lines (608 loc) · 20.5 KB
/
application-load-balancer.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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import { Duration, Lazy, Names, Resource } from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import { Construct } from 'constructs';
import { ApplicationELBMetrics } from '../elasticloadbalancingv2-canned-metrics.generated';
import { BaseLoadBalancer, BaseLoadBalancerLookupOptions, BaseLoadBalancerProps, ILoadBalancerV2 } from '../shared/base-load-balancer';
import { IpAddressType, ApplicationProtocol } from '../shared/enums';
import { ApplicationListener, BaseApplicationListenerProps } from './application-listener';
import { ListenerAction } from './application-listener-action';
/**
* Properties for defining an Application Load Balancer
*/
export interface ApplicationLoadBalancerProps extends BaseLoadBalancerProps {
/**
* Security group to associate with this load balancer
*
* @default A security group is created
*/
readonly securityGroup?: ec2.ISecurityGroup;
/**
* The type of IP addresses to use
*
* Only applies to application load balancers.
*
* @default IpAddressType.Ipv4
*/
readonly ipAddressType?: IpAddressType;
/**
* Indicates whether HTTP/2 is enabled.
*
* @default true
*/
readonly http2Enabled?: boolean;
/**
* The load balancer idle timeout, in seconds
*
* @default 60
*/
readonly idleTimeout?: Duration;
}
/**
* Options for looking up an ApplicationLoadBalancer
*/
export interface ApplicationLoadBalancerLookupOptions extends BaseLoadBalancerLookupOptions {
}
/**
* Define an Application Load Balancer
*
* @resource AWS::ElasticLoadBalancingV2::LoadBalancer
*/
export class ApplicationLoadBalancer extends BaseLoadBalancer implements IApplicationLoadBalancer {
/**
* Look up an application load balancer.
*/
public static fromLookup(scope: Construct, id: string, options: ApplicationLoadBalancerLookupOptions): IApplicationLoadBalancer {
const props = BaseLoadBalancer._queryContextProvider(scope, {
userOptions: options,
loadBalancerType: cxschema.LoadBalancerType.APPLICATION,
});
return new LookedUpApplicationLoadBalancer(scope, id, props);
}
/**
* Import an existing Application Load Balancer
*/
public static fromApplicationLoadBalancerAttributes(
scope: Construct, id: string, attrs: ApplicationLoadBalancerAttributes): IApplicationLoadBalancer {
return new ImportedApplicationLoadBalancer(scope, id, attrs);
}
public readonly connections: ec2.Connections;
public readonly ipAddressType?: IpAddressType;
constructor(scope: Construct, id: string, props: ApplicationLoadBalancerProps) {
super(scope, id, props, {
type: 'application',
securityGroups: Lazy.list({ produce: () => this.connections.securityGroups.map(sg => sg.securityGroupId) }),
ipAddressType: props.ipAddressType,
});
this.ipAddressType = props.ipAddressType ?? IpAddressType.IPV4;
const securityGroups = [props.securityGroup || new ec2.SecurityGroup(this, 'SecurityGroup', {
vpc: props.vpc,
description: `Automatically created Security Group for ELB ${Names.uniqueId(this)}`,
allowAllOutbound: false,
})];
this.connections = new ec2.Connections({ securityGroups });
if (props.http2Enabled === false) { this.setAttribute('routing.http2.enabled', 'false'); }
if (props.idleTimeout !== undefined) { this.setAttribute('idle_timeout.timeout_seconds', props.idleTimeout.toSeconds().toString()); }
}
/**
* Add a new listener to this load balancer
*/
public addListener(id: string, props: BaseApplicationListenerProps): ApplicationListener {
return new ApplicationListener(this, id, {
loadBalancer: this,
...props,
});
}
/**
* Add a redirection listener to this load balancer
*/
public addRedirect(props: ApplicationLoadBalancerRedirectConfig = {}): ApplicationListener {
const sourcePort = props.sourcePort ?? 80;
const targetPort = (props.targetPort ?? 443).toString();
return this.addListener(`Redirect${sourcePort}To${targetPort}`, {
protocol: props.sourceProtocol ?? ApplicationProtocol.HTTP,
port: sourcePort,
open: props.open ?? true,
defaultAction: ListenerAction.redirect({
port: targetPort,
protocol: props.targetProtocol ?? ApplicationProtocol.HTTPS,
permanent: true,
}),
});
}
/**
* Add a security group to this load balancer
*/
public addSecurityGroup(securityGroup: ec2.ISecurityGroup) {
this.connections.addSecurityGroup(securityGroup);
}
/**
* Return the given named metric for this Application Load Balancer
*
* @default Average over 5 minutes
*/
public metric(metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return new cloudwatch.Metric({
namespace: 'AWS/ApplicationELB',
metricName,
dimensions: { LoadBalancer: this.loadBalancerFullName },
...props,
});
}
/**
* The total number of concurrent TCP connections active from clients to the
* load balancer and from the load balancer to targets.
*
* @default Sum over 5 minutes
*/
public metricActiveConnectionCount(props?: cloudwatch.MetricOptions) {
return this.cannedMetric(ApplicationELBMetrics.activeConnectionCountSum, props);
}
/**
* The number of TLS connections initiated by the client that did not
* establish a session with the load balancer. Possible causes include a
* mismatch of ciphers or protocols.
*
* @default Sum over 5 minutes
*/
public metricClientTlsNegotiationErrorCount(props?: cloudwatch.MetricOptions) {
return this.cannedMetric(ApplicationELBMetrics.clientTlsNegotiationErrorCountSum, props);
}
/**
* The number of load balancer capacity units (LCU) used by your load balancer.
*
* @default Sum over 5 minutes
*/
public metricConsumedLCUs(props?: cloudwatch.MetricOptions) {
return this.cannedMetric(ApplicationELBMetrics.consumedLcUsAverage, {
statistic: 'sum',
...props,
});
}
/**
* The number of fixed-response actions that were successful.
*
* @default Sum over 5 minutes
*/
public metricHttpFixedResponseCount(props?: cloudwatch.MetricOptions) {
return this.cannedMetric(ApplicationELBMetrics.httpFixedResponseCountSum, props);
}
/**
* The number of redirect actions that were successful.
*
* @default Sum over 5 minutes
*/
public metricHttpRedirectCount(props?: cloudwatch.MetricOptions) {
return this.cannedMetric(ApplicationELBMetrics.httpRedirectCountSum, props);
}
/**
* The number of redirect actions that couldn't be completed because the URL
* in the response location header is larger than 8K.
*
* @default Sum over 5 minutes
*/
public metricHttpRedirectUrlLimitExceededCount(props?: cloudwatch.MetricOptions) {
return this.cannedMetric(ApplicationELBMetrics.httpRedirectUrlLimitExceededCountSum, props);
}
/**
* The number of HTTP 3xx/4xx/5xx codes that originate from the load balancer.
*
* This does not include any response codes generated by the targets.
*
* @default Sum over 5 minutes
*/
public metricHttpCodeElb(code: HttpCodeElb, props?: cloudwatch.MetricOptions) {
return this.metric(code, {
statistic: 'Sum',
...props,
});
}
/**
* The number of HTTP 2xx/3xx/4xx/5xx response codes generated by all targets
* in the load balancer.
*
* This does not include any response codes generated by the load balancer.
*
* @default Sum over 5 minutes
*/
public metricHttpCodeTarget(code: HttpCodeTarget, props?: cloudwatch.MetricOptions) {
return this.metric(code, {
statistic: 'Sum',
...props,
});
}
/**
* The total number of bytes processed by the load balancer over IPv6.
*
* @default Sum over 5 minutes
*/
public metricIpv6ProcessedBytes(props?: cloudwatch.MetricOptions) {
return this.cannedMetric(ApplicationELBMetrics.iPv6ProcessedBytesSum, props);
}
/**
* The number of IPv6 requests received by the load balancer.
*
* @default Sum over 5 minutes
*/
public metricIpv6RequestCount(props?: cloudwatch.MetricOptions) {
return this.cannedMetric(ApplicationELBMetrics.iPv6RequestCountSum, props);
}
/**
* The total number of new TCP connections established from clients to the
* load balancer and from the load balancer to targets.
*
* @default Sum over 5 minutes
*/
public metricNewConnectionCount(props?: cloudwatch.MetricOptions) {
return this.cannedMetric(ApplicationELBMetrics.newConnectionCountSum, props);
}
/**
* The total number of bytes processed by the load balancer over IPv4 and IPv6.
*
* @default Sum over 5 minutes
*/
public metricProcessedBytes(props?: cloudwatch.MetricOptions) {
return this.cannedMetric(ApplicationELBMetrics.processedBytesSum, props);
}
/**
* The number of connections that were rejected because the load balancer had
* reached its maximum number of connections.
*
* @default Sum over 5 minutes
*/
public metricRejectedConnectionCount(props?: cloudwatch.MetricOptions) {
return this.cannedMetric(ApplicationELBMetrics.rejectedConnectionCountSum, props);
}
/**
* The number of requests processed over IPv4 and IPv6.
*
* This count includes only the requests with a response generated by a target of the load balancer.
*
* @default Sum over 5 minutes
*/
public metricRequestCount(props?: cloudwatch.MetricOptions) {
return this.cannedMetric(ApplicationELBMetrics.requestCountSum, props);
}
/**
* The number of rules processed by the load balancer given a request rate averaged over an hour.
*
* @default Sum over 5 minutes
*/
public metricRuleEvaluations(props?: cloudwatch.MetricOptions) {
return this.cannedMetric(ApplicationELBMetrics.ruleEvaluationsSum, props);
}
/**
* The number of connections that were not successfully established between the load balancer and target.
*
* @default Sum over 5 minutes
*/
public metricTargetConnectionErrorCount(props?: cloudwatch.MetricOptions) {
return this.metric('TargetConnectionErrorCount', {
statistic: 'Sum',
...props,
});
}
/**
* The time elapsed, in seconds, after the request leaves the load balancer until a response from the target is received.
*
* @default Average over 5 minutes
*/
public metricTargetResponseTime(props?: cloudwatch.MetricOptions) {
return this.metric('TargetResponseTime', {
statistic: 'Average',
...props,
});
}
/**
* The number of TLS connections initiated by the load balancer that did not establish a session with the target.
*
* Possible causes include a mismatch of ciphers or protocols.
*
* @default Sum over 5 minutes
*/
public metricTargetTLSNegotiationErrorCount(props?: cloudwatch.MetricOptions) {
return this.metric('TargetTLSNegotiationErrorCount', {
statistic: 'Sum',
...props,
});
}
/**
* The number of user authentications that could not be completed
*
* Because an authenticate action was misconfigured, the load balancer
* couldn't establish a connection with the IdP, or the load balancer
* couldn't complete the authentication flow due to an internal error.
*
* @default Sum over 5 minutes
*/
public metricElbAuthError(props?: cloudwatch.MetricOptions) {
return this.metric('ELBAuthError', {
statistic: 'Sum',
...props,
});
}
/**
* The number of user authentications that could not be completed because the
* IdP denied access to the user or an authorization code was used more than
* once.
*
* @default Sum over 5 minutes
*/
public metricElbAuthFailure(props?: cloudwatch.MetricOptions) {
return this.metric('ELBAuthFailure', {
statistic: 'Sum',
...props,
});
}
/**
* The time elapsed, in milliseconds, to query the IdP for the ID token and user info.
*
* If one or more of these operations fail, this is the time to failure.
*
* @default Average over 5 minutes
*/
public metricElbAuthLatency(props?: cloudwatch.MetricOptions) {
return this.metric('ELBAuthLatency', {
statistic: 'Average',
...props,
});
}
/**
* The number of authenticate actions that were successful.
*
* This metric is incremented at the end of the authentication workflow,
* after the load balancer has retrieved the user claims from the IdP.
*
* @default Sum over 5 minutes
*/
public metricElbAuthSuccess(props?: cloudwatch.MetricOptions) {
return this.metric('ELBAuthSuccess', {
statistic: 'Sum',
...props,
});
}
private cannedMetric(
fn: (dims: { LoadBalancer: string }) => cloudwatch.MetricProps,
props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return new cloudwatch.Metric({
...fn({ LoadBalancer: this.loadBalancerFullName }),
...props,
}).attachTo(this);
}
}
/**
* Count of HTTP status originating from the load balancer
*
* This count does not include any response codes generated by the targets.
*/
export enum HttpCodeElb {
/**
* The number of HTTP 3XX redirection codes that originate from the load balancer.
*/
ELB_3XX_COUNT = 'HTTPCode_ELB_3XX_Count',
/**
* The number of HTTP 4XX client error codes that originate from the load balancer.
*
* Client errors are generated when requests are malformed or incomplete.
* These requests have not been received by the target. This count does not
* include any response codes generated by the targets.
*/
ELB_4XX_COUNT = 'HTTPCode_ELB_4XX_Count',
/**
* The number of HTTP 5XX server error codes that originate from the load balancer.
*/
ELB_5XX_COUNT = 'HTTPCode_ELB_5XX_Count',
}
/**
* Count of HTTP status originating from the targets
*/
export enum HttpCodeTarget {
/**
* The number of 2xx response codes from targets
*/
TARGET_2XX_COUNT = 'HTTPCode_Target_2XX_Count',
/**
* The number of 3xx response codes from targets
*/
TARGET_3XX_COUNT = 'HTTPCode_Target_3XX_Count',
/**
* The number of 4xx response codes from targets
*/
TARGET_4XX_COUNT = 'HTTPCode_Target_4XX_Count',
/**
* The number of 5xx response codes from targets
*/
TARGET_5XX_COUNT = 'HTTPCode_Target_5XX_Count'
}
/**
* An application load balancer
*/
export interface IApplicationLoadBalancer extends ILoadBalancerV2, ec2.IConnectable {
/**
* The ARN of this load balancer
*/
readonly loadBalancerArn: string;
/**
* The VPC this load balancer has been created in (if available).
* If this interface is the result of an import call to fromApplicationLoadBalancerAttributes,
* the vpc attribute will be undefined unless specified in the optional properties of that method.
*/
readonly vpc?: ec2.IVpc;
/**
* The IP Address Type for this load balancer
*
* @default IpAddressType.IPV4
*/
readonly ipAddressType?: IpAddressType;
/**
* Add a new listener to this load balancer
*/
addListener(id: string, props: BaseApplicationListenerProps): ApplicationListener;
}
/**
* Properties to reference an existing load balancer
*/
export interface ApplicationLoadBalancerAttributes {
/**
* ARN of the load balancer
*/
readonly loadBalancerArn: string;
/**
* ID of the load balancer's security group
*/
readonly securityGroupId: string;
/**
* The canonical hosted zone ID of this load balancer
*
* @default - When not provided, LB cannot be used as Route53 Alias target.
*/
readonly loadBalancerCanonicalHostedZoneId?: string;
/**
* The DNS name of this load balancer
*
* @default - When not provided, LB cannot be used as Route53 Alias target.
*/
readonly loadBalancerDnsName?: string;
/**
* Whether the security group allows all outbound traffic or not
*
* Unless set to `false`, no egress rules will be added to the security group.
*
* @default true
*/
readonly securityGroupAllowsAllOutbound?: boolean;
/**
* The VPC this load balancer has been created in, if available
*
* @default - If the Load Balancer was imported and a VPC was not specified,
* the VPC is not available.
*/
readonly vpc?: ec2.IVpc;
}
/**
* An ApplicationLoadBalancer that has been defined elsewhere
*/
class ImportedApplicationLoadBalancer extends Resource implements IApplicationLoadBalancer {
/**
* Manage connections for this load balancer
*/
public readonly connections: ec2.Connections;
/**
* ARN of the load balancer
*/
public readonly loadBalancerArn: string;
/**
* VPC of the load balancer
*
* Undefined if optional vpc is not specified.
*/
public readonly vpc?: ec2.IVpc;
constructor(scope: Construct, id: string, private readonly props: ApplicationLoadBalancerAttributes) {
super(scope, id, {
environmentFromArn: props.loadBalancerArn,
});
this.vpc = props.vpc;
this.loadBalancerArn = props.loadBalancerArn;
this.connections = new ec2.Connections({
securityGroups: [ec2.SecurityGroup.fromSecurityGroupId(this, 'SecurityGroup', props.securityGroupId, {
allowAllOutbound: props.securityGroupAllowsAllOutbound,
})],
});
}
public addListener(id: string, props: BaseApplicationListenerProps): ApplicationListener {
return new ApplicationListener(this, id, {
loadBalancer: this,
...props,
});
}
public get loadBalancerCanonicalHostedZoneId(): string {
if (this.props.loadBalancerCanonicalHostedZoneId) { return this.props.loadBalancerCanonicalHostedZoneId; }
// eslint-disable-next-line max-len
throw new Error(`'loadBalancerCanonicalHostedZoneId' was not provided when constructing Application Load Balancer ${this.node.path} from attributes`);
}
public get loadBalancerDnsName(): string {
if (this.props.loadBalancerDnsName) { return this.props.loadBalancerDnsName; }
// eslint-disable-next-line max-len
throw new Error(`'loadBalancerDnsName' was not provided when constructing Application Load Balancer ${this.node.path} from attributes`);
}
}
class LookedUpApplicationLoadBalancer extends Resource implements IApplicationLoadBalancer {
public readonly loadBalancerArn: string;
public readonly loadBalancerCanonicalHostedZoneId: string;
public readonly loadBalancerDnsName: string;
public readonly ipAddressType?: IpAddressType;
public readonly connections: ec2.Connections;
public readonly vpc?: ec2.IVpc;
constructor(scope: Construct, id: string, props: cxapi.LoadBalancerContextResponse) {
super(scope, id, {
environmentFromArn: props.loadBalancerArn,
});
this.loadBalancerArn = props.loadBalancerArn;
this.loadBalancerCanonicalHostedZoneId = props.loadBalancerCanonicalHostedZoneId;
this.loadBalancerDnsName = props.loadBalancerDnsName;
if (props.ipAddressType === cxapi.LoadBalancerIpAddressType.IPV4) {
this.ipAddressType = IpAddressType.IPV4;
} else if (props.ipAddressType === cxapi.LoadBalancerIpAddressType.DUAL_STACK) {
this.ipAddressType = IpAddressType.DUAL_STACK;
}
this.vpc = ec2.Vpc.fromLookup(this, 'Vpc', {
vpcId: props.vpcId,
});
this.connections = new ec2.Connections();
for (const securityGroupId of props.securityGroupIds) {
const securityGroup = ec2.SecurityGroup.fromLookup(this, `SecurityGroup-${securityGroupId}`, securityGroupId);
this.connections.addSecurityGroup(securityGroup);
}
}
public addListener(id: string, props: BaseApplicationListenerProps): ApplicationListener {
return new ApplicationListener(this, id, {
...props,
loadBalancer: this,
});
}
}
/**
* Properties for a redirection config
*/
export interface ApplicationLoadBalancerRedirectConfig {
/**
* The protocol of the listener being created
*
* @default HTTP
*/
readonly sourceProtocol?: ApplicationProtocol;
/**
* The port number to listen to
*
* @default 80
*/
readonly sourcePort?: number;
/**
* The protocol of the redirection target
*
* @default HTTPS
*/
readonly targetProtocol?: ApplicationProtocol;
/**
* The port number to redirect to
*
* @default 443
*/
readonly targetPort?: number;
/**
* Allow anyone to connect to this listener
*
* If this is specified, the listener will be opened up to anyone who can reach it.
* For internal load balancers this is anyone in the same VPC. For public load
* balancers, this is anyone on the internet.
*
* If you want to be more selective about who can access this load
* balancer, set this to `false` and use the listener's `connections`
* object to selectively grant access to the listener.
*
* @default true
*/
readonly open?: boolean;
}