def change_ip(instance_id): latest_ip = get_latest_ip(instance_id) if not latest_ip: raise RuntimeError('Invalid ec2 instance_id') allocation_id = get_allocation_id(latest_ip) new_ip = str(ec2.allocate_address()).split(':', 1)[-1] ec2.associate_address(instance_id, new_ip) ec2.release_address(allocation_id=allocation_id)
def allocate_address(ec2, domain, reuse_existing_ip_allowed): """ Allocate a new elastic IP address (when needed) and return it """ if reuse_existing_ip_allowed: domain_filter = {"domain": domain or "standard"} all_addresses = ec2.get_all_addresses(filters=domain_filter) unassociated_addresses = [a for a in all_addresses if not a.device_id] if unassociated_addresses: return unassociated_addresses[0] return ec2.allocate_address(domain=domain)
def allocate_address(ec2, domain, reuse_existing_ip_allowed): """ Allocate a new elastic IP address (when needed) and return it """ if reuse_existing_ip_allowed: domain_filter = {'domain': domain or 'standard'} all_addresses = ec2.get_all_addresses(filters=domain_filter) unassociated_addresses = [a for a in all_addresses if not a.device_id] if unassociated_addresses: return unassociated_addresses[0] return ec2.allocate_address(domain=domain)
def set_allocate_address(self, instance): region = str(instance.region).split(":") ec2 = boto.ec2.connect_to_region(region[1]) # Allocate an Elastic IP Address. This will be associated with your # account until you explicitly release it. address = ec2.allocate_address() # Associate our new Elastic IP Address with our instance. instance.use_ip(address)
def allocate_address(ec2, domain, module, reuse_existing_ip_allowed): """ Allocate a new elastic IP address (when needed) and return it """ # If we're in check mode, nothing else to do if module.check_mode: module.exit_json(change=True) if reuse_existing_ip_allowed: if domain: domain_filter = { 'domain' : domain } else: domain_filter = { 'domain' : 'standard' } all_addresses = ec2.get_all_addresses(filters=domain_filter) unassociated_addresses = filter(lambda a: not a.instance_id, all_addresses) if unassociated_addresses: address = unassociated_addresses[0]; else: address = ec2.allocate_address(domain=domain) else: address = ec2.allocate_address(domain=domain) return address
def get_limit(ec2, domain): limit = 0 temp_ips = [] try: while True: temp_ips.append(ec2.allocate_address(domain)) limit += 1 except: if not limit: print("Unable to allocate any IP addresses. Check your IAM credentials.") finally: for ip in temp_ips: ip.release() return limit
def allocate_address(ec2, domain, reuse_existing_ip_allowed): """ Allocate a new elastic IP address (when needed) and return it """ if reuse_existing_ip_allowed: domain_filter = {'domain': domain or 'standard'} all_addresses = ec2.get_all_addresses(filters=domain_filter) if domain == 'vpc': unassociated_addresses = [a for a in all_addresses if not a.association_id] else: unassociated_addresses = [a for a in all_addresses if not a.instance_id] if unassociated_addresses: return unassociated_addresses[0] return ec2.allocate_address(domain=domain)
def allocate(ec2, domain, number, tolerance): eips = [] subnets = Counter() for x in range(number + tolerance): if is_vpc: ip = ec2.allocate_address(domain) eips.append(ip) subnets[ip.public_ip.split(".")[2]] += 1 retry = False if subnets.most_common(1)[0][1] < number: retry = True for ip in eips: ip.release() return retry, subnets.most_common(1)[0][1]
def allocate_address(ec2, domain, reuse_existing_ip_allowed): """ Allocate a new elastic IP address (when needed) and return it """ if reuse_existing_ip_allowed: domain_filter = {'domain': domain or 'standard'} all_addresses = ec2.get_all_addresses(filters=domain_filter) if domain == 'vpc': unassociated_addresses = [ a for a in all_addresses if not a.association_id ] else: unassociated_addresses = [ a for a in all_addresses if not a.instance_id ] if unassociated_addresses: return unassociated_addresses[0], False return ec2.allocate_address(domain=domain), True
inst = reservation.instances[0] inst.add_tag("Name", "NAT Instance by Python") print "Tagged the instance!" while inst.state == 'pending': time.sleep(5) inst.update() #Change the attribute for NAT'ing ec2.modify_instance_attribute(inst.id, attribute='sourceDestCheck', value=False) print "Attribute changed!" #Assign elastic IP 54.173.234.49 to the NAT Instance eip = ec2.allocate_address(None, False) eip.associate(inst.id, None, None, False, False) #======================================================================== #======================================================================== # Create the Private Instance ec2 = boto.ec2.connect_to_region('us-east-1') reservation = ec2.run_instances('ami-b66ed3de', key_name='east_keypair', instance_type='t2.micro', security_group_ids=[sg_private.id], subnet_id=prvsub.id) inst = reservation.instances[0] inst.add_tag("Name", "Private Instance by Python")
def allocate_ip(): ec2 = boto.ec2.connect_to_region(region) address = ec2.allocate_address(domain="vpc", dry_run=False) return address.public_ip, address.allocation_id
inst = reservation.instances[0] inst.add_tag("Name","NAT Instance by Python") print "Tagged the instance!" while inst.state == 'pending': time.sleep(5) inst.update() #Change the attribute for NAT'ing ec2.modify_instance_attribute(inst.id, attribute='sourceDestCheck', value=False) print "Attribute changed!" #Assign elastic IP 54.173.234.49 to the NAT Instance eip = ec2.allocate_address(None, False) eip.associate(inst.id,None, None,False,False) #======================================================================== #======================================================================== # Create the Private Instance ec2 = boto.ec2.connect_to_region('us-east-1') reservation = ec2.run_instances( 'ami-b66ed3de', key_name='east_keypair', instance_type='t2.micro', security_group_ids=[sg_private.id], subnet_id=prvsub.id)
def allocate_ip(): ec2 = boto.ec2.connect_to_region(region) address = ec2.allocate_address(domain='vpc', dry_run=False) return address.public_ip, address.allocation_id
group.authorize(ip_protocol='tcp',from_port=80, to_port=80, cidr_ip='0.0.0.0/0') except ec2.ResponseError, e: if e.code == 'InvalidPermission.Duplicate': print e.code else: raise #Start a new instance reservation_object=ec2.run_instances(image_id='ami-cd5311fd',key_name=key_name,security_groups=[group_name],instance_type='t1.micro') instance = reservation_object.instances[0] while(instance.state != 'running'): instance.update() static_address = ec2.allocate_address() static_address.associate(instance.id) print 'Instance successfully launched' reservations = ec2.get_all_instances(instance_ids=[instance.id]) instance2 = reservations[0].instances[0] #Install packages for command in install_commands: os.system('ssh -o StrictHostKeyChecking=no -i '+path+ key_name+'.pem ubuntu@'+static_address.public_ip+' '+command) #Transfer files os.system('zip -r csc326 csc326/*') os.system('scp -o StrictHostKeyChecking=no -i '+path+ key_name+'.pem '+file_name+' ubuntu@'+static_address.public_ip+':~/'+remote_path) #Launch engine