From b048123656834ccafa44322e4db52f8e7cdeaf8f Mon Sep 17 00:00:00 2001 From: Marcio Almeida Date: Tue, 6 Sep 2022 21:46:25 -0300 Subject: [PATCH 1/3] fix(aws-elasticloadbalancingv2): The securityGroup property is not optional --- .../aws-elasticloadbalancingv2/lib/alb/application-listener.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-listener.ts b/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-listener.ts index adb2c6f580c98..538142c0cbeb3 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-listener.ts +++ b/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-listener.ts @@ -566,7 +566,7 @@ export interface ApplicationListenerAttributes { /** * Security group of the load balancer this listener is associated with */ - readonly securityGroup?: ec2.ISecurityGroup; + readonly securityGroup: ec2.ISecurityGroup; /** * The default port on which this listener is listening From 56d09a7ce12e384c738cff90f1a6128e05abfddd Mon Sep 17 00:00:00 2001 From: Marcio Almeida Date: Tue, 6 Sep 2022 22:39:32 -0300 Subject: [PATCH 2/3] fix: remove securityGroupId deprecated property from application-listener --- .../lib/alb/application-listener.ts | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-listener.ts b/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-listener.ts index 538142c0cbeb3..7035dfe4768ce 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-listener.ts +++ b/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-listener.ts @@ -556,13 +556,6 @@ export interface ApplicationListenerAttributes { */ readonly listenerArn: string; - /** - * Security group ID of the load balancer this listener is associated with - * - * @deprecated use `securityGroup` instead - */ - readonly securityGroupId?: string; - /** * Security group of the load balancer this listener is associated with */ @@ -712,12 +705,8 @@ class ImportedApplicationListener extends ExternalApplicationListener { let securityGroup: ec2.ISecurityGroup; if (props.securityGroup) { securityGroup = props.securityGroup; - } else if (props.securityGroupId) { - securityGroup = ec2.SecurityGroup.fromSecurityGroupId(this, 'SecurityGroup', props.securityGroupId, { - allowAllOutbound: props.securityGroupAllowsAllOutbound, - }); } else { - throw new Error('Either `securityGroup` or `securityGroupId` must be specified to import an application listener.'); + throw new Error('The `securityGroup` must be specified to import an application listener.'); } this.connections = new ec2.Connections({ From 6ab322a1c905355402551c8e1098716c0916b69c Mon Sep 17 00:00:00 2001 From: Marcio Almeida Date: Wed, 7 Sep 2022 17:56:35 -0300 Subject: [PATCH 3/3] fix: add tests --- .../lib/alb/application-listener.ts | 9 +----- .../test/alb/listener.test.ts | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-listener.ts b/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-listener.ts index 7035dfe4768ce..8242e28ec64ab 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-listener.ts +++ b/packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-listener.ts @@ -702,15 +702,8 @@ class ImportedApplicationListener extends ExternalApplicationListener { this.listenerArn = props.listenerArn; const defaultPort = props.defaultPort !== undefined ? ec2.Port.tcp(props.defaultPort) : undefined; - let securityGroup: ec2.ISecurityGroup; - if (props.securityGroup) { - securityGroup = props.securityGroup; - } else { - throw new Error('The `securityGroup` must be specified to import an application listener.'); - } - this.connections = new ec2.Connections({ - securityGroups: [securityGroup], + securityGroups: [props.securityGroup], defaultPort, }); } diff --git a/packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/listener.test.ts b/packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/listener.test.ts index d4095c93c5525..e3f2db7be4de1 100644 --- a/packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/listener.test.ts +++ b/packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/listener.test.ts @@ -707,6 +707,37 @@ describe('tests', () => { }); }); + test('imported listener only need securityGroup and listenerArn as attributes', () => { + // GIVEN + const stack = new cdk.Stack(); + + const importedListener = elbv2.ApplicationListener.fromApplicationListenerAttributes(stack, 'listener', { + listenerArn: 'listener-arn', + defaultPort: 443, + securityGroup: ec2.SecurityGroup.fromSecurityGroupId(stack, 'SG', 'security-group-id', { + allowAllOutbound: false, + }), + }); + importedListener.addAction('Hello', { + action: elbv2.ListenerAction.fixedResponse(503), + conditions: [elbv2.ListenerCondition.pathPatterns(['/hello'])], + priority: 10, + }); + + Template.fromStack(stack).hasResourceProperties('AWS::ElasticLoadBalancingV2::ListenerRule', { + ListenerArn: 'listener-arn', + Priority: 10, + Actions: [ + { + FixedResponseConfig: { + StatusCode: '503', + }, + Type: 'fixed-response', + }, + ], + }); + }); + test('Can add actions to an imported listener', () => { // GIVEN const stack = new cdk.Stack();