python 创建aws slb
#!/usr/bin/env python # _*_coding:utf-8_*_ import boto3 import json import sys class Elbv2(): def __init__(self,**kwargs): self.app = kwargs['app'] self.env = kwargs['env'] self.port = kwargs['port'] self.vpcid = kwargs['vpcid'] self.client_elb = boto3.client('elbv2') def create_elb(self,**kwargs): num = kwargs['num'] elbtype = kwargs['elbtype'] scheme = kwargs['scheme'] #seg = kwargs['securitygroups'] subnets = kwargs['subnets'] iptype = kwargs['iptype'] response = self.client_elb.create_load_balancer( Name='elb-{0}-{1}-{2}'.format(self.env,self.app,num), Subnets=subnets, Scheme=scheme, Type=elbtype, IpAddressType=iptype ) return response def create_elb_trgroup(self,**kwargs): num = kwargs['num'] protocol = kwargs['Protocol'] elbtype = kwargs['elbtype'] targettype = kwargs['TargetType'] if elbtype == "network": response = self.client_elb.create_target_group( Name='trg-{0}-{1}-{2}'.format(self.env,self.app,num), Protocol=protocol, Port=self.port, VpcId=self.vpcid, TargetType=targettype ) return response else: response = self.client_elb.create_target_group( Name='trg-{0}-{1}-{2}'.format(self.env,self.app,num), Protocol=protocol, Port=self.port, VpcId=self.vpcid, Matcher={ 'HttpCode': '200-399' }, TargetType=targettype ) return response def create_elb_listener(self,**kwargs): elbarn = kwargs['LoadBalancerArn'] protocol = kwargs['Protocol'] tgarn = kwargs['TargetGroupArn'] actiontype = kwargs['Actiontype'] if protocol == 'HTTP': response = self.client_elb.create_listener( DefaultActions=[ { 'TargetGroupArn': tgarn, 'Type': actiontype, }, ], LoadBalancerArn=elbarn, Port=80, Protocol='HTTP', ) return response elif protocol == 'TLS': CAarn = kwargs['CertificateArn'] response = self.client_elb.create_listener( Certificates=[ { 'CertificateArn': CAarn, }, ], DefaultActions=[ { 'TargetGroupArn': tgarn, 'Type': actiontype, }, ], LoadBalancerArn=elbarn, Port=443, Protocol='HTTPS', ) return response else: response = self.client_elb.create_listener( DefaultActions=[ { 'TargetGroupArn': tgarn, 'Type': actiontype, }, ], LoadBalancerArn=elbarn, Port=self.port, Protocol='TCP', ) return response def register_targets(self,Instanceid1,Instanceid2,**targetarg): TGarn = targetarg['TGarn'] Port = targetarg['Port'] response = self.client_elb.register_targets( TargetGroupArn=TGarn, Targets=[ { 'Id': Instanceid1, 'Port': Port, }, { 'Id': Instanceid2, 'Port': Port, }, ] ) return response def delete_elb(self,elbarn): response = self.client_elb.delete_load_balancer( LoadBalancerArn=elbarn ) return response def delete_trgroup(self,tgarn): response = self.client_elb.delete_target_group( TargetGroupArn=tgarn ) return response def delete_listen(self,lisarn): response = self.client_elb.delete_listener( ListenerArn=lisarn ) return response if __name__ == '__main__': #client_elb = boto3.client('elbv2') instancearg = {"app":"mintest","env":"ops","port":8009,"vpcid":"vpc-03fedcxxxxxxx"} newelb = Elbv2(**instancearg) #elb_arg = load_f_dict['elb'] elb_arg = {"num":"001","elbtype":"network","scheme":"internet-facing","iptype": "ipv4","subnets":["xxx","xxxx"]} newelb_create = newelb.create_elb(**elb_arg) print(newelb_create) #trgoup_arg = load_f_dict['elbtrg'] trgoup_arg = {"vpcid":"vpc-03fedca7bfxxxxx","num":"001","elbtype":"network","Protocol":"TCP","TargetType":"instance"} newelb_trgoup = newelb.create_elb_trgroup(**trgoup_arg) print(newelb_trgoup) elbarn = newelb_create['LoadBalancers'][0]['LoadBalancerArn'] #elbarn = 'arn:aws:elasticloadbalxxxxxxx......' tgarn = newelb_trgoup['TargetGroups'][0]['TargetGroupArn'] #tgarn = 'arn:aws:elasticloadbxxxxxxx........' caarn = "arn:aws:acm:ap-southeast-1:41846537xxxxxxxxx.........." listener_arg = {"Protocol": "TCP", "LoadBalancerArn": elbarn, "TargetGroupArn": tgarn, "Actiontype": "forward", "CertificateArn": caarn} #listener_arg = load_f_dict['elblistener'] newelb_listener = newelb.create_elb_listener(**listener_arg) print(newelb_listener)