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

feat(elbv2): add support for Lambda targets #3348

Merged
merged 25 commits into from
Aug 26, 2019

Conversation

bweigel
Copy link
Contributor

@bweigel bweigel commented Jul 18, 2019

Add a new package for ELBv2 targets called @aws-cdk/aws-elasticloadbalancingv2-targets.

In this package, add a LambdaTarget which can be used to add Lambas as a backend
for ALBs.

IpTarget and InstanceTarget have been moved to the new package, but the originals
have been left in place to not break backwards compatibility (they have been marked
@deprecated to encourage movement to the new classes).

Fixes #1921.

Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

Great start!


super(scope, id, props, {
const targetType = props ? props.targetType : undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why introduce all these local variables?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TBH I am not that proficient in TS and didn't really know what was idiomatic, since props had to be optional.
I tried to make it a little simpler now. Any advice on how to do it more idiomatic is greatly appreciated.

@@ -111,3 +111,42 @@ export class IpTarget implements IApplicationLoadBalancerTarget, INetworkLoadBal
};
}
}

export class LambdaTarget implements IApplicationLoadBalancerTarget {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering whether this class should go into a separate package, so we can set up dependencies properly so that...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A separate package inside aws-elasticloadbalancingv2? Like inside lib/shared?
I thought about that, but then IpTarget and InstanceTarget were also here.
I like the idea and can get going on this in the next few days, if you think it is the way forward.

*
* @param functionArn The Lambda Function ARN to load balance to
*/
constructor(private readonly functionArn: string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

...this can be an IFunction.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

@@ -189,6 +190,16 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
throw new Error('Can only call addTargets() when using a constructed Load Balancer; construct a new TargetGroup and use addTargetGroup');
}

function hasLambdaTargets(targets: IApplicationLoadBalancerTarget[]): boolean {
if (targets.length > 0) {
return targets.map(target => target instanceof LambdaTarget).reduceRight((previous, current) => previous || current);
Copy link
Contributor

Choose a reason for hiding this comment

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

Simpler would be:

const hasLambdaTargets = targets.some(target => target instanceof LambdaTarget);

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 tip. Didn't know that :)

@@ -198,7 +209,8 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
stickinessCookieDuration: props.stickinessCookieDuration,
targetGroupName: props.targetGroupName,
targets: props.targets,
vpc: this.loadBalancer.vpc,
targetType,
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe targetType is autodeduced from calling addTarget(), no? So you shouldn't need to pass it here?

Copy link
Contributor Author

@bweigel bweigel Aug 10, 2019

Choose a reason for hiding this comment

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

Cool. Didn't see that. The tests are passing, so thanks 🙂
Well I guess the tests aren't passing after all, if I don't explicitly supply the targetGroup here, if it is Lambda.

@j
Copy link

j commented Aug 19, 2019

Is there a current work-around to get ALB + Lambda working with CDK?

@mergify
Copy link
Contributor

mergify bot commented Aug 20, 2019

Pull Request Checklist

  • Testing
  • Unit test added (prefer to add a new test rather than modify existing tests)
  • CLI change? Re-run/add CLI integration tests
  • Documentation
  • Inline docs: make sure all public APIs are documented (copy & paste from official AWS docs)
  • README: update module README
  • Design: for significant features, follow the design process
  • Title uses the format type(scope): text
  • Type: fix, feat, refactor go into CHANGELOG, chore is hidden
  • Scope: name of the module without the aws- or cdk- prefix or postfix (e.g. s3 instead of aws-s3-deployment)
  • Style: use all lower-case, do not end with a period
  • Description
  • Rationale: describe rationale of change and approach taken
  • Issues: Indicate issues fixed via: fixes #xxx or closes #xxx
  • Breaking?: last paragraph: BREAKING CHANGE: <describe what changed + link for details>
  • Sensitive Modules (requires 2 PR approvers)
  • IAM document library (in @aws-cdk/aws-iam)
  • EC2 security groups and ACLs (in @aws-cdk/aws-ec2)
  • Grant APIs (if not based on official documentation with a reference)

@rix0rrr rix0rrr changed the title feat(elasticloadbalancing): add support for lambda target feat(elbv2): add support for Lambda targets Aug 23, 2019
@rix0rrr rix0rrr marked this pull request as ready for review August 23, 2019 12:26
@mergify
Copy link
Contributor

mergify bot commented Aug 23, 2019

Codebuild (Continuous Integration) build failed for current commits. Please check log and resolve before PR is merged.

@mergify
Copy link
Contributor

mergify bot commented Aug 23, 2019

Codebuild (Continuous Integration) build failed for current commits. Please check log and resolve before PR is merged.

@mergify
Copy link
Contributor

mergify bot commented Aug 23, 2019

Codebuild (Continuous Integration) build failed for current commits. Please check log and resolve before PR is merged.

@rix0rrr rix0rrr added the pr/do-not-merge This PR should not be merged at this time. label Aug 26, 2019
rix0rrr
rix0rrr previously approved these changes Aug 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr/do-not-merge This PR should not be merged at this time.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ALB Lambda Target Support
3 participants