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

ALB Lambda Target Support #1921

Closed
dbettin opened this issue Mar 1, 2019 · 7 comments · Fixed by #3348
Closed

ALB Lambda Target Support #1921

dbettin opened this issue Mar 1, 2019 · 7 comments · Fixed by #3348
Assignees
Labels
@aws-cdk/aws-elasticloadbalancing Related to Amazon Elastic Load Balancing @aws-cdk/aws-lambda Related to AWS Lambda feature-request A feature should be added or improved.

Comments

@dbettin
Copy link

dbettin commented Mar 1, 2019

I am perhaps missing something, but I don't see support for Lambdas as an ALB target.

Please let me know if it is currently supported.

@sam-goodwin sam-goodwin added @aws-cdk/aws-lambda Related to AWS Lambda @aws-cdk/aws-elasticloadbalancing Related to Amazon Elastic Load Balancing feature-request A feature should be added or improved. labels Mar 4, 2019
@sam-goodwin
Copy link
Contributor

IFunction needs to be updated to implement IApplicationGroupTarget

Good discussion in SAM: aws/serverless-application-model#721

@slipdexic
Copy link
Contributor

New to the project, I would like to work on this.

@eladb
Copy link
Contributor

eladb commented May 5, 2019

@slipdexic yey! Feel free. No one is actively working on this.

@slipdexic
Copy link
Contributor

Cool, I will start working on this.

@bweigel
Copy link
Contributor

bweigel commented Jun 26, 2019

@slipdexic are you still working on this?

Also at anyone → What is the sensible way to do here, concerning the target group:

  • add a new LambdaTargetGroup (introducing some duplication), or
  • implement the expected behavior inside ApplicationTargetGroup? (it is after all still an [overloaded] application load balancer target in cloudformation...)

Also, since now vpc should be optional (or rather conditional) in BaseTargetGroupProps and TargetGroupBase how would you suggest going about the linting-error that enforces all optional property-Props needing to be optional themselves? Should that be solved with some kind of type-system trickery?

constructor(scope: Construct, id: string, props: ApplicationTargetGroupProps)constructor(scope: Construct, id: string, props?: ApplicationTargetGroupProps)

@mitchlloyd
Copy link
Contributor

Was looking at this today. Here are the issues I found:

  1. As other noted, the value 'lambda' missing from enum type. Easy to work around with a type assertion.
  2. Using loadbalancerListener.addTargets doesn't work because adding the target without a protocol or port will fail CDK validation with "Supply at least one of protocol and port". However adding a protocol or port fails Cfn validation with "Port cannot be specified for target groups with target type 'lambda'".

Here's the code I was using up to the point where I got stuck.

import cdk = require("@aws-cdk/core");
import lambda = require("@aws-cdk/aws-lambda");
import elbv2 = require("@aws-cdk/aws-elasticloadbalancingv2");
import ec2 = require("@aws-cdk/aws-ec2");

export class TheStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const vpc = new ec2.Vpc(this, "Main");

    const fn = new lambda.Function(this, "API", {
      handler: "index",
      runtime: lambda.Runtime.NODEJS_10_X,
      code: new lambda.AssetCode("dist"),
      vpc
    });

    const fnAlias = new lambda.Alias(this, "Live", {
      version: fn.addVersion("1"),
      aliasName: "Live"
    });

    const lb = new elbv2.ApplicationLoadBalancer(this, "LoadBalancer", {
      vpc,
      internetFacing: false
    });

    const listener = lb.addListener("Listener", {
      port: 80,
    });
    listener.addTargets('Targets', {
      targets: [new LambdaALBTarget(fnAlias)]
    });
  }
}

class LambdaALBTarget implements elbv2.IApplicationLoadBalancerTarget {
  private fn: lambda.IFunction;

  constructor(fn: lambda.IFunction) {
    this.fn = fn;
  }

  attachToApplicationTargetGroup(
    targetGroup: elbv2.ApplicationTargetGroup
  ): elbv2.LoadBalancerTargetProps {
    return {
      targetType: "lambda" as elbv2.TargetType,
      targetJson: {
        id: this.fn.functionArn
      }
    };
  }
}

@eladb eladb assigned eladb and unassigned rix0rrr Aug 12, 2019
rix0rrr pushed a commit that referenced this issue Aug 26, 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.
@hleb-albau
Copy link

Also, it is not possible to enable health-check (no such property, and it is disabled by default for lambda).

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 @aws-cdk/aws-lambda Related to AWS Lambda feature-request A feature should be added or improved.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants