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

elbv2: ApplicationTargetGroup should validate that port is specified (when required) #4628

Closed
fogfish opened this issue Oct 22, 2019 · 8 comments
Labels
@aws-cdk/aws-elasticloadbalancing Related to Amazon Elastic Load Balancing effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. good first issue Related to contributions. See CONTRIBUTING.md p2

Comments

@fogfish
Copy link
Contributor

fogfish commented Oct 22, 2019

ApplicationTargetGroupProps defines all properties optional. However, it fails at runtime if port property is not defined.

Reproduction Steps

import * as alb from '@aws-cdk/aws-elasticloadbalancingv2'

new alb.ApplicationTargetGroup(this, 'DefaultTarget', {});

Error Log

A port must be specified (Service: AmazonElasticLoadBalancingV2; Status Code: 400; Error Code: ValidationError; Request ID: xxx)

Environment

  • CLI Version :1.13.1 (build 96cfc63)
  • Framework Version:1.13.1 (build 96cfc63)
  • OS :MacOS
  • Language :TypeScript

This is 🐛 Bug Report

@fogfish fogfish added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Oct 22, 2019
@nmussy
Copy link
Contributor

nmussy commented Oct 22, 2019

Hello @fogfish,

The port parameter not being required is due to the following, as described in the docs:

If the target is a Lambda function, this parameter does not apply.

Same with Protocol and VpcId.

We could add some client-side checks (with exceptions being thrown), but I'm not sure it's possible to do it with types given the current API.

@fogfish
Copy link
Contributor Author

fogfish commented Oct 22, 2019

Yes, this is definitely make sense. Thank you for hints! May be similar statement shall be applied to CDK docs
https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-elasticloadbalancingv2.ApplicationTargetGroup.html

Feel free to close the issue!

One side comment. I've notice that similar issue is reflected to other Props. I know it would not be closed as part of this bug report. However, have you considered to declare a narrow Props that concern a particular use-case. It would make API a typesafe good for developers but of course it would require maintenance from your side. In typescript, this is called 'Discriminated Unions'.

@SomayaB SomayaB added the @aws-cdk/aws-elasticloadbalancing Related to Amazon Elastic Load Balancing label Oct 22, 2019
@SomayaB SomayaB removed the needs-triage This issue or PR still needs to be triaged. label Oct 22, 2019
@rix0rrr
Copy link
Contributor

rix0rrr commented Oct 23, 2019

Unfortunately we're not currently allowing type unions, since not all languages we're targeting with jsii support them natively and we haven't narrowed down a way to represent them in those languages.

We can still keep this issue open, even if there is no static checking, there should be runtime validation in the construct so you don't need to wait for a CloudFormation deployment to detect this situation.

@rix0rrr rix0rrr changed the title Loosely type definition for ApplicationTargetGroupProps elbv2: ApplicationTargetGroup should validate that port is specified (when required) Oct 23, 2019
@rix0rrr rix0rrr added feature-request A feature should be added or improved. good first issue Related to contributions. See CONTRIBUTING.md and removed bug This issue is a bug. labels Oct 23, 2019
@fogfish
Copy link
Contributor Author

fogfish commented Oct 24, 2019

Oh... True... TypeScript is not a central language for AWS CDK. I'll wish it will be the only one ;-)

Thank you for feedback!

@nmussy
Copy link
Contributor

nmussy commented Oct 25, 2019

I can take the PR if no one wants it 👍

@rix0rrr rix0rrr added the effort/small Small work item – less than a day of effort label Jan 23, 2020
@rix0rrr rix0rrr added the p2 label Aug 12, 2020
@kaizencc
Copy link
Contributor

I took a look into this, and it looks like the validation method is already there, introduced in #3348:

protected validate(): string[] {
const ret = super.validate();
if (this.targetType !== undefined && this.targetType !== TargetType.LAMBDA
&& (this.protocol === undefined || this.port === undefined)) {
ret.push('At least one of \'port\' or \'protocol\' is required for a non-Lambda TargetGroup');
}
if (this.healthCheck && this.healthCheck.protocol && !ALB_HEALTH_CHECK_PROTOCOLS.includes(this.healthCheck.protocol)) {
ret.push([
`Health check protocol '${this.healthCheck.protocol}' is not supported. `,
`Must be one of [${ALB_HEALTH_CHECK_PROTOCOLS.join(', ')}]`,
].join(''));
}
return ret;
}
}

Since targetType is not required by the resource, I don't think we can do any additional validation if targetType is undefined, which is why this becomes an error only when deployed.

Outside of the fact that I'm pretty sure the error message ought to read "Both port and protocol is required" based on the logic, I don't think there's much to do here.

@rix0rrr perhaps you can double check but I think this can just be closed.

@SomayaB SomayaB assigned njlynch and unassigned rix0rrr Aug 20, 2020
@madeline-k
Copy link
Contributor

Please re-open if you are experiencing this issue

@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-elasticloadbalancing Related to Amazon Elastic Load Balancing effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. good first issue Related to contributions. See CONTRIBUTING.md p2
Projects
None yet
Development

No branches or pull requests

7 participants