Skip to content

Commit

Permalink
fix(elasticloadbalancingv2): validate port/protocol are not provided …
Browse files Browse the repository at this point in the history
…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*
  • Loading branch information
corymhall committed Feb 21, 2022
1 parent 692a0d0 commit 64d26cc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Expand Up @@ -134,6 +134,17 @@ export class ApplicationTargetGroup extends TargetGroupBase implements IApplicat
this.protocol = protocol;
this.port = port;

// this.targetType is lazy
this.node.addValidation({
validate: () => {
if (this.targetType === TargetType.LAMBDA && (this.port || this.protocol)) {
return ['port/protocol should not be specified for Lambda targets'];
} else {
return [];
}
},
});

this.connectableMembers = [];
this.listeners = [];

Expand Down
Expand Up @@ -48,6 +48,35 @@ describe('tests', () => {
expect(Object.keys(matches).length).toBe(0);
});

test('Lambda target should not have port set', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'Stack');

const tg = new elbv2.ApplicationTargetGroup(stack, 'TG2', {
protocol: elbv2.ApplicationProtocol.HTTPS,
});
tg.addTarget({
attachToApplicationTargetGroup(_targetGroup: elbv2.IApplicationTargetGroup): elbv2.LoadBalancerTargetProps {
return {
targetType: elbv2.TargetType.LAMBDA,
targetJson: { id: 'arn:aws:lambda:eu-west-1:123456789012:function:myFn' },
};
},
});
expect(() => app.synth()).toThrow(/port\/protocol should not be specified for Lambda targets/);
});

test('Lambda target should not have protocol set', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'Stack');

new elbv2.ApplicationTargetGroup(stack, 'TG', {
port: 443,
targetType: elbv2.TargetType.LAMBDA,
});
expect(() => app.synth()).toThrow(/port\/protocol should not be specified for Lambda targets/);
});

test('Can add self-registering target to imported TargetGroup', () => {
// GIVEN
const app = new cdk.App();
Expand Down

0 comments on commit 64d26cc

Please sign in to comment.