如何在 Boto3 中正确创建和附加 ELB [英] How to create and attach a ELB properly in Boto3
问题描述
我是亚马逊 Boto3
API 的新手.我创建了如下所示的示例架构的基本图表,其中包含一个 ELB、4 个实例、2 个子网和 2 个不同可用区中的 2 个目标组(每个目标组中有 2 个实例).
I'm new to Amazon's Boto3
API. I created a basic diagram of my sample architecture shown below, with an ELB, 4 instances, 2 subnets and and 2 target groups in 2 different Availability Zones (2 instances in each target group).
我知道如何创建 EC2 实例、目标组、子网和 ELB.但是我不清楚要使用什么 ELB 函数.
I know how to create an EC2 instance, a target group, subnets, and an ELB. But what ELB functions to use, is not clear to me.
如何将 ELB 连接到其他组件?基本上,如何将实例添加到 ELB?我不确定现在需要哪些后续步骤和功能.
How I can attach the ELB to other components? Basically, how to add instances to the ELB? I'm not sure what next steps and functions are needed now.
到目前为止,这是我的简单代码:
Here is my simple code so far:
def create_load_balancer(load_balancer_name, vpcid, subnets, security_group):
command = "aws elbv2 create-load-balancer --name " + load_balancer_name + " --subnets " + subnets + " --security-groups " + security_group+" --scheme internet-facing --type application"
response = os.popen(command).read()
// ....created 4 instances, subnets, and security groups ...
//now ELB:
#Load Balancer
elb = boto3.client('elbv2')
elb.create_target_group( Name='boto3-target-a', Protocol='HTTP', Port=80, VpcId=vpc.id)
elb.create_target_group( Name='boto3-target-b', Protocol='HTTP', Port=80, VpcId=vpc.id)
response = elb.create_load_balancer(Name="elb_boto3", Listeners=[ { 'Protocol': 'tcp', 'LoadBalancerPort': 80, 'InstanceProtocol': 'tcp', 'InstancePort': 80, 'SSLCertificateId': 'string'}, ], Subnets=[subnet1,subnet2], SecurityGroups=[sec_group], Scheme='internet-facing', Type='application')
推荐答案
使用 register_targets()
将实例附加到目标组:
Use register_targets()
to attach instances to a Target Group:
response = client.register_targets(
TargetGroupArn='arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067',
Targets=[
{
'Id': 'i-80c8dd94',
},
{
'Id': 'i-ceddcd4d',
},
],
)
使用create_listener()
将目标组与负载均衡器关联:
Use create_listener()
to associate a Target Group with a Load Balancer:
response = client.create_listener(
DefaultActions=[
{
'TargetGroupArn': 'arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067',
'Type': 'forward',
},
],
LoadBalancerArn='arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188',
Port=80,
Protocol='HTTP',
)
来自 <代码>create_target_group() 文档:
From the create_target_group()
documentation:
要向目标组注册目标,请使用 RegisterTargets .要更新目标组的运行状况检查设置,请使用 ModifyTargetGroup .要监控目标组中目标的运行状况,请使用 DescribeTargetHealth .
To register targets with the target group, use RegisterTargets . To update the health check settings for the target group, use ModifyTargetGroup . To monitor the health of targets in the target group, use DescribeTargetHealth .
要将流量路由到目标组中的目标,请使用 CreateListener 或 CreateRule 在操作中指定目标组.
To route traffic to the targets in a target group, specify the target group in an action using CreateListener or CreateRule .
所以,最好的创建顺序是:
So, the best order of creation is:
- 创建负载均衡器
- 创建目标组
- 创建侦听器以将目标组链接到 ELB
- 将实例注册到目标组
这篇关于如何在 Boto3 中正确创建和附加 ELB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!