Hey Automation Specialist!
Welcome to Python-Orientation blog, In this article we are going to explore the possible functions what we can use for managing AWS ELB automations using Boto3 Python scripts.
AWS provides us 4 types of load balancers - CLB Classic Load balancer supports HTTP, HTTPS, TCP, TLS which is older and soon sun set. ALB Application Load Balancer which supports HTTP, HTTPS requests. Introduced in 2016 this V2 version Elastic Load Balancer. And in the 2017 AWS introduced Network Load Balancer NLB, it is designed to support Transport Layer protocols - TCP, UDP and TLS requests. And the latest one is more focused on Security Threat Monitoring - which lays on Network Layer and it support routing traffic to 3rd party Threat monitoring service which can be isolated from the Application services network. It works as Gateway for the external traffic so it is known as Gateway Load balancer.
What is ELB?
| ELB to EC2 instances |
- Create ELB
- Apply security group
- Create Health check
- Register instances with ELB
- Delete ELB services
1. How to Create ELB using Python Boto3 automations?
import boto3
elb_client=boto3.client('elb')
response = elb_client.create_load_blancer(
LoadBalancerName='vybhava_lb',
Listeners=[{'Protocol': 'HTTP', 'LoadBalancerPort':80, 'InstanceProtocol':'HTTP', 'InstancePort': 80},
AvailabilityZone=['ap-south-1a']
)
print(response)
print(response['DNSName'])
2. Apply security group on ELB using Boto3
object: client refers to elb method: apply_security_groups_to_load_balancer() here we can use existing security group(that is created during EC2 instance creation) can be associated with the ELB as well.import boto3
elb_client=boto3.client('elb')
response=elb_client.apply_security_groups_to_load_balancer(
LoadBalancerName='vybhava_lb',
SecurityGroups='sg-vybhava_lb']
)
print(response)
3. How to create Health check?
object: client refers to elb method: configure_health_check() without healthcheck there is not strong bonding between ELB and EC2 instances. This will validate to check weather the incoming request should be send to the instance or not based on the parameters here we provide.import boto3
elb_client=boto3.client('elb')
health_check_resp=elb_client.configure_health_check(
LoadBalancerName = 'vybhava_lb',
HealthCheck={
'Target':'TCP:22',
'Interval':10,
'Timeout':5,
'UnhealthyThreshold':5,
'HealthyThreshould': 5
}
print(health_check_resp)
The health_check_resp object output c should have 200. Http code for success.4. How to register instance with ELB services?
EC2 Instance(s) will be attached to ELB object: client method: register_instances_with_load_balancerimport boto3
elb_client=boto3.client('elb')
instanceid=input('Please enter instance id to attach to ELB:')
attachinstance=elb_client.register_instances_with_load_balancer(
LoadBalancerName='vybhava_lb',
Instances=[ {
'InstanceId': instanceid,
} ]
)
print(attachinstance)
Observe that 'Instance Count' value changes according to attached number of instances. In the bottom tabs look for 'Instances'.
5. How to deregister instance from ELB using Boto3?
De-register EC2 instances from ELB, its reverse process what we did for registration.The object: client method: register_instances_with_load_balancerimport boto3
elb_client=boto3.client('elb')
deattachinstance=elb_client.deregister_instances_with_load_balancer(
LoadBalancerName='vybhava_lb',
Instances=[ {
'InstanceId': instanceid,
} ]
)
print(deattachinstance)
6. How to delete ELB service using Boto3?
The removal of elb can be performed on elb_client, to delete ELB we need to use param for delete_loadbalancer is existing elb services name.import boto3
elb_client=boto3.client('elb')
response=elb_client.delete_loadbalancer(LoadBalancerName='vybhava_lb')
print(response)
Goto the aws console and confirm that no ELB in that region where we did all the above.
Comments
Post a Comment