def create_subnet(vpc_id, cidr_block, availability_zone, subnet_name, region): vpc = VPCConnection(aws_access_key_id=access_key, aws_secret_access_key=secret_key) datacenters = vpc.create_subnet(vpc_id=vpc_id, cidr_block=cidr_block, availability_zone=availability_zone) ec2_conn = boto.ec2.connect_to_region(region, aws_access_key_id=access_key, aws_secret_access_key=secret_key) time.sleep(1) ec2_conn.create_tags(datacenters.id, {"Name": subnet_name, "Project": 'LiveLiveProd'}) return datacenters.id
def test_db_subnet_group(self): vpc_api = VPCConnection() rds_api = RDSConnection() vpc = vpc_api.create_vpc('10.0.0.0/16') az_list = vpc_api.get_all_zones(filters={'state': 'available'}) subnet = list() n = 0 for az in az_list: try: subnet.append( vpc_api.create_subnet(vpc.id, '10.0.' + str(n) + '.0/24', availability_zone=az.name)) n = n + 1 except: pass grp_name = 'db_subnet_group' + str(int(time.time())) subnet_group = rds_api.create_db_subnet_group( grp_name, grp_name, [subnet[0].id, subnet[1].id]) if not _is_ok(subnet_group, vpc.id, grp_name, [subnet[0].id, subnet[1].id]): raise Exception("create_db_subnet_group returned bad values") rds_api.modify_db_subnet_group(grp_name, description='new description') subnet_grps = rds_api.get_all_db_subnet_groups(name=grp_name) if not _is_ok(subnet_grps[0], vpc.id, 'new description', [subnet[0].id, subnet[1].id]): raise Exception( "modifying the subnet group desciption returned bad values") rds_api.modify_db_subnet_group(grp_name, subnet_ids=[subnet[1].id, subnet[2].id]) subnet_grps = rds_api.get_all_db_subnet_groups(name=grp_name) if not _is_ok(subnet_grps[0], vpc.id, 'new description', [subnet[1].id, subnet[2].id]): raise Exception( "modifying the subnet group subnets returned bad values") rds_api.delete_db_subnet_group(subnet_group.name) try: rds_api.get_all_db_subnet_groups(name=grp_name) raise Exception(subnet_group.name + " still accessible after delete_db_subnet_group") except: pass while n > 0: n = n - 1 vpc_api.delete_subnet(subnet[n].id) vpc_api.delete_vpc(vpc.id)
def create_subnet(vpc_id, cidr_block, availability_zone, subnet_name, region): vpc = VPCConnection(aws_access_key_id=access_key, aws_secret_access_key=secret_key) datacenters = vpc.create_subnet(vpc_id=vpc_id, cidr_block=cidr_block, availability_zone=availability_zone) ec2_conn = boto.ec2.connect_to_region(region, aws_access_key_id=access_key, aws_secret_access_key=secret_key) time.sleep(1) ec2_conn.create_tags(datacenters.id, { "Name": subnet_name, "Project": 'LiveLiveProd' }) return datacenters.id
def test_db_subnet_group(self): vpc_api = VPCConnection() rds_api = RDSConnection() vpc = vpc_api.create_vpc('10.0.0.0/16') az_list = vpc_api.get_all_zones(filters={'state':'available'}) subnet = list() n = 0; for az in az_list: try: subnet.append(vpc_api.create_subnet(vpc.id, '10.0.'+str(n)+'.0/24',availability_zone=az.name)) n = n+1 except: pass grp_name = 'db_subnet_group'+str(int(time.time())) subnet_group = rds_api.create_db_subnet_group(grp_name, grp_name, [subnet[0].id,subnet[1].id]) if not _is_ok(subnet_group, vpc.id, grp_name, [subnet[0].id,subnet[1].id]): raise Exception("create_db_subnet_group returned bad values") rds_api.modify_db_subnet_group(grp_name, description='new description') subnet_grps = rds_api.get_all_db_subnet_groups(name=grp_name) if not _is_ok(subnet_grps[0], vpc.id, 'new description', [subnet[0].id,subnet[1].id]): raise Exception("modifying the subnet group desciption returned bad values") rds_api.modify_db_subnet_group(grp_name, subnet_ids=[subnet[1].id,subnet[2].id]) subnet_grps = rds_api.get_all_db_subnet_groups(name=grp_name) if not _is_ok(subnet_grps[0], vpc.id, 'new description', [subnet[1].id,subnet[2].id]): raise Exception("modifying the subnet group subnets returned bad values") rds_api.delete_db_subnet_group(subnet_group.name) try: rds_api.get_all_db_subnet_groups(name=grp_name) raise Exception(subnet_group.name+" still accessible after delete_db_subnet_group") except: pass while n > 0: n = n-1 vpc_api.delete_subnet(subnet[n].id) vpc_api.delete_vpc(vpc.id)
def create_vpc(self, vpc_cidr, subnet_cidr): c = VPCConnection() vpc = c.create_vpc(vpc_cidr) vpc.add_tag('organization', value=self.organization) subnet = c.create_subnet(vpc.id, subnet_cidr) while True: try: subnet.add_tag('organization', value=self.organization) break except: pass db_vpc = organization_vpc.OrganizationVPC() org = organizations.Organizations() org.load(name=self.organization) print org db_vpc.set('organization_id', org.get('id')) db_vpc.set('vpc_id', vpc.id) db_vpc.set('subnet_id', subnet.id) db_vpc.insert() return vpc.id, subnet.id, org.get('id')
# Use existing VPC new_vpc = vpcc.get_all_vpcs(filters=[("cidrBlock", VPC_CIDR)]) ''' # Doesn't work due to VPC dependency print "Requested VPC already exists! Will attempt to delete vpc and recreate" del_status = vpcc.delete_vpc(aws_vpc_id) print "Deletion Completed", del_status ''' exist_subnet1 = vpcc.get_all_subnets(filters=[("cidrBlock", VPC_SUBNET1)]) exist_subnet2 = vpcc.get_all_subnets(filters=[("cidrBlock", VPC_SUBNET2)]) if not len(exist_subnet1): print "Creating new subnet ..." new_subnet1 = vpcc.create_subnet(aws_vpc_id, VPC_SUBNET1, AVAIL_ZONE1, dry_run=BOOLEAN_DRYRUN) subnet1_id = new_subnet1.id print "New subnet 1 ID: {}\n".format(subnet1_id) else: print "Subnet with {} already exists. Skipped creation".format( VPC_SUBNET1) subnet1_id = str(exist_subnet1.pop(0))[7:] print "Existing subnet 1 ID: {}\n".format(subnet1_id) if not len(exist_subnet2): print "Creating new subnet2 ..." new_subnet2 = vpcc.create_subnet(aws_vpc_id, VPC_SUBNET2, AVAIL_ZONE2, dry_run=BOOLEAN_DRYRUN)
def create_asg_with_tags(asg_name, tags, ami_id="ami-abcd1234", elbs=None): """ Create an ASG with the given name, tags and AMI. This is meant to be used in tests that are decorated with the @mock_autoscaling moto decorator. Arguments: asg_name(str): The name of the new auto-scaling group. tags(dict): A dict mapping tag names to tag values. ami_id(str): The ID of the AMI that should be deployed. Returns: boto.ec2.autoscale.group.AutoScalingGroup """ tag_list = [ Tag(key=k, value=v, resource_id=asg_name, propagate_at_launch=True) for k, v in six.iteritems(tags) ] if elbs is None: elbs = [] # Create asgs vpcconn = VPCConnection() conn = boto.ec2.autoscale.connect_to_region('us-east-1') config = LaunchConfiguration( name='{}_lc'.format(asg_name), image_id=ami_id, instance_type='t2.medium', ) conn.create_launch_configuration(config) vpc = vpcconn.create_vpc('10.0.0.0/24') subnetc = vpcconn.create_subnet(vpc.id, '10.0.0.0/28', 'us-east-1c') subnetb = vpcconn.create_subnet(vpc.id, '10.0.0.16/28', 'us-east-1b') group = AutoScalingGroup( name=asg_name, availability_zones=['us-east-1c', 'us-east-1b'], default_cooldown=60, desired_capacity=2, load_balancers=elbs, health_check_period=100, health_check_type="EC2", max_size=2, min_size=2, launch_config=config, placement_group="test_placement", vpc_zone_identifier="{subnetbid},{subnetcid}".format( subnetbid=subnetb.id, subnetcid=subnetc.id), termination_policies=["OldestInstance", "NewestInstance"], tags=tag_list, ) conn.create_auto_scaling_group(group) # Each ASG tag that has 'propagate_at_launch' set to True is *supposed* to be set on the instances. # However, it seems that moto (as of 0.4.30) does not properly set the tags on the instances created by the ASG. # So set the tags on the ASG instances manually instead. ec2_conn = boto.connect_ec2() for asg in conn.get_all_groups(): if asg.name == asg_name: asg_instance_ids = [ instance.instance_id for instance in asg.instances ] for instance_id in asg_instance_ids: ec2_conn.create_tags(instance_id, tags) return group
print "Existing VPC ID: {}\n".format(aws_vpc_id) # Use existing VPC new_vpc = vpcc.get_all_vpcs(filters=[("cidrBlock", VPC_CIDR)]) """ # Doesn't work due to VPC dependency print "Requested VPC already exists! Will attempt to delete vpc and recreate" del_status = vpcc.delete_vpc(aws_vpc_id) print "Deletion Completed", del_status """ exist_subnet1 = vpcc.get_all_subnets(filters=[("cidrBlock", VPC_SUBNET1)]) exist_subnet2 = vpcc.get_all_subnets(filters=[("cidrBlock", VPC_SUBNET2)]) if not len(exist_subnet1): print "Creating new subnet ..." new_subnet1 = vpcc.create_subnet(aws_vpc_id, VPC_SUBNET1, AVAIL_ZONE1, dry_run=BOOLEAN_DRYRUN) subnet1_id = new_subnet1.id print "New subnet 1 ID: {}\n".format(subnet1_id) else: print "Subnet with {} already exists. Skipped creation".format(VPC_SUBNET1) subnet1_id = str(exist_subnet1.pop(0))[7:] print "Existing subnet 1 ID: {}\n".format(subnet1_id) if not len(exist_subnet2): print "Creating new subnet2 ..." new_subnet2 = vpcc.create_subnet(aws_vpc_id, VPC_SUBNET2, AVAIL_ZONE2, dry_run=BOOLEAN_DRYRUN) subnet2_id = new_subnet2.id print "New subnet 2 ID: {}\n".format(subnet2_id) else: print "Subnet with {} already exists. Skipped creation".format(VPC_SUBNET2) subnet2_id = str(exist_subnet2.pop(0))[7:]
import time cloud_network = '10.1.0.0/16' blue_subnet = '10.1.0.0/24' green_subnet = '10.1.1.0/24' generic_linux_image = 'ami-a250ecca' nat_linux_image = 'ami-184dc970' connection = VPCConnection() private_cloud = connection.create_vpc(cloud_network) time.sleep(2) #fix race condition later private_cloud.add_tag('Name', 'virtual-private-cloud') print ("created vpc", private_cloud.id) private_subnet = connection.create_subnet(private_cloud.id, green_subnet) time.sleep(2) #fix race condition later private_subnet.add_tag('Name', 'green_subnet') public_subnet = connection.create_subnet(private_cloud.id, blue_subnet) time.sleep(2) public_subnet.add_tag('Name', 'blue_subnet') print "created public and private subnets" igw = connection.create_internet_gateway() connection.attach_internet_gateway(igw.id, private_cloud.id) print "created and attached internet gateway" host_security_group = connection.create_security_group('private_cloud_sg', 'private_cloud_sg', private_cloud.id) host_security_group.authorize('tcp', 80, 80, '0.0.0.0/0') host_security_group.authorize('tcp', 22, 22, '0.0.0.0/0')
def create_subnet(vpc, subnet_cidr): ec2region = vpc.region conn = VPCConnection(region=ec2region) return conn.create_subnet(vpc.id, subnet_cidr)
def create_security_group(ec2_conn,vpc_id,security_group_name): groups = ec2_conn.get_all_security_groups() for group in groups : if group.name == security_group_name : return group web = ec2_conn.create_security_group(security_group_name,vpc_id=vpc_id,description='ABC Talks Group') web.authorize('tcp',8080,8080,'0.0.0.0/0') return web def create_network_interface(ec2_conn,domain='vpc',public=False,subnet=None,groups=None,description=None): elastic_network_interface = ec2_conn.create_network_interface(subnet,groups=groups,description=description) if public: elastic_ip_address = ec2_conn.allocate_address(domain=domain) associated_address = ec2_conn.associate_address(allocation_id=elastic_ip_address.allocation_id,network_interface_id=elastic_network_interface.id) return elastic_netowrk_interface vpc_connection = VPCConnection(aws_access_key_id='',aws_secret_access_key='') vpc = vpc_connection.create_vpc('10.0.0.0/16') print vpc.id print vpc.state time.sleep(60) print vpc.state subnet = vpc_connection.create_subnet(vpc.id,'10.0.0.0/25') print subnet.state print subnet.available_ip_address_count ec2_conn = ec2.connect_to_region('us-east-1',aws_access_key_id='',aws_secret_access_key='') web = create_security_group(ec2_conn,vpc.id,'abctalks1') #address = address.Address(connection=ec2_conn,instance_id=ec2_instance.instances[0].id) print address network_interface = create_network_interface(ec2_conn,public=True,subnet=subnet,groups=[web.id],description='Storm Cluster') ec2_instance = create_ec2_instance(ec2_conn,'ami-d05e75b8','abctalks',get_user_data(),web.id,subnet.id,network_interface)
import time cloud_network = '10.1.0.0/16' blue_subnet = '10.1.0.0/24' green_subnet = '10.1.1.0/24' generic_linux_image = 'ami-a250ecca' nat_linux_image = 'ami-184dc970' connection = VPCConnection() private_cloud = connection.create_vpc(cloud_network) time.sleep(2) #fix race condition later private_cloud.add_tag('Name', 'virtual-private-cloud') print("created vpc", private_cloud.id) private_subnet = connection.create_subnet(private_cloud.id, green_subnet) time.sleep(2) #fix race condition later private_subnet.add_tag('Name', 'green_subnet') public_subnet = connection.create_subnet(private_cloud.id, blue_subnet) time.sleep(2) public_subnet.add_tag('Name', 'blue_subnet') print "created public and private subnets" igw = connection.create_internet_gateway() connection.attach_internet_gateway(igw.id, private_cloud.id) print "created and attached internet gateway" host_security_group = connection.create_security_group('private_cloud_sg', 'private_cloud_sg', private_cloud.id)