我们有2个代理服务器,我们希望在NLB上处于前端。我正在使用AWS-CDK(python)通过CloudFormation创建。我正在尝试将ec2注册为instanceid或实例documentation link。我希望有人可以提供示例链接或帮助修复以下代码。
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)
答案 0 :(得分:0)
这是有效的更新代码。它由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
)
]
)