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

aws-elasticloadbalancingv2: cannot create lambda TargetGroup #17261

Closed
pradeepviswanathan opened this issue Nov 1, 2021 · 13 comments · Fixed by #17271
Closed

aws-elasticloadbalancingv2: cannot create lambda TargetGroup #17261

pradeepviswanathan opened this issue Nov 1, 2021 · 13 comments · Fixed by #17271
Assignees
Labels
@aws-cdk/aws-elasticloadbalancingv2 Related to Amazon Elastic Load Balancing V2 bug This issue is a bug. effort/small Small work item – less than a day of effort p1

Comments

@pradeepviswanathan
Copy link

What is the problem?

Cannot create lambda TargetGroup as the created cfn template has stickiness.enabled set as true in TargetGroupAttributes. Seems like this attribute is not expected for lambda based target groups.

Reproduction Steps

const lambdaTargetGroup = new ApplicationTargetGroup(this, TargetGroup, {
targets: [new targets.LambdaTarget(lambdaFunction)],
});

You should be able to see stickiness.enabled set in cfn template. During deployment , deployment fails with

The provided target group attribute is not supported (Service: AmazonElasticLoadBalancing; Status Code: 400

What did you expect to happen?

Target group with lambda targets getting created successfully

What actually happened?

Target group with lambda targets are not getting created

CDK CLI Version

1.130

Framework Version

No response

Node.js Version

17.0.1

OS

macos 11.3.1

Language

Typescript

Language Version

No response

Other information

No response

@pradeepviswanathan pradeepviswanathan added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Nov 1, 2021
@github-actions github-actions bot added the @aws-cdk/aws-elasticloadbalancingv2 Related to Amazon Elastic Load Balancing V2 label Nov 1, 2021
@peterwoodworth peterwoodworth added needs-reproduction This issue needs reproduction. p1 labels Nov 2, 2021
@topecongiro
Copy link
Contributor

For reproduction, run mkdir cdk-issue && cd cdk-issue && cdk init app --language typescript, apply the following patch and run npm i && cdk deploy:

diff --git a/bin/cdk-issue.ts b/bin/cdk-issue.ts
index eb0c70e..fa0d52c 100644
--- a/bin/cdk-issue.ts
+++ b/bin/cdk-issue.ts
@@ -11,7 +11,7 @@ new CdkIssueStack(app, 'CdkIssueStack', {
 
   /* Uncomment the next line to specialize this stack for the AWS Account
    * and Region that are implied by the current CLI configuration. */
-  // env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
+  env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
 
   /* Uncomment the next line if you know exactly what Account and Region you
    * want to deploy the stack to. */
diff --git a/lib/cdk-issue-stack.lambda.ts b/lib/cdk-issue-stack.lambda.ts
new file mode 100644
index 0000000..46a7acb
--- /dev/null
+++ b/lib/cdk-issue-stack.lambda.ts
@@ -0,0 +1,5 @@
+export async function handler(
+    event: any,
+): Promise<any> {
+    return {};
+}
\ No newline at end of file
diff --git a/lib/cdk-issue-stack.ts b/lib/cdk-issue-stack.ts
index aa6e79e..864ac2e 100644
--- a/lib/cdk-issue-stack.ts
+++ b/lib/cdk-issue-stack.ts
@@ -1,9 +1,24 @@
 import * as cdk from '@aws-cdk/core';
+import * as ec2 from '@aws-cdk/aws-ec2';
+import * as lb from '@aws-cdk/aws-elasticloadbalancingv2';
+import * as targets from '@aws-cdk/aws-elasticloadbalancingv2-targets';
+import * as lambda from '@aws-cdk/aws-lambda-nodejs';
 
 export class CdkIssueStack extends cdk.Stack {
   constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
     super(scope, id, props);
 
-    // The code that defines your stack goes here
+    const alb = new lb.ApplicationLoadBalancer(this, 'lb', {
+      vpc: ec2.Vpc.fromLookup(this, 'vpc', { isDefault: true }),
+    });
+    const listener = alb.addListener('Listener', {
+      port: 80
+    });
+    listener.addTargets('Target', {
+      port: 80,
+      targets: [
+        new targets.LambdaTarget(new lambda.NodejsFunction(this, 'lambda')),
+      ],
+    });
   }
 }
diff --git a/package.json b/package.json
index 236f05f..cc6e0e4 100644
--- a/package.json
+++ b/package.json
@@ -14,13 +14,18 @@
     "@aws-cdk/assert": "1.130.0",
     "@types/jest": "^26.0.10",
     "@types/node": "10.17.27",
+    "aws-cdk": "1.130.0",
     "jest": "^26.4.2",
     "ts-jest": "^26.2.0",
-    "aws-cdk": "1.130.0",
     "ts-node": "^9.0.0",
     "typescript": "~3.9.7"
   },
   "dependencies": {
+    "@aws-cdk/aws-ec2": "^1.130.0",
+    "@aws-cdk/aws-elasticloadbalancingv2": "^1.130.0",
+    "@aws-cdk/aws-elasticloadbalancingv2-targets": "^1.130.0",
+    "@aws-cdk/aws-lambda-nodejs": "^1.130.0",
     "@aws-cdk/core": "1.130.0",
     "source-map-support": "^0.5.16"
   }

@PascalPflaum
Copy link

I can confirm this issue. Had to pin "1.129.0" to keep CDK avoiding this bug. The issue seems to be related with #17111.

@spg
Copy link
Contributor

spg commented Nov 2, 2021

Temporary workaround:

    const defaultTargetGroupFunc = new lambda.Function(this, 'DefaultTargetGroupFunc', {
      runtime: lambda.Runtime.NODEJS_12_X,
      handler: 'index.handler',
      code: lambda.Code.fromInline(
        'exports.handler = async function (event, context) {console.log(JSON.stringify(event));}'
      ),
      logRetention: logs.RetentionDays.ONE_WEEK,
    })
    const defaultFuncPermission = new lambda.CfnPermission(this, 'perm', {
      action: 'lambda:InvokeFunction',
      functionName: defaultTargetGroupFunc.functionName,
      principal: 'elasticloadbalancing.amazonaws.com',
    })

    const lambdaTarget = new elbv2Targets.LambdaTarget(defaultTargetGroupFunc)
    const defaultTargetGroup = new alb.ApplicationTargetGroup(this, 'DefaultTargetGroup', {
      targetType: alb.TargetType.LAMBDA,
      targets: [lambdaTarget],
    })
    const cfnDefaultTargetGroup = defaultTargetGroup.node.defaultChild as alb.CfnTargetGroup
    cfnDefaultTargetGroup.addDependsOn(defaultFuncPermission)

    // TODO: remove this once https://github.com/aws/aws-cdk/issues/17261 is fixed
    cfnDefaultTargetGroup.addDeletionOverride('Properties.TargetGroupAttributes')

@peterwoodworth peterwoodworth removed the needs-reproduction This issue needs reproduction. label Nov 2, 2021
@peterwoodworth
Copy link
Contributor

Thanks all for clearly describing the issue and providing a workaround, much appreciated 😄 I can confirm the behavior described here

@peterwoodworth peterwoodworth added effort/small Small work item – less than a day of effort and removed needs-triage This issue or PR still needs to be triaged. labels Nov 3, 2021
@gov-sanap
Copy link

Facing similar issue in python. Can anyone provide temporary fix for python also? That will be really appreciated. Thank you

@gov-sanap
Copy link

Got it after trying few things. Following is snippet of what I did.

elbv2.CfnTargetGroup.add_deletion_override(tg1.node.default_child, 'Properties.TargetGroupAttributes')

In the above code tg1 is object of elbv2.ApplicationTargetGroup.
Add tg1 to listener using listener.add_target_groups or using any other way.

@gburke-ppb
Copy link

Any idea when this issue will be fixed? There have been 2 releases of the CDK since it was introduced and it's still present...

@adriantaut
Copy link
Contributor

Also affected by this 😨

@koshic
Copy link

koshic commented Nov 23, 2021

Much simple workaround - 'listener.addTargets(...).setAttribute('stickiness.enabled', undefined)'

@David-Tamrazov
Copy link

Much simple workaround - 'listener.addTargets(...).setAttribute('stickiness.enabled', undefined)'

imo this is great cause its an easy half-a-line fix and outlines exactly which properties are problematic with a direct fix 👍

@PascalPflaum
Copy link

#17271 seems to be ready to be merged and included in next release. Any update on this?

@mergify mergify bot closed this as completed in #17271 Nov 29, 2021
mergify bot pushed a commit that referenced this issue Nov 29, 2021
… type is lambda (#17271)

Avoid setting `stickiness.enabled` to `false` when the target group type is lambda as it breaks on deployment.

Fixes #17261.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@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.

pedrosola pushed a commit to pedrosola/aws-cdk that referenced this issue Dec 1, 2021
… type is lambda (aws#17271)

Avoid setting `stickiness.enabled` to `false` when the target group type is lambda as it breaks on deployment.

Fixes aws#17261.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@stewberticus
Copy link

A target group with HTTPS protocol does not support the attribute stickiness.enabled

adding the exact error text so this thread is more easily searched from google

TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this issue Feb 21, 2022
… type is lambda (aws#17271)

Avoid setting `stickiness.enabled` to `false` when the target group type is lambda as it breaks on deployment.

Fixes aws#17261.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-elasticloadbalancingv2 Related to Amazon Elastic Load Balancing V2 bug This issue is a bug. effort/small Small work item – less than a day of effort p1
Projects
None yet