aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

[AutoScaling] ApplicationTargetGroup method expects AutoscalingGroup class but static method from_auto_scaling_group_name is returning IAutoScalingGroup

Open ysenthu opened this issue 6 years ago • 4 comments

Description of the bug

aws_cdk.aws_autoscaling.AutoScalingGroup.from_auto_scaling_group_name(self, AsgName, auto_scaling_group_name=AsgName) is returning IAutoScalingGroup instead of AutoscalingGroup. Since ApplicationTargetGourp is only accepting AutoscalingGroup unable to attach imported IAutoscalingGroup to the target group

Reproduction Steps

asg = autoscaling.AutoScalingGroup.from_auto_scaling_group_name(self,autoscaling_group_name,auto_scaling_group_name="sample-asg") target_group = elbv2.ApplicationTargetGroup(self,"sample-target",targets=[asg],port=8080,vpc=vpc)

What did you expect to happen?

ApplicationTargetGroup should be able to accept IAutoScalingGroup as a target in the targets parameter or there should be a way to cast IAutoScalingGroup to AutoScalingGroup.

What actually happened?

jsii.errors.JavaScriptError: TypeError: target.attachToApplicationTargetGroup is not a function at ApplicationTargetGroup.addTarget (/tmp/jsii-kernel-39Glfi/node_modules/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-target-group.js:53:35) at new ApplicationTargetGroup (/tmp/jsii-kernel-39Glfi/node_modules/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-target-group.js:31:18) at obj._wrapSandboxCode (/mnt/c/Users/uyog2se/OneDrive - Pearson PLC/Documents/solostack/appdeploy/.env/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7922:49) at Kernel._wrapSandboxCode (/mnt/c/Users/uyog2se/OneDrive - Pearson PLC/Documents/solostack/appdeploy/.env/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8398:19) at Kernel._create (/mnt/c/Users/uyog2se/OneDrive - Pearson PLC/Documents/solostack/appdeploy/.env/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7922:26) at Kernel.create (/mnt/c/Users/uyog2se/OneDrive - Pearson PLC/Documents/solostack/appdeploy/.env/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7666:21) at KernelHost.processRequest (/mnt/c/Users/uyog2se/OneDrive - Pearson PLC/Documents/solostack/appdeploy/.env/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7446:28) at KernelHost.run (/mnt/c/Users/uyog2se/OneDrive - Pearson PLC/Documents/solostack/appdeploy/.env/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7384:14) at Immediate.setImmediate [as _onImmediate] (/mnt/c/Users/uyog2se/OneDrive - Pearson PLC/Documents/solostack/appdeploy/.env/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7387:37) at runCallback (timers.js:705:18) at tryOnImmediate (timers.js:676:5) at processImmediate (timers.js:658:5)

The above exception was the direct cause of the following exception: jsii.errors.JSIIError: target.attachToApplicationTargetGroup is not a function

Environment

  • CLI Version : 1.46.0 (build 63860b2)
  • Framework Version: aws-cdk.aws-autoscaling==1.56.0, aws-cdk.aws-elasticloadbalancingv2==1.56.0
  • Node.js Version: v10.21.0
  • OS : Ubuntu 18
  • Language (Version): Python 3.6.9

Other


This is :bug: Bug Report

ysenthu avatar Aug 18 '20 07:08 ysenthu

Hi @senthurany, I took a look at this along with @NetaNir. Returning IAutoScalingGroup is the right thing to do here because you are importing an AutoscalingGroup to the CDK and thus, the CDK cannot modify it. Unfortunately, adding an AutoscalingGroup as a target group to an Elastic Load Balancer is registered on the AutoscalingGroup -- the ELB is added into the targetGroupArns property in ASG. Since you are importing your AutoscalingGroup, it isn't possible to modify targetGroupArns here.

The error you are getting is due to the fact that a targetin elbv2.ApplicationTargetGroup() has the type IApplicationLoadBalancerTarget that requires the method attachToApplicationTargetGroup(). You are sending in an IAutoScalingGroup, which does not implement attachToApplicationTargetGroup() (because, as mentioned above, it is not possible).

Hope this explanation helps.

kaizencc avatar Aug 19 '20 23:08 kaizencc

Hi @kaizen3031593 , Thank you for the explanations, Our situation is like we have two auto scaling groups in one stack, after we created the both asgs for the first time, the second time we only need to modify one asg without touching the second one so we require current config of the asg which we do not wanna change which will require boto3, we are trying to achieve this without using boto3 hence the code will be more cleaner, Is there anyway to do this or atleast get all attributes of the asg using cdk ?

ysenthu avatar Aug 20 '20 04:08 ysenthu

Hi @senthurany, I am not sure what you mean by creating asg the second time -- perhaps a code snippet would be helpful in understanding what you are trying to do.

kaizencc avatar Aug 20 '20 16:08 kaizencc

Any suggested workaround about this issue expect having to create a new ASG? I have an ASG already built through another CDK stack, just want to import it and register it as an ELB target

andybirenzi avatar Dec 14 '20 18:12 andybirenzi