-
Notifications
You must be signed in to change notification settings - Fork 4k
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(ecs-patterns): allow imported load balancers as inputs #5461
Conversation
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
0ad5580
to
74d8bcc
Compare
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
74d8bcc
to
c21a848
Compare
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service.ts
Show resolved
Hide resolved
packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service.ts
Show resolved
Hide resolved
c21a848
to
2e576f4
Compare
Pull request has been modified.
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
Pull request has been modified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is awesome! Ship it!
@rix0rrr please take a look at the imported load balancer construct changes
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
Pull request has been modified.
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ship it!
Pull request has been modified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ship it!
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
@@ -297,12 +300,14 @@ export abstract class ApplicationLoadBalancedServiceBase extends cdk.Construct { | |||
internetFacing | |||
}; | |||
|
|||
this.loadBalancer = props.loadBalancer !== undefined ? props.loadBalancer : new ApplicationLoadBalancer(this, 'LB', lbProps); | |||
this.loadBalancer = props.loadBalancer !== undefined ? props.loadBalancer as ApplicationLoadBalancer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, this is dangerous. Why is the input argument changed to IApplicationLoadBalancer
, but it's downcast to a concrete class here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To completely explain: this cast is not necessarily correct. For example, for an imported load balancer this cast is DEFINITELY NOT correct, as an ApplicationLoadBalancer
is going to have methods that an ImportedApplicationLoadBalancer
does not have, and if you try to use those you will get crashes.
If the problem is that this.loadBalancer
has been typed as an ApplicationLoadBalancer
and the jsii-diff
tool won't let you change it to IApplicationLoadBalancer
for fear of breaking backwards compatibility, and so you must trick the type system, this is not the way to do it! A way to "properly" handle this case while not breaking backwards compatibility would be something like this:
class {
private readonly _applicationLoadBalancer?: ApplicationLoadBalancer;
constructor() {
if (props.loadBalancer instanceof ApplicationLoadBalancer) {
this._applicationLoadBalancer = props.loadBalancer;
}
}
public get applicationLoadBalancer(): ApplicationLoadBalancer {
if (!this._applicationLoadBalancer) {
throw new Error('.applicationLoadBalancer can only accessed if the class was constructed with an owned load balancer, not an imported one');
}
return this._applicationLoadBalancer;
}
}
That is, we still promise that the return type of applicationLoadBalancer
will be a proper ApplicationLoadBalancer
, but we just don't make that guarantee in 100% of the cases anymore.
Not ideal, but the best we can do given the contracts we uphold.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the detailed feedback, this was really helpful in understanding the guards we put against backwards compatibility.
|
||
if (props.certificate !== undefined && props.protocol !== undefined && props.protocol !== ApplicationProtocol.HTTPS) { | ||
throw new Error('The HTTPS protocol must be used when a certificate is given'); | ||
} | ||
const protocol = props.protocol !== undefined ? props.protocol : (props.certificate ? ApplicationProtocol.HTTPS : ApplicationProtocol.HTTP); | ||
const protocol = props.protocol !== undefined ? props.protocol : | ||
(props.certificate ? ApplicationProtocol.HTTPS : ApplicationProtocol.HTTP); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation please.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved in 57cd88a
@@ -268,7 +270,7 @@ export abstract class NetworkLoadBalancedServiceBase extends cdk.Construct { | |||
internetFacing | |||
}; | |||
|
|||
this.loadBalancer = props.loadBalancer !== undefined ? props.loadBalancer : new NetworkLoadBalancer(this, 'LB', lbProps); | |||
this.loadBalancer = props.loadBalancer !== undefined ? props.loadBalancer as NetworkLoadBalancer : new NetworkLoadBalancer(this, 'LB', lbProps); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment.
* @default - When not provided, listeners cannot be created on imported load | ||
* balancers. | ||
*/ | ||
readonly loadBalancerVpc?: ec2.IVpc; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the application load balancer this prop is called vpc
, why is it called loadBalancerVpc
here? Please make both the same. vpc
will suffice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved in 57cd88a
Pull request has been modified.
Pull request has been modified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work @bvtujo!
Thank you for contributing! Your pull request is now being automatically merged. |
This PR adds functionality in aws-ecs-patterns to allow imported Network and Application load balancers to be used when initializing the LoadBalancedEC2Service and LoadBalancedFargateService constructs. By necessity, this means:
Unit tests are added in aws-ecs-patterns to test the new import functionality and in aws-elasticloadbalancingv2 to ensure that addTargets behaves properly when a Vpc is or is not specified for imported load balancers.
Resolves: #5209
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
Commit Message
fix(ecs-patterns): allow imported load balancers as inputs
This PR adds functionality in aws-ecs-patterns to allow imported Network and Application load balancers to be used when initializing the LoadBalancedEC2Service and LoadBalancedFargateService constructs. By necessity, this means:
Unit tests are added in aws-ecs-patterns to test the new import functionality and in aws-elasticloadbalancingv2 to ensure that addTargets behaves properly when a Vpc is or is not specified for imported load balancers.
Resolves: #5209