如何在Boto3中正确创建和附加ELB [英] How to create and attach a ELB properly in Boto3
问题描述
我是Amazon 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')
推荐答案
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',
},
],
)
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',
)
要向目标组注册目标,请使用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屋!