Skip to content

Commit

Permalink
fix: Use the correct LB full name when creating metrics for imported …
Browse files Browse the repository at this point in the history
…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*
  • Loading branch information
edisongustavo committed Feb 8, 2023
1 parent a5317ca commit 16c23b7
Show file tree
Hide file tree
Showing 9 changed files with 1,337 additions and 62 deletions.
23 changes: 17 additions & 6 deletions packages/@aws-cdk/aws-elasticloadbalancingv2/lib/shared/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import { Arn, ArnFormat, Fn, Token } from '@aws-cdk/core';
import { ApplicationProtocol, Protocol } from './enums';
import { Arn, ArnFormat } from '@aws-cdk/core';

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

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

export function parseLoadBalancerFullName(loadBalancerArn: string): string {
const arnComponents = Arn.split(loadBalancerArn, ArnFormat.SLASH_RESOURCE_NAME);
if (!arnComponents.resourceName) {
throw new Error(`Provided ARN does not belong to a load balancer: ${loadBalancerArn}`);
export function parseLoadBalancerFullName(arn: string): string {
if (Token.isUnresolved(arn)) {
// Unfortunately it is not possible to use Arn.split() because the ARNs have this shape:
//
// arn:...:loadbalancer/net/my-load-balancer/123456
//
// And the way that Arn.split() handles this situation is not enough to obtain the full name
const arnParts = Fn.split('/', arn);
return `${Fn.select(1, arnParts)}/${Fn.select(2, arnParts)}/${Fn.select(3, arnParts)}`;
} else {
const arnComponents = Arn.split(arn, ArnFormat.SLASH_RESOURCE_NAME);
const resourceName = arnComponents.resourceName;
if (!resourceName) {
throw new Error(`Provided ARN does not belong to a load balancer: ${arn}`);
}
return resourceName;
}
return arnComponents.resourceName;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"version": "29.0.0",
"files": {
"b2b2e615554259736dccb6ecc100edae2dc9d18e7d4b2103b6b7ebacebba8485": {
"e24b7b4b9bebbe70e470dbe2c83f8f69c8338d37b258b8ebec384b51fd61536d": {
"source": {
"path": "aws-cdk-elbv2-StackWithLb.template.json",
"packaging": "file"
},
"destinations": {
"123456-eu-west-1": {
"bucketName": "cdk-hnb659fds-assets-123456-eu-west-1",
"objectKey": "b2b2e615554259736dccb6ecc100edae2dc9d18e7d4b2103b6b7ebacebba8485.json",
"region": "eu-west-1",
"assumeRoleArn": "arn:${AWS::Partition}:iam::123456:role/cdk-hnb659fds-file-publishing-role-123456-eu-west-1"
"12345678-test-region": {
"bucketName": "cdk-hnb659fds-assets-12345678-test-region",
"objectKey": "e24b7b4b9bebbe70e470dbe2c83f8f69c8338d37b258b8ebec384b51fd61536d.json",
"region": "test-region",
"assumeRoleArn": "arn:${AWS::Partition}:iam::12345678:role/cdk-hnb659fds-file-publishing-role-12345678-test-region"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"VpcId": {
"Ref": "VPCB9E5F0B4"
},
"AvailabilityZone": "dummy1a",
"AvailabilityZone": "test-region-1a",
"CidrBlock": "10.0.0.0/18",
"MapPublicIpOnLaunch": true,
"Tags": [
Expand Down Expand Up @@ -122,7 +122,7 @@
"VpcId": {
"Ref": "VPCB9E5F0B4"
},
"AvailabilityZone": "dummy1b",
"AvailabilityZone": "test-region-1b",
"CidrBlock": "10.0.64.0/18",
"MapPublicIpOnLaunch": true,
"Tags": [
Expand Down Expand Up @@ -223,7 +223,7 @@
"VpcId": {
"Ref": "VPCB9E5F0B4"
},
"AvailabilityZone": "dummy1a",
"AvailabilityZone": "test-region-1a",
"CidrBlock": "10.0.128.0/18",
"MapPublicIpOnLaunch": false,
"Tags": [
Expand Down Expand Up @@ -285,7 +285,7 @@
"VpcId": {
"Ref": "VPCB9E5F0B4"
},
"AvailabilityZone": "dummy1b",
"AvailabilityZone": "test-region-1b",
"CidrBlock": "10.0.192.0/18",
"MapPublicIpOnLaunch": false,
"Tags": [
Expand Down Expand Up @@ -382,12 +382,6 @@
"Ref": "VPCPublicSubnet2Subnet74179F39"
}
],
"Tags": [
{
"Key": "some",
"Value": "tag"
}
],
"Type": "network"
},
"DependsOn": [
Expand All @@ -398,6 +392,24 @@
]
}
},
"Outputs": {
"NlbArn": {
"Value": {
"Ref": "LB8A12904C"
},
"Export": {
"Name": "NlbArn"
}
},
"ExportsOutputRefLB8A12904C1150D6A6": {
"Value": {
"Ref": "LB8A12904C"
},
"Export": {
"Name": "aws-cdk-elbv2-StackWithLb:ExportsOutputRefLB8A12904C1150D6A6"
}
}
},
"Parameters": {
"BootstrapVersion": {
"Type": "AWS::SSM::Parameter::Value<String>",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"version": "29.0.0",
"files": {
"c90244dbab9ab3bd198b9233fcf42c068fad41afc3efb1b1a1b12d352b81970d": {
"e42d9c4a114328c16cb773781b2fee3cceeb499294ef3cb4f7a0aeafce947f13": {
"source": {
"path": "aws-cdk-elbv2-integ-StackUnderTest.template.json",
"packaging": "file"
},
"destinations": {
"123456-eu-west-1": {
"bucketName": "cdk-hnb659fds-assets-123456-eu-west-1",
"objectKey": "c90244dbab9ab3bd198b9233fcf42c068fad41afc3efb1b1a1b12d352b81970d.json",
"region": "eu-west-1",
"assumeRoleArn": "arn:${AWS::Partition}:iam::123456:role/cdk-hnb659fds-file-publishing-role-123456-eu-west-1"
"12345678-test-region": {
"bucketName": "cdk-hnb659fds-assets-12345678-test-region",
"objectKey": "e42d9c4a114328c16cb773781b2fee3cceeb499294ef3cb4f7a0aeafce947f13.json",
"region": "test-region",
"assumeRoleArn": "arn:${AWS::Partition}:iam::12345678:role/cdk-hnb659fds-file-publishing-role-12345678-test-region"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Resources": {
"NlbByAttributesAlarmFlowCountB9EE6965": {
"NlbByHardcodedArnAlarmFlowCount60A46641": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
Expand All @@ -17,6 +17,136 @@
"Statistic": "Average",
"Threshold": 0
}
},
"NlbByCfnOutputsFromAnotherStackOutsideCdkAlarmFlowCountD9A1D5AC": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"EvaluationPeriods": 1,
"Dimensions": [
{
"Name": "LoadBalancer",
"Value": {
"Fn::Join": [
"",
[
{
"Fn::Select": [
1,
{
"Fn::Split": [
"/",
{
"Fn::ImportValue": "NlbArn"
}
]
}
]
},
"/",
{
"Fn::Select": [
2,
{
"Fn::Split": [
"/",
{
"Fn::ImportValue": "NlbArn"
}
]
}
]
},
"/",
{
"Fn::Select": [
3,
{
"Fn::Split": [
"/",
{
"Fn::ImportValue": "NlbArn"
}
]
}
]
}
]
]
}
}
],
"MetricName": "ActiveFlowCount",
"Namespace": "AWS/NetworkELB",
"Period": 300,
"Statistic": "Average",
"Threshold": 0
}
},
"NlbByCfnOutputsFromAnotherStackWithinCdkAlarmFlowCountD865DB84": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"EvaluationPeriods": 1,
"Dimensions": [
{
"Name": "LoadBalancer",
"Value": {
"Fn::Join": [
"",
[
{
"Fn::Select": [
1,
{
"Fn::Split": [
"/",
{
"Fn::ImportValue": "aws-cdk-elbv2-StackWithLb:ExportsOutputRefLB8A12904C1150D6A6"
}
]
}
]
},
"/",
{
"Fn::Select": [
2,
{
"Fn::Split": [
"/",
{
"Fn::ImportValue": "aws-cdk-elbv2-StackWithLb:ExportsOutputRefLB8A12904C1150D6A6"
}
]
}
]
},
"/",
{
"Fn::Select": [
3,
{
"Fn::Split": [
"/",
{
"Fn::ImportValue": "aws-cdk-elbv2-StackWithLb:ExportsOutputRefLB8A12904C1150D6A6"
}
]
}
]
}
]
]
}
}
],
"MetricName": "ActiveFlowCount",
"Namespace": "AWS/NetworkELB",
"Period": 300,
"Statistic": "Average",
"Threshold": 0
}
}
},
"Parameters": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"enableLookups": true,
"version": "29.0.0",
"testCases": {
"elbv2-integ/DefaultTest": {
Expand All @@ -8,8 +9,8 @@
},
"aws-cdk-elbv2-integ-StackUnderTest/aws-cdk-elbv2-integ-StackUnderTestTestCase": {
"env": {
"account": "123456",
"region": "eu-west-1"
"account": "12345678",
"region": "test-region"
},
"stacks": [
"aws-cdk-elbv2-integ-StackUnderTest"
Expand Down
Loading

0 comments on commit 16c23b7

Please sign in to comment.