如何在 Boto3 中正确创建和附加 ELB
How to create and attach a ELB properly in Boto3
我是亚马逊 Boto3
API 的新手。我创建了一个示例架构的基本图,如下所示,在 2 个不同的可用区中有一个 ELB、4 个实例、2 个子网和 2 个目标组(每个目标组中有 2 个实例)。
我知道如何创建 EC2 实例、目标组、子网和 ELB。但是我不清楚要使用什么 ELB 函数。
如何将 ELB 附加到其他组件?基本上,如何将实例添加到 ELB?我不确定现在需要哪些后续步骤和功能。
到目前为止,这是我的简单代码:
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()
将实例附加到目标组:
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()
将目标组与负载均衡器相关联:
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()
documentation:
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 .
To route traffic to the targets in a target group, specify the target group in an action using CreateListener or CreateRule .
所以,最好的创建顺序是:
- 创建负载均衡器
- 创建目标群体
- 为 ELB link 目标组创建监听器
- 将实例注册到目标组
我是亚马逊 Boto3
API 的新手。我创建了一个示例架构的基本图,如下所示,在 2 个不同的可用区中有一个 ELB、4 个实例、2 个子网和 2 个目标组(每个目标组中有 2 个实例)。
我知道如何创建 EC2 实例、目标组、子网和 ELB。但是我不清楚要使用什么 ELB 函数。
如何将 ELB 附加到其他组件?基本上,如何将实例添加到 ELB?我不确定现在需要哪些后续步骤和功能。
到目前为止,这是我的简单代码:
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()
将实例附加到目标组:
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()
将目标组与负载均衡器相关联:
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()
documentation:
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 .
To route traffic to the targets in a target group, specify the target group in an action using CreateListener or CreateRule .
所以,最好的创建顺序是:
- 创建负载均衡器
- 创建目标群体
- 为 ELB link 目标组创建监听器
- 将实例注册到目标组