Skip to content

Commit cb1b92f

Browse files
author
Eugene Cheung
authored
feat(ecs-patterns): improve ALB/NLB type guards (#480)
Fixes #476 This is an alternative to PR #477 without a breaking API change. --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent b915c4a commit cb1b92f

File tree

4 files changed

+838
-9
lines changed

4 files changed

+838
-9
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"editor.codeActionsOnSave": {
3-
"source.fixAll.eslint": true
3+
"source.fixAll.eslint": "explicit"
44
},
55
"eslint.validate": ["javascript", "typescript"]
66
}

lib/monitoring/aws-loadbalancing/LoadBalancerMetricFactory.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,27 @@ import { MetricFactory, MetricWithAlarmSupport } from "../../common";
1212
// It's not possible to use instanceOf with typescript interfaces, so we have to use type guards to differentiate
1313
// between the interfaces. As another problem, the LoadBalancer/TargetGroup for both Network and Application types
1414
// don't have distinguished fields that could be used to differentiate between both types, so we resort to using
15-
// the name of the class below.
16-
//
17-
// Ideally the 2 interfaces would provide a specific field to distinguish both types.
15+
// checking for unique methods in their metrics interfaces.
1816
function isApplicationLoadBalancer(
1917
loadBalancer: INetworkLoadBalancer | IApplicationLoadBalancer
2018
): loadBalancer is IApplicationLoadBalancer {
21-
return loadBalancer.constructor.name.indexOf("Application") != -1;
19+
return !!(loadBalancer.metrics as any).httpRedirectCount;
2220
}
2321
function isNetworkLoadBalancer(
2422
loadBalancer: INetworkLoadBalancer | IApplicationLoadBalancer
2523
): loadBalancer is INetworkLoadBalancer {
26-
return loadBalancer.constructor.name.indexOf("Network") != -1;
24+
return !isApplicationLoadBalancer(loadBalancer);
2725
}
2826

2927
function isApplicationTargetGroup(
3028
targetGroup: INetworkTargetGroup | IApplicationTargetGroup
3129
): targetGroup is IApplicationTargetGroup {
32-
return targetGroup.constructor.name.indexOf("Application") != -1;
30+
return !!(targetGroup.metrics as any).httpCodeTarget;
3331
}
3432
function isNetworkTargetGroup(
3533
targetGroup: INetworkTargetGroup | IApplicationTargetGroup
3634
): targetGroup is INetworkTargetGroup {
37-
return targetGroup.constructor.name.indexOf("Network") != -1;
35+
return !isApplicationTargetGroup(targetGroup);
3836
}
3937

4038
/**

test/monitoring/aws-ecs-patterns/FargateServiceMonitoring.test.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {
1111
ApplicationLoadBalancedFargateService,
1212
NetworkLoadBalancedFargateService,
1313
} from "aws-cdk-lib/aws-ecs-patterns";
14-
import { NetworkLoadBalancer } from "aws-cdk-lib/aws-elasticloadbalancingv2";
14+
import {
15+
NetworkLoadBalancer,
16+
NetworkTargetGroup,
17+
} from "aws-cdk-lib/aws-elasticloadbalancingv2";
1518

1619
import { AlarmWithAnnotation, FargateServiceMonitoring } from "../../../lib";
1720
import { addMonitoringDashboardsToStack } from "../../utils/SnapshotUtil";
@@ -483,3 +486,46 @@ test("snapshot test: with imported service", () => {
483486
addMonitoringDashboardsToStack(stack, monitoring);
484487
expect(Template.fromStack(stack)).toMatchSnapshot();
485488
});
489+
490+
test("snapshot test: with imported NLB", () => {
491+
const stack = new Stack();
492+
493+
const scope = new TestMonitoringScope(stack, "Scope");
494+
495+
const cluster = new Cluster(stack, "Cluster");
496+
const image = new EcrImage(new Repository(stack, "Repository"), "DummyImage");
497+
const taskDefinition = new FargateTaskDefinition(stack, "TaskDef", {});
498+
499+
taskDefinition
500+
.addContainer("Container", { image })
501+
.addPortMappings({ containerPort: 8080 });
502+
503+
const fargateService = new FargateService(stack, "Service", {
504+
cluster,
505+
taskDefinition,
506+
});
507+
const networkLoadBalancer =
508+
NetworkLoadBalancer.fromNetworkLoadBalancerAttributes(stack, "NLB", {
509+
loadBalancerArn:
510+
"arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/net/LoadBalancer/123",
511+
});
512+
const networkTargetGroup = NetworkTargetGroup.fromTargetGroupAttributes(
513+
stack,
514+
"NTG",
515+
{
516+
targetGroupArn:
517+
"arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/TargetGroup/123",
518+
loadBalancerArns: networkLoadBalancer.loadBalancerArn,
519+
}
520+
);
521+
522+
const monitoring = new FargateServiceMonitoring(scope, {
523+
fargateService,
524+
loadBalancer: networkLoadBalancer,
525+
targetGroup: networkTargetGroup,
526+
alarmFriendlyName: "DummyFargateService",
527+
});
528+
529+
addMonitoringDashboardsToStack(stack, monitoring);
530+
expect(Template.fromStack(stack)).toMatchSnapshot();
531+
});

0 commit comments

Comments
 (0)