title: Unable to pass the ec2-instance details while creating AWS network load balancer using AWS CDK for python
body: <p>I am trying to create two EC2 instances and a Network load balancer using python for AWS-CDK. I am unable to pass in the target type details which are the above two instances while creating the NLB target group. In short, I need to create EC2 instances and attach a network load balance along with it. Please refer to the below code.</p>
  <pre><code>from aws_cdk import core 
  from aws_cdk import aws_ec2 as ec2
  from aws_cdk import aws_elasticloadbalancingv2 as elb
  from aws_cdk import aws_elasticloadbalancingv2_targets as targets
  from cdk_splunk_servers.myconstants import *
  
  
   class CdkDeploymentStack(core.Stack):
  
     def __init__(
      self,
      scope: core.Construct,
      construct_id: str,
      env_vars,
      inbound_rules,
      **kwargs
  ) -&gt; None:
      super().__init__(scope, construct_id, **kwargs)
  
      myvpc = ec2.Vpc.from_lookup(self, &quot;myvpc&quot;, vpc_name=env_vars[&quot;vpc_name&quot;])
  
      ### Create the security group with required ingress rules
      ec2_sg = ec2.SecurityGroup(self, &quot;sg-splunk&quot;, vpc=myvpc)
      core.Tags.of(ec2_sg).add(&quot;Name&quot;, ec2_sg.node.path)
  
      # Add the required inbound rules to the security group
      for rule in inbound_rules:
          peer = ec2.Peer.ipv4(rule[&quot;source&quot;])
          if rule[&quot;protocol&quot;] == &quot;tcp&quot;:
              if rule[&quot;portRange&quot;] == &quot;&quot;:
                  connection = ec2.Port.tcp(rule[&quot;port&quot;])
              else:
                  portRange = rule[&quot;portRange&quot;].split(&quot;-&quot;)
                  if portRange.__len__() != 2:
                      print(&quot;Port range should be in format startport-endport&quot;)
                  print(dir(ec2.Port))
                  connection = ec2.Port.tcp_range(
                      int(portRange[0]), int(portRange[1])
                  )
          elif rule[&quot;protocol&quot;] == &quot;udp&quot;:
              if rule[&quot;portRange&quot;] == &quot;&quot;:
                  connection = ec2.Port.udp(rule[&quot;port&quot;])
              else:
                  portRange = rule[&quot;portRange&quot;].split(&quot;-&quot;)
                  if portRange.__len__() != 2:
                      print(&quot;Port range should be in format startport-endport&quot;)
                  connection = ec2.Port.udp_range(
                      int(portRange[0]), int(portRange[1])
                  )
          description = rule[&quot;description&quot;]
          ec2_sg.add_ingress_rule(
              peer=peer, connection=connection, description=description
          )
      
      ### Create EC2 instance for Deployment servers
      deployment_01 = ec2.CfnInstance(
          self,
          env_vars[&quot;deployment_server_name1&quot;],
          block_device_mappings=[
              ec2.CfnInstance.BlockDeviceMappingProperty(
                  device_name=&quot;/dev/sda1&quot;,
                  ebs=ec2.CfnInstance.EbsProperty(
                      delete_on_termination=False, encrypted=True, volume_size=env_vars[&quot;deployment_root_volume&quot;]
                  ),
              ),
              ec2.CfnInstance.BlockDeviceMappingProperty(
                  device_name=&quot;/dev/sdb&quot;,
                  ebs=ec2.CfnInstance.EbsProperty(
                      delete_on_termination=False, encrypted=True, volume_size=env_vars[&quot;deployment_user_volume1&quot;]
                  ),
              ),
              ec2.CfnInstance.BlockDeviceMappingProperty(
                  device_name=&quot;/dev/sdc&quot;,
                  ebs=ec2.CfnInstance.EbsProperty(
                      delete_on_termination=False, encrypted=True, volume_size=env_vars[&quot;deployment_user_volume2&quot;]
                  ),
              )
          ],
          ebs_optimized=True,
          image_id=env_vars[&quot;ami_id_rhel&quot;],
          instance_type=env_vars[&quot;instance_type_dep&quot;],
          key_name=env_vars[&quot;key_pair_name&quot;],
          security_group_ids=[ec2_sg.security_group_id],
          subnet_id=env_vars[&quot;general_subnet1&quot;]
      )
      deployment_02 = ec2.CfnInstance(
          self,
          env_vars[&quot;deployment_server_name2&quot;],
          block_device_mappings=[
             ec2.CfnInstance.BlockDeviceMappingProperty(
                  device_name=&quot;/dev/sda1&quot;,
                  ebs=ec2.CfnInstance.EbsProperty(
                      delete_on_termination=False, encrypted=True, volume_size=env_vars[&quot;deployment_root_volume&quot;]
                  ),
              ),
              ec2.CfnInstance.BlockDeviceMappingProperty(
                  device_name=&quot;/dev/sdb&quot;,
                  ebs=ec2.CfnInstance.EbsProperty(
                      delete_on_termination=False, encrypted=True, volume_size=env_vars[&quot;deployment_user_volume1&quot;]
                  ),
              ),
              ec2.CfnInstance.BlockDeviceMappingProperty(
                  device_name=&quot;/dev/sdc&quot;,
                  ebs=ec2.CfnInstance.EbsProperty(
                      delete_on_termination=False, encrypted=True, volume_size=env_vars[&quot;deployment_user_volume2&quot;]
                  ),
              )
          ],
          ebs_optimized=True,
          image_id=env_vars[&quot;ami_id_rhel&quot;],
          instance_type=env_vars[&quot;instance_type_dep&quot;],
          key_name=env_vars[&quot;key_pair_name&quot;],
          security_group_ids=[ec2_sg.security_group_id],
          subnet_id=env_vars[&quot;general_subnet2&quot;]
      )
  
     
      myvpc = ec2.Vpc.from_lookup(
          self,
          &quot;SecurityVpc&quot;,
          vpc_name=env_vars[&quot;vpc_name&quot;],
      )
  
      subnet_id_1 = env_vars[&quot;general_subnet1&quot;]
      subnet_id_2 = env_vars[&quot;general_subnet2&quot;]
  
      # Import the corresponding subnets into CDK
      subnet_1 = ec2.Subnet.from_subnet_id(
          self, id=&quot;elbsubnet1&quot;, subnet_id=subnet_id_1
      )
      subnet_2 = ec2.Subnet.from_subnet_id(
          self, id=&quot;elbsubnet2&quot;, subnet_id=subnet_id_2
      )
  
      ### NLB creation in security account
      nlb_security_deployment_server = elb.NetworkLoadBalancer(
          self,
          env_vars[&quot;nlb_name&quot;],
          cross_zone_enabled= True,
          vpc=myvpc,
          vpc_subnets=ec2.SubnetSelection(subnets=[subnet_1, subnet_2]),
          internet_facing=False,
          load_balancer_name=env_vars[&quot;nlb_name&quot;],
      )
  
      # Add a listener on a particular port
      listener_security_deployment_server1 = nlb_security_deployment_server.add_listener(
          &quot;listener1&quot;, port=LISTENER_PORT1
      )
      listener_security_deployment_server2 = nlb_security_deployment_server.add_listener(
          &quot;listener2&quot;, port=LISTENER_PORT2
      )
  
      listener_security_deployment_server1.add_targets(
          &quot;tg1-deployment-server-security&quot;,
          port=LISTENER_PORT1,
          target_group_name=&quot;tg1-deployment-server-security&quot;,
          targets=[
              targets.InstanceTarget(
                  #NEED HELP HERE
                  instance=deployment01,
                  port=TARGET_PORT1, 
              ),
              targets.InstanceTarget(
                  instance=deployment01,
                  port=TARGET_PORT2,
              )
          ],
      )
      listener_security_deployment_server2.add_targets(
          &quot;tg2-deployment-server-security&quot;,
          port=LISTENER_PORT2,
          target_group_name=&quot;tg2-deployment-server-security&quot;,
          targets=[
              targets.InstanceTarget(
                  instance=deployment_02,
                  port=TARGET_PORT1,
              ),
              targets.InstanceTarget(
                  instance=deployment_02,
                  port=TARGET_PORT2,
              )
          ],
      )
  
      core.Tags.of(deployment_01).add(&quot;Name&quot;, deployment_01.node.path)
      core.Tags.of(deployment_02).add(&quot;Name&quot;, deployment_02.node.path)
  
      ### Common Tagging
      core.Tags.of(self).add(&quot;Project&quot;, PROJECT)
      core.Tags.of(self).add(&quot;Environment&quot;, env_vars[&quot;env_name&quot;])
      core.Tags.of(self).add(&quot;CostCenter&quot;, COST_CENTER)
      core.Tags.of(self).add(&quot;LineOfBusiness&quot;, LINE_OF_BUSINESS)
      core.Tags.of(self).add(&quot;Owner&quot;, OWNER)
  </code></pre>