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_elasticloadbalancing): add classic load balancer support for EC2 instance #23500

Closed
LucasSymons opened this issue Dec 30, 2022 · 3 comments · Fixed by #24353
Closed

(aws_elasticloadbalancing): add classic load balancer support for EC2 instance #23500

LucasSymons opened this issue Dec 30, 2022 · 3 comments · Fixed by #24353
Labels
@aws-cdk/aws-elasticloadbalancing Related to Amazon Elastic Load Balancing effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. good first issue Related to contributions. See CONTRIBUTING.md p2

Comments

@LucasSymons
Copy link

Describe the bug

Trying to attach a single EC2 instance to a classic ELB when using the add target method, I get an error

##Method

elb.add_target(instance.instance_id)

Docs

https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_elasticloadbalancing/LoadBalancer.html#aws_cdk.aws_elasticloadbalancing.LoadBalancer.add_target

Error

jsii.errors.JavaScriptError: 
  @jsii/kernel.RuntimeError: TypeError: target.attachToClassicLB is not a function
      at Kernel._ensureSync (/private/var/folders/72/tttvxd_17gz67dmn1zlcbx7m0000gn/T/tmp4b19xcp3/lib/program.js:8427:27)
      at Kernel.invoke (/private/var/folders/72/tttvxd_17gz67dmn1zlcbx7m0000gn/T/tmp4b19xcp3/lib/program.js:7827:34)
      at KernelHost.processRequest (/private/var/folders/72/tttvxd_17gz67dmn1zlcbx7m0000gn/T/tmp4b19xcp3/lib/program.js:11037:36)
      at KernelHost.run (/private/var/folders/72/tttvxd_17gz67dmn1zlcbx7m0000gn/T/tmp4b19xcp3/lib/program.js:10997:22)
      at Immediate._onImmediate (/private/var/folders/72/tttvxd_17gz67dmn1zlcbx7m0000gn/T/tmp4b19xcp3/lib/program.js:10998:46)
      at process.processImmediate (node:internal/timers:471:21)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Documents/repo/base-aws-cloudformation/stacks/gitlab/app.py", line 33, in <module>
    ec2 = GitlabStack(app, 'Gitlab-EC2', props=props, env=Environment(
  File "/.local/share/virtualenvs/gitlab-qBbXTgKn/lib/python3.10/site-packages/jsii/_runtime.py", line 111, in __call__
    inst = super().__call__(*args, **kwargs)
  File "/Documents/repo/base-aws-cloudformation/stacks/gitlab/gitlab/gitlab_ec2.py", line 177, in __init__
    gitlab_elb.add_target(gitlab_instance)
  File "/.local/share/virtualenvs/gitlab-qBbXTgKn/lib/python3.10/site-packages/aws_cdk/aws_elasticloadbalancing/__init__.py", line 2399, in add_target
    return typing.cast(None, jsii.invoke(self, "addTarget", [target]))
  File "/.local/share/virtualenvs/gitlab-qBbXTgKn/lib/python3.10/site-packages/jsii/_kernel/__init__.py", line 149, in wrapped
    return _recursize_dereference(kernel, fn(kernel, *args, **kwargs))
  File "/.local/share/virtualenvs/gitlab-qBbXTgKn/lib/python3.10/site-packages/jsii/_kernel/__init__.py", line 401, in invoke
    response = self.provider.invoke(
  File "/.local/share/virtualenvs/gitlab-qBbXTgKn/lib/python3.10/site-packages/jsii/_kernel/providers/process.py", line 378, in invoke
    return self._process.send(request, InvokeResponse)
  File "/.local/share/virtualenvs/gitlab-qBbXTgKn/lib/python3.10/site-packages/jsii/_kernel/providers/process.py", line 340, in send
    raise RuntimeError(resp.error) from JavaScriptError(resp.stack)
RuntimeError: TypeError: target.attachToClassicLB is not a function

Subprocess exited with error 1

Expected Behavior

I expected to be able to register a single instance as the target for the ELB

Current Behavior

Error posted above

Reproduction Steps

# vpc: ec2.IVpc

# my_instance: ec2.Instance

lb = elb.LoadBalancer(self, "LB",
    vpc=vpc,
    internet_facing=True,
    health_check=elb.HealthCheck(
        port=80
    )
)
lb.add_target(my_instance)

Possible Solution

I found a post suggesting a possible workaround but it is untested

# https://stackoverflow.com/a/68632192
        elbObj = elb.node.default_child
        if elbObj:
            elbObj.instances = [instance.instance_id, ]

Additional Information/Context

No response

CDK CLI Version

2.58.0 (build 4a577f3)

Framework Version

aws-cdk-lib = "==2.54.0"

Node.js Version

node --version v18.1.0

OS

MacOS Ventura 13.1 (22C65)

Language

Python

Language Version

Python 3.10.9

Other information

No response

@LucasSymons LucasSymons added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 30, 2022
@github-actions github-actions bot added the @aws-cdk/aws-elasticloadbalancing Related to Amazon Elastic Load Balancing label Dec 30, 2022
@peterwoodworth
Copy link
Contributor

peterwoodworth commented Dec 30, 2022

The addTarget method is designed to work on constructs which implement ILoadBalancerTarget. EC2's Instance doesn't implement this, so this method isn't intended to be working here. At least yet, this is definitely something we could support.

Please try the escape hatch method described until support is provided for this.

I am marking this issue as p2, which means that we are unable to work on this immediately.

We use +1s to help prioritize our work, and are happy to revaluate this issue based on community feedback. You can reach out to the cdk.dev community on Slack to solicit support for reprioritization.

Check out our contributing guide if you're interested in contributing yourself - there's a low chance the team will be able to address this soon but we can try to review a PR 🙂

@peterwoodworth peterwoodworth added p2 effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. and removed needs-triage This issue or PR still needs to be triaged. bug This issue is a bug. labels Dec 30, 2022
@peterwoodworth peterwoodworth changed the title (aws_elasticloadbalancing): (target.attachToClassicLB is not a function) (aws_elasticloadbalancing): add classic load balancer support for EC2 instance Dec 30, 2022
@peterwoodworth peterwoodworth added the good first issue Related to contributions. See CONTRIBUTING.md label Dec 30, 2022
@zorrofox
Copy link
Contributor

zorrofox commented Feb 5, 2023

We know that ILoadBalancerTarget. only have the addTarget method, and if we add the Instance to support this method too we need implement this method to pick up one of the elb.LoadBalancer targets which is an array and how we implement this logic? And if there have no targets exists do we need create a new one?

@mergify mergify bot closed this as completed in #24353 Mar 17, 2023
mergify bot pushed a commit that referenced this issue Mar 17, 2023
…ces (#24353)

Introducing the `InstanceTarget` class: an EC2 instance that serves as the target for load balancing. This class allows to register an instance to a load balancer. `InstanceTarget ` takes an instance to register as the target for the  load balancer.

For example,
```ts
const target = new elb.InstanceTarget(instance);
elb.addTarget(target);
```
creates an InstanceTarget object with the specified instance and then adds it to the load balancer.

> [CONTRIBUTING GUIDE]: https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md
> [DESIGN GUIDELINES]: https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md

Closes #23500.

----

*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.

homakk pushed a commit to homakk/aws-cdk that referenced this issue Mar 28, 2023
…ces (aws#24353)

Introducing the `InstanceTarget` class: an EC2 instance that serves as the target for load balancing. This class allows to register an instance to a load balancer. `InstanceTarget ` takes an instance to register as the target for the  load balancer.

For example,
```ts
const target = new elb.InstanceTarget(instance);
elb.addTarget(target);
```
creates an InstanceTarget object with the specified instance and then adds it to the load balancer.

> [CONTRIBUTING GUIDE]: https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md
> [DESIGN GUIDELINES]: https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md

Closes aws#23500.

----

*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-elasticloadbalancing Related to Amazon Elastic Load Balancing effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. good first issue Related to contributions. See CONTRIBUTING.md p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants