Skip to content

Commit 16c23b7

Browse files
fix: Use the correct LB full name when creating metrics for imported LBs (#23972)
The `Arn.split()` method doesn't parse the `resourceName` correctly when it has multiple `/`, which is the case for the resources created by by the elbv2 API. I've also refactored the `integ.nlb-lookup.ts` test because it was not well written and I couldn't really deploy it with Cloudformation. The capability to create metrics from imported Load Balancers is new. It was introduced in #23853. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent a5317ca commit 16c23b7

9 files changed

+1337
-62
lines changed

packages/@aws-cdk/aws-elasticloadbalancingv2/lib/shared/util.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
2+
import { Arn, ArnFormat, Fn, Token } from '@aws-cdk/core';
23
import { ApplicationProtocol, Protocol } from './enums';
3-
import { Arn, ArnFormat } from '@aws-cdk/core';
44

55
export type Attributes = { [key: string]: string | undefined };
66

@@ -92,10 +92,21 @@ export function mapTagMapToCxschema(tagMap: Record<string, string>): cxschema.Ta
9292
.map(([key, value]) => ({ key, value }));
9393
}
9494

95-
export function parseLoadBalancerFullName(loadBalancerArn: string): string {
96-
const arnComponents = Arn.split(loadBalancerArn, ArnFormat.SLASH_RESOURCE_NAME);
97-
if (!arnComponents.resourceName) {
98-
throw new Error(`Provided ARN does not belong to a load balancer: ${loadBalancerArn}`);
95+
export function parseLoadBalancerFullName(arn: string): string {
96+
if (Token.isUnresolved(arn)) {
97+
// Unfortunately it is not possible to use Arn.split() because the ARNs have this shape:
98+
//
99+
// arn:...:loadbalancer/net/my-load-balancer/123456
100+
//
101+
// And the way that Arn.split() handles this situation is not enough to obtain the full name
102+
const arnParts = Fn.split('/', arn);
103+
return `${Fn.select(1, arnParts)}/${Fn.select(2, arnParts)}/${Fn.select(3, arnParts)}`;
104+
} else {
105+
const arnComponents = Arn.split(arn, ArnFormat.SLASH_RESOURCE_NAME);
106+
const resourceName = arnComponents.resourceName;
107+
if (!resourceName) {
108+
throw new Error(`Provided ARN does not belong to a load balancer: ${arn}`);
109+
}
110+
return resourceName;
99111
}
100-
return arnComponents.resourceName;
101112
}

packages/@aws-cdk/aws-elasticloadbalancingv2/test/integ.nlb-lookup.js.snapshot/aws-cdk-elbv2-StackWithLb.assets.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
22
"version": "29.0.0",
33
"files": {
4-
"b2b2e615554259736dccb6ecc100edae2dc9d18e7d4b2103b6b7ebacebba8485": {
4+
"e24b7b4b9bebbe70e470dbe2c83f8f69c8338d37b258b8ebec384b51fd61536d": {
55
"source": {
66
"path": "aws-cdk-elbv2-StackWithLb.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
10-
"123456-eu-west-1": {
11-
"bucketName": "cdk-hnb659fds-assets-123456-eu-west-1",
12-
"objectKey": "b2b2e615554259736dccb6ecc100edae2dc9d18e7d4b2103b6b7ebacebba8485.json",
13-
"region": "eu-west-1",
14-
"assumeRoleArn": "arn:${AWS::Partition}:iam::123456:role/cdk-hnb659fds-file-publishing-role-123456-eu-west-1"
10+
"12345678-test-region": {
11+
"bucketName": "cdk-hnb659fds-assets-12345678-test-region",
12+
"objectKey": "e24b7b4b9bebbe70e470dbe2c83f8f69c8338d37b258b8ebec384b51fd61536d.json",
13+
"region": "test-region",
14+
"assumeRoleArn": "arn:${AWS::Partition}:iam::12345678:role/cdk-hnb659fds-file-publishing-role-12345678-test-region"
1515
}
1616
}
1717
}

packages/@aws-cdk/aws-elasticloadbalancingv2/test/integ.nlb-lookup.js.snapshot/aws-cdk-elbv2-StackWithLb.template.json

+22-10
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"VpcId": {
2222
"Ref": "VPCB9E5F0B4"
2323
},
24-
"AvailabilityZone": "dummy1a",
24+
"AvailabilityZone": "test-region-1a",
2525
"CidrBlock": "10.0.0.0/18",
2626
"MapPublicIpOnLaunch": true,
2727
"Tags": [
@@ -122,7 +122,7 @@
122122
"VpcId": {
123123
"Ref": "VPCB9E5F0B4"
124124
},
125-
"AvailabilityZone": "dummy1b",
125+
"AvailabilityZone": "test-region-1b",
126126
"CidrBlock": "10.0.64.0/18",
127127
"MapPublicIpOnLaunch": true,
128128
"Tags": [
@@ -223,7 +223,7 @@
223223
"VpcId": {
224224
"Ref": "VPCB9E5F0B4"
225225
},
226-
"AvailabilityZone": "dummy1a",
226+
"AvailabilityZone": "test-region-1a",
227227
"CidrBlock": "10.0.128.0/18",
228228
"MapPublicIpOnLaunch": false,
229229
"Tags": [
@@ -285,7 +285,7 @@
285285
"VpcId": {
286286
"Ref": "VPCB9E5F0B4"
287287
},
288-
"AvailabilityZone": "dummy1b",
288+
"AvailabilityZone": "test-region-1b",
289289
"CidrBlock": "10.0.192.0/18",
290290
"MapPublicIpOnLaunch": false,
291291
"Tags": [
@@ -382,12 +382,6 @@
382382
"Ref": "VPCPublicSubnet2Subnet74179F39"
383383
}
384384
],
385-
"Tags": [
386-
{
387-
"Key": "some",
388-
"Value": "tag"
389-
}
390-
],
391385
"Type": "network"
392386
},
393387
"DependsOn": [
@@ -398,6 +392,24 @@
398392
]
399393
}
400394
},
395+
"Outputs": {
396+
"NlbArn": {
397+
"Value": {
398+
"Ref": "LB8A12904C"
399+
},
400+
"Export": {
401+
"Name": "NlbArn"
402+
}
403+
},
404+
"ExportsOutputRefLB8A12904C1150D6A6": {
405+
"Value": {
406+
"Ref": "LB8A12904C"
407+
},
408+
"Export": {
409+
"Name": "aws-cdk-elbv2-StackWithLb:ExportsOutputRefLB8A12904C1150D6A6"
410+
}
411+
}
412+
},
401413
"Parameters": {
402414
"BootstrapVersion": {
403415
"Type": "AWS::SSM::Parameter::Value<String>",

packages/@aws-cdk/aws-elasticloadbalancingv2/test/integ.nlb-lookup.js.snapshot/aws-cdk-elbv2-integ-StackUnderTest.assets.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
22
"version": "29.0.0",
33
"files": {
4-
"c90244dbab9ab3bd198b9233fcf42c068fad41afc3efb1b1a1b12d352b81970d": {
4+
"e42d9c4a114328c16cb773781b2fee3cceeb499294ef3cb4f7a0aeafce947f13": {
55
"source": {
66
"path": "aws-cdk-elbv2-integ-StackUnderTest.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
10-
"123456-eu-west-1": {
11-
"bucketName": "cdk-hnb659fds-assets-123456-eu-west-1",
12-
"objectKey": "c90244dbab9ab3bd198b9233fcf42c068fad41afc3efb1b1a1b12d352b81970d.json",
13-
"region": "eu-west-1",
14-
"assumeRoleArn": "arn:${AWS::Partition}:iam::123456:role/cdk-hnb659fds-file-publishing-role-123456-eu-west-1"
10+
"12345678-test-region": {
11+
"bucketName": "cdk-hnb659fds-assets-12345678-test-region",
12+
"objectKey": "e42d9c4a114328c16cb773781b2fee3cceeb499294ef3cb4f7a0aeafce947f13.json",
13+
"region": "test-region",
14+
"assumeRoleArn": "arn:${AWS::Partition}:iam::12345678:role/cdk-hnb659fds-file-publishing-role-12345678-test-region"
1515
}
1616
}
1717
}

packages/@aws-cdk/aws-elasticloadbalancingv2/test/integ.nlb-lookup.js.snapshot/aws-cdk-elbv2-integ-StackUnderTest.template.json

+131-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"Resources": {
3-
"NlbByAttributesAlarmFlowCountB9EE6965": {
3+
"NlbByHardcodedArnAlarmFlowCount60A46641": {
44
"Type": "AWS::CloudWatch::Alarm",
55
"Properties": {
66
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
@@ -17,6 +17,136 @@
1717
"Statistic": "Average",
1818
"Threshold": 0
1919
}
20+
},
21+
"NlbByCfnOutputsFromAnotherStackOutsideCdkAlarmFlowCountD9A1D5AC": {
22+
"Type": "AWS::CloudWatch::Alarm",
23+
"Properties": {
24+
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
25+
"EvaluationPeriods": 1,
26+
"Dimensions": [
27+
{
28+
"Name": "LoadBalancer",
29+
"Value": {
30+
"Fn::Join": [
31+
"",
32+
[
33+
{
34+
"Fn::Select": [
35+
1,
36+
{
37+
"Fn::Split": [
38+
"/",
39+
{
40+
"Fn::ImportValue": "NlbArn"
41+
}
42+
]
43+
}
44+
]
45+
},
46+
"/",
47+
{
48+
"Fn::Select": [
49+
2,
50+
{
51+
"Fn::Split": [
52+
"/",
53+
{
54+
"Fn::ImportValue": "NlbArn"
55+
}
56+
]
57+
}
58+
]
59+
},
60+
"/",
61+
{
62+
"Fn::Select": [
63+
3,
64+
{
65+
"Fn::Split": [
66+
"/",
67+
{
68+
"Fn::ImportValue": "NlbArn"
69+
}
70+
]
71+
}
72+
]
73+
}
74+
]
75+
]
76+
}
77+
}
78+
],
79+
"MetricName": "ActiveFlowCount",
80+
"Namespace": "AWS/NetworkELB",
81+
"Period": 300,
82+
"Statistic": "Average",
83+
"Threshold": 0
84+
}
85+
},
86+
"NlbByCfnOutputsFromAnotherStackWithinCdkAlarmFlowCountD865DB84": {
87+
"Type": "AWS::CloudWatch::Alarm",
88+
"Properties": {
89+
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
90+
"EvaluationPeriods": 1,
91+
"Dimensions": [
92+
{
93+
"Name": "LoadBalancer",
94+
"Value": {
95+
"Fn::Join": [
96+
"",
97+
[
98+
{
99+
"Fn::Select": [
100+
1,
101+
{
102+
"Fn::Split": [
103+
"/",
104+
{
105+
"Fn::ImportValue": "aws-cdk-elbv2-StackWithLb:ExportsOutputRefLB8A12904C1150D6A6"
106+
}
107+
]
108+
}
109+
]
110+
},
111+
"/",
112+
{
113+
"Fn::Select": [
114+
2,
115+
{
116+
"Fn::Split": [
117+
"/",
118+
{
119+
"Fn::ImportValue": "aws-cdk-elbv2-StackWithLb:ExportsOutputRefLB8A12904C1150D6A6"
120+
}
121+
]
122+
}
123+
]
124+
},
125+
"/",
126+
{
127+
"Fn::Select": [
128+
3,
129+
{
130+
"Fn::Split": [
131+
"/",
132+
{
133+
"Fn::ImportValue": "aws-cdk-elbv2-StackWithLb:ExportsOutputRefLB8A12904C1150D6A6"
134+
}
135+
]
136+
}
137+
]
138+
}
139+
]
140+
]
141+
}
142+
}
143+
],
144+
"MetricName": "ActiveFlowCount",
145+
"Namespace": "AWS/NetworkELB",
146+
"Period": 300,
147+
"Statistic": "Average",
148+
"Threshold": 0
149+
}
20150
}
21151
},
22152
"Parameters": {

packages/@aws-cdk/aws-elasticloadbalancingv2/test/integ.nlb-lookup.js.snapshot/integ.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"enableLookups": true,
23
"version": "29.0.0",
34
"testCases": {
45
"elbv2-integ/DefaultTest": {
@@ -8,8 +9,8 @@
89
},
910
"aws-cdk-elbv2-integ-StackUnderTest/aws-cdk-elbv2-integ-StackUnderTestTestCase": {
1011
"env": {
11-
"account": "123456",
12-
"region": "eu-west-1"
12+
"account": "12345678",
13+
"region": "test-region"
1314
},
1415
"stacks": [
1516
"aws-cdk-elbv2-integ-StackUnderTest"

0 commit comments

Comments
 (0)