要使用AWS负载均衡器来自终端点,你可以按照以下步骤操作:
import boto3
# 创建负载均衡器
elbv2 = boto3.client('elbv2')
response = elbv2.create_load_balancer(
Name='my-load-balancer',
Subnets=['subnet-12345678', 'subnet-23456789'],
SecurityGroups=['sg-12345678'],
Scheme='internet-facing',
Type='application',
IpAddressType='ipv4',
Tags=[
{
'Key': 'Name',
'Value': 'my-load-balancer'
},
]
)
load_balancer_arn = response['LoadBalancers'][0]['LoadBalancerArn']
# 创建终端点服务
response = elbv2.create_endpoint_group(
ListenerArn=load_balancer_arn,
EndpointGroupRegion='us-west-2',
EndpointConfigurations=[
{
'EndpointId': 'vpce-12345678',
'Weight': 1
},
],
TrafficDialPercentage=100,
HealthCheckPort='traffic-port',
HealthCheckProtocol='TCP',
HealthCheckEnabled=True,
)
endpoint_group_arn = response['EndpointGroupArn']
# 更新负载均衡器的终端点配置
response = elbv2.modify_load_balancer_attributes(
LoadBalancerArn=load_balancer_arn,
Attributes=[
{
'Key': 'endpoint.enabled',
'Value': 'true'
},
{
'Key': 'endpoint.endpointGroupArn',
'Value': endpoint_group_arn
},
]
)
以上代码示例使用Python的boto3库来调用AWS负载均衡器的API。在使用之前,请确保你已经安装了boto3库并配置了正确的AWS凭证。
注意:上述代码示例仅供参考,你需要根据你的实际情况进行适当的调整和修改。