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(ecs-patterns): allow imported load balancers as inputs #5461

Merged
merged 19 commits into from
Feb 12, 2020

Conversation

bvtujo
Copy link
Contributor

@bvtujo bvtujo commented Dec 17, 2019

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:

  1. editing the Network and Application load balancer constructs to add an optional IVpc property to be used when calling the fromLoadBalancerAttributes static methods, and
  2. changing the error which is thrown when calling addTargets on imported load balancers.

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:

  1. editing the Network and Application load balancer constructs to add an optional IVpc property to be used when calling the fromLoadBalancerAttributes static methods, and
  2. changing the error which is thrown when calling addTargets on imported load balancers.

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

@bvtujo bvtujo requested a review from a team December 17, 2019 17:38
@bvtujo bvtujo requested review from piradeepk and removed request for a team, rix0rrr and SoManyHs December 17, 2019 17:39
@piradeepk piradeepk changed the title feat(aws-ecs-patterns): allow imported load balancers in l3 constructs feat(aws-ecs-patterns): allow imported load balancers in alb/nlb constructs Dec 17, 2019
@piradeepk piradeepk changed the title feat(aws-ecs-patterns): allow imported load balancers in alb/nlb constructs feat(ecs-patterns): allow imported load balancers in alb/nlb constructs Dec 17, 2019
@piradeepk piradeepk requested a review from SoManyHs December 17, 2019 17:43
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@bvtujo bvtujo force-pushed the bvtujo/allow-imported-lb branch from 0ad5580 to 74d8bcc Compare December 17, 2019 18:02
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@bvtujo bvtujo force-pushed the bvtujo/allow-imported-lb branch from c21a848 to 2e576f4 Compare December 20, 2019 00:08
@mergify mergify bot dismissed piradeepk’s stale review December 20, 2019 00:09

Pull request has been modified.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot dismissed piradeepk’s stale review February 5, 2020 17:18

Pull request has been modified.

piradeepk
piradeepk previously approved these changes Feb 5, 2020
Copy link
Contributor

@piradeepk piradeepk left a 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-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot dismissed piradeepk’s stale review February 5, 2020 19:01

Pull request has been modified.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

piradeepk
piradeepk previously approved these changes Feb 7, 2020
Copy link
Contributor

@piradeepk piradeepk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ship it!

@mergify mergify bot dismissed piradeepk’s stale review February 7, 2020 16:06

Pull request has been modified.

piradeepk
piradeepk previously approved these changes Feb 7, 2020
Copy link
Contributor

@piradeepk piradeepk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ship it!

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

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
Copy link
Contributor

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?

Copy link
Contributor

@rix0rrr rix0rrr Feb 10, 2020

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.

Copy link
Contributor Author

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation please.

Copy link
Contributor Author

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);
Copy link
Contributor

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;
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved in 57cd88a

@rix0rrr rix0rrr self-assigned this Feb 10, 2020
@mergify mergify bot dismissed stale reviews from piradeepk and rix0rrr February 12, 2020 01:15

Pull request has been modified.

@rix0rrr rix0rrr changed the title feat(ecs-patterns): allow imported load balancers as inputs fix(ecs-patterns): allow imported load balancers as inputs Feb 12, 2020
rix0rrr
rix0rrr previously approved these changes Feb 12, 2020
@rix0rrr rix0rrr removed the pr/do-not-merge This PR should not be merged at this time. label Feb 12, 2020
@mergify mergify bot dismissed rix0rrr’s stale review February 12, 2020 17:32

Pull request has been modified.

Copy link
Contributor

@piradeepk piradeepk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work @bvtujo!

@mergify
Copy link
Contributor

mergify bot commented Feb 12, 2020

Thank you for contributing! Your pull request is now being automatically merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ecs Related to Amazon Elastic Container
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to use imported ALB/NLB with LoadBalancedFargateService
7 participants