def _create_vpc(self): """ create the VPC and update the network configuration with the ipv6 detail """ # for create we only need to know the CIDR block first vpc_cidr = get_cidr(dc_cfg=self.net_config) self.vpc_conn = AwsVPC(aws_conn=self.aws_conn, tag=self.tag, cidr=vpc_cidr) if not self.vpc_conn: print('error AwsVPC\n') sys.exit(-1) # there should be no ID yet if self.vpc_conn.get_vpc_id() is not None: print('There is already a VPC with the given tag: {}, aborted.'. format(self.tag)) sys.exit(-1) print('\t--< Start creating the VPC: {} >--'.format(strftime("%c"))) if self.vpc_conn.create() is None: print('error creating the VPC\n') sys.exit(-1) # get the vpc id and ipv6 details and update the net_config dict self.vpc_id = self.vpc_conn.get_vpc_id() networkv6, cidrv6 = self.vpc_conn.get_vpc_cidr(ip_type='v6', split_cidr=True) self.net_config.update(set_network_config(dc_cfg=self.net_config, \ dc_cidr_v6=cidrv6, dc_network_v6=networkv6)) # get the main route table self.vpc_route_table = self.vpc_conn.get_main_route_table() if self.vpc_route_table is None: print('error getting main route of the VPC\n') sys.exit(-1)
def main(): """ main """ my_logfile = 'logs/awsbuild.log' my_region = 'us-east-1' #my_vpc = 'vpc-xxx' my_tag = 'momo-us-east-1' # setup logging log_formatter = logging.Formatter( "%(asctime)s %(filename)s %(name)s %(levelname)s %(message)s") root_logger = logging.getLogger() file_handler = logging.FileHandler(my_logfile) file_handler.setFormatter(log_formatter) root_logger.addHandler(file_handler) console_handler = logging.StreamHandler() console_handler.setFormatter(log_formatter) root_logger.addHandler(console_handler) config = AwsConfig(cfgdir='configs',\ cfgfile='target_group.yaml',\ cfgkey='target_groups') conn = AwsConnector(credentials=config.settings['aws_cfg']['credentials'], region=my_region) aws_conn = conn.get_all_conn() if not aws_conn: print('error AwsConnector\n') sys.exit(-1) vpc_conn = AwsVPC(aws_conn=aws_conn, tag=my_tag) if not vpc_conn: print('error AwsVPC\n') sys.exit(-1) vpc_id = vpc_conn.get_vpc_id() target_grp_conn = AwsTargetGroup(aws_conn=aws_conn, target_group=config.settings['target_groups'], \ vpc_id=vpc_id, tag='tau-dev' \ ) if not target_grp_conn: print('error AwsTargetGroup\n') sys.exit(-1) target_grp_conn.create()
def main(): """ main """ my_logfile = 'logs/awsbuild.log' my_region = 'us-east-1' #my_vpc = 'vpc-xxx' my_tag = 'momo-us-east-1' # setup logging log_formatter = logging.Formatter("%(asctime)s %(filename)s %(name)s %(levelname)s %(message)s") root_logger = logging.getLogger() file_handler = logging.FileHandler(my_logfile) file_handler.setFormatter(log_formatter) root_logger.addHandler(file_handler) console_handler = logging.StreamHandler() console_handler.setFormatter(log_formatter) root_logger.addHandler(console_handler) config = AwsConfig(cfgdir='configs',\ cfgfile='ec2.yaml') conn = AwsConnector(credentials=config.settings['aws_cfg']['credentials'], region=my_region) aws_conn = conn.get_all_conn() if not aws_conn: print('error AwsConnector\n') sys.exit(-1) vpc_conn = AwsVPC(aws_conn=aws_conn, tag=my_tag) if not vpc_conn: print('error AwsVPC\n') sys.exit(-1) vpc_id = vpc_conn.get_vpc_id() vpc_conn.set_vpc_resource(vpc_id=vpc_id) dc_ipv6 = config.settings['dc_cfg'][my_region]['dc_ipv6'] networkv6, cidrv6 = vpc_conn.get_vpc_cidr(ip_type='v6', split_cidr=True) net_config = set_network_config(dc_cfg=config.settings['dc_cfg'][my_region]) if not net_config: print('error set_network_config\n') sys.exit(-1) # set the network topoloy first (subnet size) net_config.update(config.settings['dc_cfg'][my_region]) # add the config from the configuration file and the VPC ipv6 settings net_config.update(set_network_config(dc_cfg=net_config, \ dc_cidr_v6=cidrv6, dc_network_v6=networkv6)) sec_conn = AwsSecGroup(aws_conn=aws_conn, cfgs=config.get_settings(), \ dc_cfg=net_config, vpc_id=vpc_id, ipv6=dc_ipv6 \ ) if not sec_conn: print('error\n') sys.exit(-1) print('sec -> {}\n'.format(sec_conn.destroy()))
def main(): """ main """ my_logfile = 'logs/awsbuild.log' my_region = 'us-east-1' my_vpc = 'vpc-xxx' my_tag = 'momo-us-east-1' # setup logging log_formatter = logging.Formatter("%(asctime)s %(filename)s %(name)s %(levelname)s %(message)s") root_logger = logging.getLogger() file_handler = logging.FileHandler(my_logfile) file_handler.setFormatter(log_formatter) root_logger.addHandler(file_handler) console_handler = logging.StreamHandler() console_handler.setFormatter(log_formatter) root_logger.addHandler(console_handler) config = AwsConfig(cfgdir='configs',\ cfgfile='auto_scaling_group.yaml',\ cfgkey='autoscale_group') conn = AwsConnector(credentials=config.settings['aws_cfg']['credentials'], region=my_region) aws_conn = conn.get_all_conn() if not aws_conn: print('error AwsConnector\n') sys.exit(-1) autoscale_conn = aws_conn['autoscaling'] if not autoscale_conn: print('error AwsConnector for autoscaling\n') sys.exit(-1) vpc_conn = AwsVPC(aws_conn=aws_conn, tag=my_tag) if not vpc_conn: print('error AwsVPC\n') sys.exit(-1) #vpc_id = vpc_conn.get_vpc_id() vpc_conn.set_vpc_resource(vpc_id=my_vpc) net_config = set_network_config(dc_cfg=config.settings['dc_cfg'][my_region]) if not net_config: print('error set_network_config\n') sys.exit(-1) groups_data = config.settings['autoscale_group'] for autoscale_group in groups_data: temp_tag_dict = {} tags_list = [] group_data = groups_data[autoscale_group] # create tag list for tag in group_data['Tags']: # set the working dictonary and overwrite or skip as needd temp_tag_dict = {'Key': tag, 'Value': group_data['Tags'][tag], 'PropagateAtLaunch': True} # we leave hostid empty if set to none if tag == 'hostid' and group_data['Tags'][tag] == 'none': temp_tag_dict = {'Key': tag, 'Value': '', 'PropagateAtLaunch': True} tags_list.append(temp_tag_dict) try: autoscale_conn.create_auto_scaling_group( AutoScalingGroupName=autoscale_group, LaunchTemplate={ 'LaunchTemplateName': group_data['LaunchTemplate']['LaunchTemplateName'], 'Version': group_data['LaunchTemplate']['Version'] }, MinSize=int(group_data['MinSize']), MaxSize=int(group_data['MaxSize']), DesiredCapacity=int(group_data['DesiredCapacity']), DefaultCooldown=int(group_data['DefaultCooldown']), AvailabilityZones=group_data['AvailabilityZones'], HealthCheckGracePeriod=group_data['HealthCheckGracePeriod'], VPCZoneIdentifier=','.join(group_data['VPCZoneIdentifier']), TerminationPolicies=[group_data['TerminationPolicies']], Tags=tags_list ) print('Autoscale Group {} created'.format(autoscale_group)) except Exception as err: critical('Unable to create launch group {}, error {}'.format(autoscale_group, err)) sys.exit(0)
class BaoCreate(): """ Class to create or delete a VPC and it components """ def __init__(self, **kwargs): """ initial the object """ self.aws_conn = kwargs.get('aws_conn', {}) self.net_config = kwargs.get('net_config', {}) self.tag = kwargs.get('tag', {}) self.ipv6 = kwargs.get('ipv6', False) self.vpc_conn = None self.vpc_id = None self.vpc_route_table = None self.subnet_conn = None self.subnets_fe_id = [] self.subnets_be_id = {} self.int_gate_id = None self.nat_gate_info = {} def _create_vpc(self): """ create the VPC and update the network configuration with the ipv6 detail """ # for create we only need to know the CIDR block first vpc_cidr = get_cidr(dc_cfg=self.net_config) self.vpc_conn = AwsVPC(aws_conn=self.aws_conn, tag=self.tag, cidr=vpc_cidr) if not self.vpc_conn: print('error AwsVPC\n') sys.exit(-1) # there should be no ID yet if self.vpc_conn.get_vpc_id() is not None: print('There is already a VPC with the given tag: {}, aborted.'. format(self.tag)) sys.exit(-1) print('\t--< Start creating the VPC: {} >--'.format(strftime("%c"))) if self.vpc_conn.create() is None: print('error creating the VPC\n') sys.exit(-1) # get the vpc id and ipv6 details and update the net_config dict self.vpc_id = self.vpc_conn.get_vpc_id() networkv6, cidrv6 = self.vpc_conn.get_vpc_cidr(ip_type='v6', split_cidr=True) self.net_config.update(set_network_config(dc_cfg=self.net_config, \ dc_cidr_v6=cidrv6, dc_network_v6=networkv6)) # get the main route table self.vpc_route_table = self.vpc_conn.get_main_route_table() if self.vpc_route_table is None: print('error getting main route of the VPC\n') sys.exit(-1) def _create_subnets(self): """ create the subnets and keep list of their id """ print('\t--< create the VPC\'s subnet >--') self.subnet_conn = AwsSubnet(aws_conn=self.aws_conn, tag=self.tag, vpc_id=self.vpc_id) if not self.subnet_conn: print('error AwsSubnet\n') sys.exit(-1) subnet_position = 0 for _ in self.net_config['vpc_fe_subnets']: subnet_id = self.subnet_conn.create_subnet( zone_name=self.net_config['dc_zones_names'][subnet_position], \ subnet_cidr=self.net_config['vpc_fe_subnets'][subnet_position], \ subnet_cidrv6=self.net_config['vpc_fe_subnets_v6'][subnet_position], \ subnet_type='fe', ipv6=self.ipv6 ) if subnet_id is None: sys.exit(-1) subnet_position += 1 self.subnets_fe_id.append(subnet_id) subnet_position = 0 for _ in self.net_config['vpc_be_subnets']: subnet_id = self.subnet_conn.create_subnet( zone_name=self.net_config['dc_zones_names'][subnet_position], \ subnet_cidr=self.net_config['vpc_be_subnets'][subnet_position], \ subnet_cidrv6=self.net_config['vpc_be_subnets_v6'][subnet_position], \ subnet_type='be', ipv6=self.ipv6 ) if subnet_id is None: sys.exit(-1) self.subnets_be_id[self.net_config['dc_zones_names'] [subnet_position]] = { 'subnet_id': subnet_id } subnet_position += 1 def _create_internet_gateway(self): """ create the internet gateway and attach to VPC """ print('\t--< create the internet gateway and attach to the VPC >--') int_gate_conn = AwsInternetGateway(aws_conn=self.aws_conn, tag=self.tag, vpc_id=self.vpc_id) if not int_gate_conn: print('error AwsInternetGateway\n') sys.exit(-1) self.int_gate_id = int_gate_conn.create_internet_gateway() if self.int_gate_id is None: sys.exit(-1) result = int_gate_conn.attach_internet_gateway() if result is None: sys.exit(-1) def _create_nat_gateways(self): """ create the NAT gateways and attach one to each fe-subnet with it own EIP """ # get the subnet ids subnet_data_fe, _, _ = self.subnet_conn.get_subnet_info(fe_subnet=self.net_config['vpc_fe_subnets'], \ be_subnet=self.net_config['vpc_be_subnets']) print( '\t--< create the NAT gateway and attach to each fe-subnet with it own EIP >--' ) nat_gate_conn = AwsNatGateway(aws_conn=self.aws_conn, tag=self.tag) if not nat_gate_conn: print('error nat_gate_conn\n') sys.exit(-1) eip_conn = AwsEIP(aws_conn=self.aws_conn) if not eip_conn: print('error AwsEIP\n') sys.exit(-1) for subnet_id in subnet_data_fe: zone_name = subnet_data_fe[subnet_id]['zone_name'] eip_id = eip_conn.create_eip(tag=self.tag + '-' + 'nat_gate' + '-' + zone_name) if eip_id is None: sys.exit(-1) nat_gateway_id = nat_gate_conn.create_nat_gateway(eip_id=eip_id, subnet_id=subnet_id, \ tag=self.tag + '-' + zone_name) if nat_gateway_id is None: sys.exit(-1) self.nat_gate_info[zone_name] = nat_gateway_id def _create_routes(self): """ create the route for the fe-subnets create the route for the be-subnets, each subnet get it own route and own NAT gateway """ print('\t--< create the route for the fe-subnets >--') route_conn = AwsRouteTable(aws_conn=self.aws_conn, vpc_id=self.vpc_id, tag=self.tag) if not route_conn: print('error AwsRouteTable\n') sys.exit(-1) if route_conn.create_fe_route_table(subnets_id=self.subnets_fe_id, \ internet_gateway=self.int_gate_id, main_route_table=self.vpc_route_table) is False: sys.exit(1) print( '\t--< create the route for the be-subnets, 1 route per subnet with it own NAT gateway >--' ) for subnet in self.subnets_be_id: zone_name = subnet subnet_id = self.subnets_be_id[zone_name]['subnet_id'] nat_gate_id = self.nat_gate_info[zone_name] if route_conn.create_be_route_table(subnet_id=subnet_id, \ nat_gateway=nat_gate_id, zone_name=zone_name) is False: sys.exit(1) def create(self): """ create tge VPC and is components """ # start the creation process self._create_vpc() self._create_subnets() self._create_internet_gateway() self._create_nat_gateways() self._create_routes() def get_vpc_detail(self): """ get the new vpc detail """ vpc_detail = {} vpc_detail['vpc_id'] = self.vpc_id vpc_detail['subnets_fe'] = self.subnets_fe_id vpc_detail['subnets_be'] = self.subnets_be_id vpc_detail['vpc_int_gate'] = self.int_gate_id vpc_detail['vpc_nat_gate'] = self.nat_gate_info vpc_detail['vpc_route_table'] = self.vpc_route_table return vpc_detail
def main(): """ main """ my_logfile = 'logs/awsbuild.log' my_region = 'us-east-1' my_vpc = 'vpc-xxx' my_tag = 'momo-us-east-1' # setup logging log_formatter = logging.Formatter( "%(asctime)s %(filename)s %(name)s %(levelname)s %(message)s") root_logger = logging.getLogger() file_handler = logging.FileHandler(my_logfile) file_handler.setFormatter(log_formatter) root_logger.addHandler(file_handler) console_handler = logging.StreamHandler() console_handler.setFormatter(log_formatter) root_logger.addHandler(console_handler) config = AwsConfig(cfgdir='configs',\ cfgfile='launch_configuration.yaml',\ cfgkey='autoscale') conn = AwsConnector(credentials=config.settings['aws_cfg']['credentials'], region=my_region) aws_conn = conn.get_all_conn() if not aws_conn: print('error AwsConnector\n') sys.exit(-1) autoscale_conn = aws_conn['autoscaling'] if not autoscale_conn: print('error AwsConnector for autoscaling\n') sys.exit(-1) vpc_conn = AwsVPC(aws_conn=aws_conn, tag=my_tag) if not vpc_conn: print('error AwsVPC\n') sys.exit(-1) #vpc_id = vpc_conn.get_vpc_id() vpc_conn.set_vpc_resource(vpc_id=my_vpc) net_config = set_network_config( dc_cfg=config.settings['dc_cfg'][my_region]) if not net_config: print('error set_network_config\n') sys.exit(-1) sec_conn = AwsSecGroup(aws_conn=aws_conn, cfgs=config.get_settings(), \ dc_cfg=net_config, vpc_id=my_vpc, ipv6=False\ ) if not sec_conn: print('error\n') sec_name_dict = sec_conn.get(vpc_id=my_vpc) autoscale_data = config.settings['autoscale'] for template in autoscale_data: temp_tag_dict = {} tags_list = [] sgs_list = [] template_data = autoscale_data[template] # create tag list for tag in template_data['tags']: # set the working dictonary and overwrite or skip as needd temp_tag_dict = {'Key': tag, 'Value': template_data['tags'][tag]} # we leave hostid empty if set to none if tag == 'hostid' and template_data['tags'][tag] == 'none': temp_tag_dict = {'Key': tag, 'Value': ''} # we do not add role if set to none if tag == 'role' and template_data['tags'][tag] == 'none': continue tags_list.append(temp_tag_dict) # create security group list for sec_group in template_data['SecurityGroups']: sec_id = sec_name_dict[sec_group] sgs_list.append(sec_id) try: result = autoscale_conn.create_launch_configuration( LaunchConfigurationName=template, ImageId=template_data['ImageId'], KeyName=template_data['KeyName'], InstanceType=template_data['InstanceType'], SecurityGroups=sgs_list, InstanceMonitoring={ 'Enabled': template_data['InstanceMonitoring'] }, PlacementTenancy=template_data['PlacementTenancy'], BlockDeviceMappings=[{ 'DeviceName': template_data['BlockDeviceMappings']['DeviceName'], 'Ebs': { 'VolumeSize': template_data['BlockDeviceMappings']['VolumeSize'], 'DeleteOnTermination': template_data['BlockDeviceMappings'] ['DeleteOnTermination'] } }], AssociatePublicIpAddress=template_data[ 'AssociatePublicIpAddress'], UserData=''.join(template_data['UserData'])) #autoscale_conn.create_or_update_tags(Tags=tags_list) print('{}'.format(result)) except Exception as err: critical('Unable to create launch template {}, error {}'.format( template, err)) sys.exit(0)
def main(): """ main """ my_logfile = 'logs/awsbuild.log' my_region = 'us-east-1' my_vpc = 'vpc-xxx' my_tag = 'momo-us-east-1' # setup logging log_formatter = logging.Formatter("%(asctime)s %(filename)s %(name)s %(levelname)s %(message)s") root_logger = logging.getLogger() file_handler = logging.FileHandler(my_logfile) file_handler.setFormatter(log_formatter) root_logger.addHandler(file_handler) console_handler = logging.StreamHandler() console_handler.setFormatter(log_formatter) root_logger.addHandler(console_handler) config = AwsConfig(cfgdir='configs',\ cfgfile='launch_template.yaml',\ cfgkey='launch_template') conn = AwsConnector(credentials=config.settings['aws_cfg']['credentials'], region=my_region) aws_conn = conn.get_all_conn() if not aws_conn: print('error AwsConnector\n') sys.exit(-1) ec2_conn = aws_conn['ec2_client'] if not ec2_conn: print('error AwsConnector for ec2_client\n') sys.exit(-1) vpc_conn = AwsVPC(aws_conn=aws_conn, tag=my_tag) if not vpc_conn: print('error AwsVPC\n') sys.exit(-1) #vpc_id = vpc_conn.get_vpc_id() vpc_conn.set_vpc_resource(vpc_id=my_vpc) net_config = set_network_config(dc_cfg=config.settings['dc_cfg'][my_region]) if not net_config: print('error set_network_config\n') sys.exit(-1) sec_conn = AwsSecGroup(aws_conn=aws_conn, cfgs=config.get_settings(), \ dc_cfg=net_config, vpc_id=my_vpc, ipv6=False\ ) if not sec_conn: print('error\n') sec_name_dict = sec_conn.get(vpc_id=my_vpc) templates_data = config.settings['launch_template'] for template in templates_data: temp_tag_dict = {} tags_list = [] sgs_list = [] template_data = templates_data[template] # create tag list for tag in template_data['TagSpecifications']: # set the working dictonary and overwrite or skip as needd temp_tag_dict = {'Key': tag, 'Value': template_data['TagSpecifications'][tag]} # we leave hostid empty if set to none if tag == 'hostid' and template_data['TagSpecifications'][tag] == 'none': temp_tag_dict = {'Key': tag, 'Value': ''} # we do not add role if set to none if tag == 'role' and template_data['TagSpecifications'][tag] == 'none': continue tags_list.append(temp_tag_dict) # create security group list for sec_group in template_data['SecurityGroupIds']: sec_id = sec_name_dict[sec_group] sgs_list.append(sec_id) try: result = ec2_conn.create_launch_template( LaunchTemplateName=template, VersionDescription=template_data['VersionDescription'], LaunchTemplateData={ 'IamInstanceProfile': { 'Arn': template_data['IamInstanceProfile'] }, 'BlockDeviceMappings': [{ 'DeviceName': template_data['BlockDeviceMappings']['DeviceName'], 'Ebs': { 'DeleteOnTermination': template_data['BlockDeviceMappings']['Ebs']['DeleteOnTermination'], 'VolumeSize': int(template_data['BlockDeviceMappings']['Ebs']['VolumeSize']) } }], 'ImageId': template_data['ImageId'], 'InstanceType': template_data['InstanceType'], 'KeyName': template_data['KeyName'], 'Monitoring': { 'Enabled': template_data['Monitoring'], }, 'Placement': { 'Tenancy': template_data['Placement']['Tenancy'] }, 'InstanceInitiatedShutdownBehavior': template_data['InstanceInitiatedShutdownBehavior'], 'UserData': ''.join(template_data['UserData']), 'TagSpecifications': [ { 'ResourceType': 'instance', 'Tags': tags_list }, { 'ResourceType': 'volume', 'Tags': tags_list } ], 'SecurityGroupIds': sgs_list } ) print('Launch template {} created, result {}.'.format(template, result)) except Exception as err: critical('Unable to create launch template {}, error {}'.format(template, err)) sys.exit(0)
def main(): """ main """ my_logfile = 'logs/awsbuild.log' my_region = 'us-east-1' my_vpc = 'vpc-xxx' my_tag = 'momo-us-east-1' # setup logging log_formatter = logging.Formatter( "%(asctime)s %(filename)s %(name)s %(levelname)s %(message)s") root_logger = logging.getLogger() file_handler = logging.FileHandler(my_logfile) file_handler.setFormatter(log_formatter) root_logger.addHandler(file_handler) console_handler = logging.StreamHandler() console_handler.setFormatter(log_formatter) root_logger.addHandler(console_handler) config = AwsConfig(cfgdir='configs',\ cfgfile='lb.yaml',\ cfgkey='elbs') conn = AwsConnector(credentials=config.settings['aws_cfg']['credentials'], region=my_region) aws_conn = conn.get_all_conn() if not aws_conn: print('error AwsConnector\n') sys.exit(-1) elbv2_conn = aws_conn['elbv2_client'] if not elbv2_conn: print('error AwsConnector for elbv2_client\n') sys.exit(-1) vpc_conn = AwsVPC(aws_conn=aws_conn, tag=my_tag) if not vpc_conn: print('error AwsVPC\n') sys.exit(-1) #vpc_id = vpc_conn.get_vpc_id() vpc_conn.set_vpc_resource(vpc_id=my_vpc) net_config = set_network_config( dc_cfg=config.settings['dc_cfg'][my_region]) if not net_config: print('error set_network_config\n') sys.exit(-1) sec_conn = AwsSecGroup(aws_conn=aws_conn, cfgs=config.get_settings(), \ dc_cfg=net_config, vpc_id=my_vpc, ipv6=False\ ) if not sec_conn: print('error\n') sec_name_dict = sec_conn.get(vpc_id=my_vpc) target_grp_conn = AwsTargetGroup(aws_conn=aws_conn, vpc_id=my_vpc, tag=my_tag) if not target_grp_conn: print('error AwsTargetGroup\n') sys.exit(-1) target_grp_dict = target_grp_conn.get_target_groups(named_key=True) elbs_data = config.settings['elbs'] lbs_info = {} for elb_data in elbs_data: # reset by each pass elb_sec_groups_list = [] elb_tag_list = [] temp_tag_dict = {} # elb_config = elbs_data[elb_data] elb_name = elb_data elb_scheme = elb_config['scheme'] elb_type = elb_config['type'] elb_ipaddresstype = elb_config['ipaddresstype'] elb_tag_list.append({'Key': 'Name', 'Value': elb_name}) for tag_set in elb_config['tag']: for tag_detail in tag_set: temp_tag_dict = { 'Key': tag_detail, 'Value': tag_set[tag_detail] } elb_tag_list.append(temp_tag_dict) if elb_type == 'application': for elb_sec_group in elb_config['securitygroups']: elb_sec_group_name = elb_sec_group elb_sec_groups_list.append(sec_name_dict[elb_sec_group_name]) if elb_scheme == 'internal': subnets_list = [ 'subnet-02faead28c21ed12a', 'subnet-08a48156d3d611758', 'subnet-0f8bc11728fcc4cc7' ] else: subnets_list = [ 'subnet-05f204849ad05086b', 'subnet-0a5676dd39d5923a4', 'subnet-017e90e9060f41fd0' ] # for charter all internal elb_scheme = 'internal' try: if elb_type == 'application': new_elb_info = elbv2_conn.create_load_balancer( Name=elb_name, Subnets=subnets_list, Scheme=elb_scheme, Tags=elb_tag_list, Type=elb_type, SecurityGroups=elb_sec_groups_list, IpAddressType=elb_ipaddresstype) else: new_elb_info = elbv2_conn.create_load_balancer( Name=elb_name, Subnets=subnets_list, Scheme=elb_scheme, Tags=elb_tag_list, Type=elb_type, IpAddressType=elb_ipaddresstype) except Exception as err: critical('Unable to create elb {}, error {}'.format(elb_name, err)) sys.exit(0) # get the ARN of the new created LB for new_elb_info in new_elb_info['LoadBalancers']: lbs_info[new_elb_info['LoadBalancerName']] =\ new_elb_info['LoadBalancerArn'] lb_arn = new_elb_info['LoadBalancerArn'] # create the listner then add to the LB, for now only support forward and fixed-response for listner_port in elb_config['listner']: default_type = elb_config['listner'][listner_port][ 'defaultactions']['Type'] if default_type == 'forward': default_value = elb_config['listner'][listner_port][ 'defaultactions']['TargetGroupArn'] default_arn = target_grp_dict[default_value] if default_type == 'fixed-response': default_value = elb_config['listner'][listner_port][ 'defaultactions']['StatusCode'] try: if default_type == 'forward': new_listener = elbv2_conn.create_listener( LoadBalancerArn=lb_arn, Protocol=elb_config['listner'][listner_port] ['protocol'], Port=listner_port, DefaultActions=[{ 'Type': default_type, 'TargetGroupArn': default_arn }]) if default_type == 'fixed-response': new_listener = elbv2_conn.create_listener( LoadBalancerArn=lb_arn, Protocol=elb_config['listner'][listner_port] ['protocol'], Port=listner_port, DefaultActions=[{ 'Type': default_type, 'FixedResponseConfig': { 'StatusCode': default_value } }]) except Exception as err: critical('Unable to create elb\'s listner {}, error {}'.format( elb_name, err)) sys.exit(0) # get the new listner arn for new_listener_detail in new_listener['Listeners']: new_listeners_arn = new_listener_detail['ListenerArn'] if elb_config['listner'][listner_port]['rules']: listner_rules = elb_config['listner'][listner_port]['rules'] for rule_detail in listner_rules: rule_priority = int(rule_detail) rule_conditions = listner_rules[rule_detail]['Conditions'] rule_action = listner_rules[rule_detail]['Actions'] if 'TargetGroupArn' in rule_action: target_grp_name = rule_action['TargetGroupArn'] target_grp_arn = target_grp_dict[target_grp_name] rule_action['TargetGroupArn'] = target_grp_arn try: elbv2_conn.create_rule(ListenerArn=new_listeners_arn, Conditions=[rule_conditions], Priority=rule_priority, Actions=[rule_action]) except Exception as err: critical( 'Unable to create elb\'s listner {}, error {}'.format( elb_name, err)) sys.exit(0)