Skip to main content

Manage ELB using Python Boto3

 Managing AWS ELB automations using Boto3 Python scripts. 

What is ELB?

ELB automatically distributes incoming network traffic (may be HTTP/HTTPS requests) across multiple EC2 instances running in different Availability Zones.

ELB to EC2 instances



 In this post I will experimenting on the following: 
  • Create ELB 
  • Apply security group
  • Create Health check
  • Register instances with ELB  
  • Delete ELB services

1. How to Create ELB using Python Boto3 automations?

The create_load_blancer method is used to create a Load Balancer.
object: client refers to elb 
method: create_load_blancer() which requires LoadBalancerName, Listeners that is again a dictionary of Protocol,LoadBalancerPort,InstanceProtocol, InstancePort, And another parameter 'AvailabilityZone' is important that refer to region on which you ar working on.
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_balancer
import 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_balancer
import 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.

object: client refer to elb service
method: delete_loadbalancer()
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. 

 Refrences: https://unbiased-coder.com/boto3-load-balancer-guide/

Comments

Popular posts from this blog

Python Interview Questions -Coding Snipets

 This post is dedicated for all DevOps Engineer, Software Engineers who are preparing for Coding Interviews,  Overview of Coding Interviews Most Companies looking for People with minimum Coding knowledge. In a coding interview, you will be given a small problem to solve within 10 - 20 minutes online screen or in-person on their system. In the question, you might be having some part of the code framed and you might be asked to write a snippet of code in between. You need to understand the code comments and proceed to build the expected snippet of code. Bigger companies look for the General purpose questions, where small companies look for specific questions. The General questions would be like this: Determine if the given word is a palindrome or not. (Example madam) Determine given number is prime or not. How to prepare for a coding interview? Now we have the flexibility to choose the programming language on which you are comfortable.  In general DevOps Infra guys will be ...

Python Sets and Dictionaries

Python Sets Python Maps examples A mapping object maps hashable values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the dictionary. Dictionaries consist of pairs (called items) of keys and their corresponding values. Dictionaries can be created by placing a comma-separated list of key: value pairs within curly braces Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples. Python Set Operations and methods Python Set methods continueed >>> SET={'new','old','list','new'} >>> SET set(['new', 'old', 'list']) Lets play with set operations, it is more like school math here... >>> s={1,2,3,4} >>> k={3,4,5,6} >>> len(s) 4 >>> len(k) 4 >>> 1 in s True >>...

Program Controls and loops in Python

Python Programming controls The flow of program will be controlled with conditional statements where this flow  if condition if-else condition if-elif condition while loop while - else loop for loop for with if- else loop if condition Here I would like to work on program control statements, wherein relate some operating system functionalities so that, this would give some basic prototype for the sys admin scripts. #!/usr/bin/python # This illustrate if condition # Filename : ifex.py import os d=os.listdir('/home/pavanbsd/pybin') print d f='ifex.py' if f in d: print "File exists" else: print "Not found..." if-elif-else ladder No worries, coming soon... When you need the task that need to be done repeatedly then your choice is using loops. Python provides two simple loop constructs easy to use! More powerful then other scripting compare to, it has 'else' block. while loop Understand the power of while loop,...