Create Network Load Balancer (NLB) using existing EC2 instances with AWS-CDK

1.5k views Asked by At

We have 2 proxy servers which we will like to be front-ended with NLB. I am using AWS-CDK (python) for creating via CloudFormation. I am trying to register ec2 as either instanceid or instance documentation link. I am hoping if someone can provide a link to an example or help to fix the code below.

from aws_cdk import core
from aws_cdk import aws_elasticloadbalancingv2 as _elbv2 
from aws_cdk import aws_ec2 as _ec2
from aws_cdk import aws_elasticloadbalancingv2_targets as _elbv2target


class EnergyelbStack(core.Stack):

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

  
    vpc =  _ec2.Vpc.from_lookup(
        self,
        "vpc",
        vpc_id = "vpc-0exxxb36c"
    )
    
    ec2a = "i-0xxx7f31"
    ec2b = "i-0xxx80b1"
    
    tg = _elbv2target.InstanceIdTarget(
        instance_id = [ec2a],
        port = 3130
    )
    
    nlb = _elbv2.NetworkLoadBalancer(
        self, 
        "NetworkLB",
        vpc=vpc,
        internet_facing=False,
        #attach_to_network_target_group = tg
        )
    # Add a listener on a particular port.
    listener = nlb.add_listener(
        "Listener",
        protocol = "tcp",
        port=3130
    )
    target_group = listener.add_targets("ProxyServer",
        port=3130,
        targets=tg
    )
    
    listener.attach_to_network_target_group(tg)
1

There are 1 answers

0
Parvinder Raheja On

Here is the updated code that worked. It provided by Alban Esc @DaWyz gitter.com/aws-cdk

from aws_cdk import core
from aws_cdk import aws_elasticloadbalancingv2 as _elbv2 
from aws_cdk import aws_ec2 as _ec2
from aws_cdk import aws_elasticloadbalancingv2_targets as targets

class ElbStack(core.Stack):

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

        # The code that defines your stack goes here
    # Create the load balancer in a VPC. 'internetFacing' is 'false'
    # by default, which creates an internal load balancer.
    
        vpc =  _ec2.Vpc.from_lookup(
            self,
            "vpc",
            vpc_id = "vpc-xxxx36c"
        )
        
        ec2a = "i-xxxf31"
        ec2b = "i-xxx0b1"
               
        nlb = _elbv2.NetworkLoadBalancer(
            self, 
            "NetworkLB",
            vpc=vpc,
            internet_facing=False,
            #attach_to_network_target_group = tg
            )
        # Add a listener on a particular port.
        listener = nlb.add_listener(
            "Listener",
            port=3130,
        )
        listener.add_targets(
           'ec2_instance', 
           port=3130, 
           targets=[targets.InstanceIdTarget(
               instance_id=ec2a,
               port=3130
               ),
            targets.InstanceIdTarget(
               instance_id=ec2b,
               port=3130
            )
            ]
         )