Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(elasticloadbalancingv2): securityGroup property is not required in fromApplicationListenerAttributes #21934

Merged
merged 10 commits into from
Sep 11, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -556,17 +556,10 @@ 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
*/
readonly securityGroup?: ec2.ISecurityGroup;
readonly securityGroup: ec2.ISecurityGroup;

/**
* The default port on which this listener is listening
Expand Down Expand Up @@ -709,19 +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 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.');
}

this.connections = new ec2.Connections({
securityGroups: [securityGroup],
securityGroups: [props.securityGroup],
defaultPort,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,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();
Expand Down