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): validate port/protocol are not provided for lambda targets #19043

Merged
merged 2 commits into from Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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