question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I attach an instance to an application target group?

See original GitHub issue

❓ General Issue

The Question

Hi, I’d like to create an application load balancer that has a listener pointing to a target group with a single instance target.

Not sure if I took the right approach at the first place (cannot find any relevant doc), but now I just can’t move on and I’m afraid I discovered a bug in CDK.

Environment

  • CDK CLI Version: 1.18.0 (build bc924bc)
  • Module Version: ?
  • OS: Kubuntu 18.04
  • Language: Python

Other information

This is my code:

from aws_cdk import (
    core,
    aws_ec2,
    aws_elasticloadbalancingv2,
)

class WebsiteStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        self.vpc = aws_ec2.Vpc.from_lookup(self, 'default_vpc', is_default=True)

        self.sg_ssh = aws_ec2.SecurityGroup(
            self,
            'ssh',
            vpc=self.vpc,
            description="Allow SSH from anywhere",
            security_group_name="SSH from anywhere"
        )
        self.sg_ssh.add_ingress_rule(aws_ec2.Peer.any_ipv4(), aws_ec2.Port.tcp(22))

        ami = aws_ec2.LookupMachineImage(
            name="ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*",
            owners=["099720109477"],
        )

        ec2 = aws_ec2.Instance(
            self,
            'website',
            instance_type=aws_ec2.InstanceType('t3a.micro'),
            machine_image=ami,
            vpc=self.vpc,
            security_group=self.sg_ssh,
        )

        tg = aws_elasticloadbalancingv2.ApplicationTargetGroup(
            self,
            'website-target-group',
            protocol=aws_elasticloadbalancingv2.ApplicationProtocol.HTTP,
            port=80,
            vpc=self.vpc,
            # target_type=aws_elasticloadbalancingv2.TargetType.INSTANCE,
            # targets=[ec2],  # FIXME
        )
        tg.add_target(ec2)  # FIXME

Running either cdk diff or cdk deploy with one of the “FIXME” - commented line uncommented leads to the following error:

$ cdk diff
jsii.errors.JavaScriptError:
  TypeError: target.attachToApplicationTargetGroup is not a function
      at ApplicationTargetGroup.addTarget (/tmp/jsii-kernel-Tq3fWv/node_modules/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-target-group.js:52:35)
      at _wrapSandboxCode (/home/my/cdktest/.env/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7582:51)
      at Kernel._wrapSandboxCode (/home/my/cdktest/.env/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8205:19)
      at ret._ensureSync (/home/my/cdktest/.env/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7582:25)
      at Kernel._ensureSync (/home/my/cdktest/.env/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8178:20)
      at Kernel.invoke (/home/my/cdktest/.env/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7581:26)
      at KernelHost.processRequest (/home/my/cdktest/.env/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7293:28)
      at KernelHost.run (/home/my/cdktest/.env/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7233:14)
      at Immediate.setImmediate [as _onImmediate] (/home/my/cdktest/.env/lib/python3.6/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7236: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:

Traceback (most recent call last):
  File "app.py", line 11, in <module>
    'account': '483******425',
  File "/home/my/cdktest/.env/lib/python3.6/site-packages/jsii/_runtime.py", line 66, in __call__
    inst = super().__call__(*args, **kwargs)
  File "/home/my/cdktest/website/website_stack.py", line 84, in __init__
    tg.add_target(ec2)  # FIXME
  File "/home/my/cdktest/.env/lib/python3.6/site-packages/aws_cdk/aws_elasticloadbalancingv2/__init__.py", line 8168, in add_target
    return jsii.invoke(self, "addTarget", [*targets])
  File "/home/my/cdktest/.env/lib/python3.6/site-packages/jsii/_kernel/__init__.py", line 107, in wrapped
    return _recursize_dereference(kernel, fn(kernel, *args, **kwargs))
  File "/home/my/cdktest/.env/lib/python3.6/site-packages/jsii/_kernel/__init__.py", line 282, in invoke
    args=_make_reference_for_native(self, args),
  File "/home/my/cdktest/.env/lib/python3.6/site-packages/jsii/_kernel/providers/process.py", line 348, in invoke
    return self._process.send(request, InvokeResponse)
  File "/home/my/cdktest/.env/lib/python3.6/site-packages/jsii/_kernel/providers/process.py", line 318, in send
    raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: target.attachToApplicationTargetGroup is not a function
Subprocess exited with error 1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
rahejapscommented, Sep 29, 2020

@porn, Thank you. I was able get some help from the gitter forum. I modified the listener code as follows.

from aws_cdk import aws_elasticloadbalancingv2_targets as targets

     listener.add_targets(
           'ec2_instance', 
           port=3130, 
           targets=[targets.InstanceIdTarget(
               instance_id=ec2a,
               port=3130
               ),
            targets.InstanceIdTarget(
               instance_id=ec2b,
               port=3130)]
         )
0reactions
porncommented, Sep 29, 2020

Hi @rahejaps,

I don’t have any code that is being used, but by the time I raised this bug, i was experimenting with various infra-as-code toolkits. You can check this one out, maybe it’ll be helpful: https://gitlab.com/ikar/infrastructure-as-code/-/blob/master/cdk-py/website/website_stack.py

Read more comments on GitHub >

github_iconTop Results From Across the Web

Target groups for your Application Load Balancers
On the navigation pane, under LOAD BALANCING, choose Target Groups. · Choose the name of the target group to open its details page....
Read more >
Target groups for your Network Load Balancers - 亚马逊云科技
Each target group is used to route requests to one or more registered targets. When you create a listener, you specify a target...
Read more >
Register EC2 Instances With Load Balancers Actions
Multiple instances can be added by clicking "Add many instances". In the dialog that appears, enter a space-, comma-, or line-seperated list of...
Read more >
Using Amazon Application Load Balancers
Target Groups can have one of two Instance Association Strategies, which define how the environment instances are associated with the Target ...
Read more >
Application Load Balancer and Target group attachment using ...
The terraform code will help you to create an Application Load Balancer, target group and then attaching the EC2 Instances within the TG....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found