Skip to content

Commit 64d26cc

Browse files
authored
fix(elasticloadbalancingv2): validate port/protocol are not provided for lambda targets (#19043)
When creating a target group with the targetType = `LAMBDA` you should not provide the port or protocol. If protocol is provided then CloudFormation will throw an error message. If you provide the port to CDK, CDK will figure out and provide the protocol as well. This PR adds validation and throws an error if either port or protocol is provided when the target type is Lambda. fixes #12514 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 692a0d0 commit 64d26cc

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-target-group.ts

+11
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,17 @@ export class ApplicationTargetGroup extends TargetGroupBase implements IApplicat
134134
this.protocol = protocol;
135135
this.port = port;
136136

137+
// this.targetType is lazy
138+
this.node.addValidation({
139+
validate: () => {
140+
if (this.targetType === TargetType.LAMBDA && (this.port || this.protocol)) {
141+
return ['port/protocol should not be specified for Lambda targets'];
142+
} else {
143+
return [];
144+
}
145+
},
146+
});
147+
137148
this.connectableMembers = [];
138149
this.listeners = [];
139150

packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/target-group.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,35 @@ describe('tests', () => {
4848
expect(Object.keys(matches).length).toBe(0);
4949
});
5050

51+
test('Lambda target should not have port set', () => {
52+
const app = new cdk.App();
53+
const stack = new cdk.Stack(app, 'Stack');
54+
55+
const tg = new elbv2.ApplicationTargetGroup(stack, 'TG2', {
56+
protocol: elbv2.ApplicationProtocol.HTTPS,
57+
});
58+
tg.addTarget({
59+
attachToApplicationTargetGroup(_targetGroup: elbv2.IApplicationTargetGroup): elbv2.LoadBalancerTargetProps {
60+
return {
61+
targetType: elbv2.TargetType.LAMBDA,
62+
targetJson: { id: 'arn:aws:lambda:eu-west-1:123456789012:function:myFn' },
63+
};
64+
},
65+
});
66+
expect(() => app.synth()).toThrow(/port\/protocol should not be specified for Lambda targets/);
67+
});
68+
69+
test('Lambda target should not have protocol set', () => {
70+
const app = new cdk.App();
71+
const stack = new cdk.Stack(app, 'Stack');
72+
73+
new elbv2.ApplicationTargetGroup(stack, 'TG', {
74+
port: 443,
75+
targetType: elbv2.TargetType.LAMBDA,
76+
});
77+
expect(() => app.synth()).toThrow(/port\/protocol should not be specified for Lambda targets/);
78+
});
79+
5180
test('Can add self-registering target to imported TargetGroup', () => {
5281
// GIVEN
5382
const app = new cdk.App();

0 commit comments

Comments
 (0)