amazon-web-services - 使用现有 EC2 实例和 AWS-CDK 创建网络负载均衡器 (NLB)
问题描述
我们有 2 个代理服务器,我们希望将其作为 NLB 的前端。我正在使用 AWS-CDK (python) 通过 CloudFormation 创建。我正在尝试将 ec2 注册为 instanceid 或 instance 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)
解决方案
这是有效的更新代码。它由 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
)
]
)
推荐阅读
- mysql - MySQL:如何访问另一个表中的数据?
- c++ - 序列化 QEvent 以通过 QTcpSocket 连接发送
- python - Python 压缩参数
- spring - BeanCreationException,NoSuchBeanDefinitionException 在功能分支中找到相同的服务 jar,但在主分支代码中没有
- ios - 您可以让应用程序在打开时显示本地通知吗?
- android - 如何使用 Android Java Camera View 模块创建自定义 React Native 组件?
- html - 如何使导航栏的每个选项卡均等地适合屏幕
- flutter - 我对颤振中的“awesome_dialog”包有问题
- javascript - 选择多个过滤器,仅显示具有共同特征的标记
- excel - 在多个工作表中使用通用下拉菜单来更改单个单元格