Created
September 10, 2016 06:16
-
-
Save jayeye/09f3a48a0a96f8f0b6230ae3a7286414 to your computer and use it in GitHub Desktop.
Create a vpc with all its accoutrements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from __future__ import print_function | |
import boto3 | |
import ipaddress | |
import sys | |
import time | |
ec2 = boto3.client('ec2') | |
def mkvpc(name, cidr, prefix_len, subnet_prefix_len, zones=['us-east-1e', 'us-east-1d', 'us-east-1c', 'us-east-1a']): | |
network = ipaddress.ip_network(str(cidr) + '/' + str(prefix_len)) | |
cidrblock = str(network) | |
n_subnets = len(zones) | |
subnets = list(network.subnets(new_prefix=subnet_prefix_len))[:n_subnets] | |
existing_vpcs = ec2.describe_vpcs() | |
for vpc in existing_vpcs.get('Vpcs', []): | |
if vpc.get('CidrBlock') == cidrblock: | |
print('{} already exists'.format(cidrblock), file=sys.stderr) | |
break | |
else: | |
vpc = ec2.create_vpc(CidrBlock=str(network), InstanceTenancy='default')['Vpc'] | |
is_available = False | |
while not is_available: | |
time.sleep(.1) | |
for v in ec2.describe_vpcs().get('Vpcs', []): | |
if v['CidrBlock'] == str(network) and v['State'] == 'available': | |
is_available = True | |
break | |
vpcid = vpc['VpcId'] | |
while True: | |
try: | |
ec2.create_tags(Resources=[vpcid], Tags=[{'Key': 'Name', 'Value': name}]) | |
break | |
except Exception as e: | |
print(str(e), file=sys.stderr) | |
time.sleep(.1) | |
igw = ec2.create_internet_gateway() | |
igwid = igw['InternetGateway']['InternetGatewayId'] | |
while True: | |
try: | |
ec2.attach_internet_gateway(InternetGatewayId=igwid, VpcId=vpcid) | |
break | |
except Exception as e: | |
print(str(e), file=sys.stderr) | |
time.sleep(.1) | |
while True: | |
try: | |
ec2.create_tags(Resources=[igwid], Tags=[{'Key': 'Name', 'Value': name}]) | |
break | |
except Exception as e: | |
print(str(e), file=sys.stderr) | |
time.sleep(.1) | |
drt = ec2.describe_route_tables(Filters=[{'Name': 'vpc-id', 'Values': [vpcid]}]) | |
rts = drt['RouteTables'] | |
assert len(rts) == 1 | |
rt = rts[0] | |
rtid = rt['RouteTableId'] | |
ec2.create_tags(Resources=[rtid], Tags=[{'Key': 'Name', 'Value': name}]) | |
ec2.create_route(RouteTableId=rtid, DestinationCidrBlock='0.0.0.0/0', GatewayId=igwid) | |
odhoid = vpc['DhcpOptionsId'] | |
dho = ec2.create_dhcp_options(DhcpConfigurations=[ | |
{ | |
'Key': 'domain-name', | |
'Values': ['net-10-20.example.com'], | |
}, | |
{ | |
'Key': 'domain-name-servers', | |
'Values': ['AmazonProvidedDNS'], | |
}, | |
]) | |
dhoid = dho['DhcpOptions']['DhcpOptionsId'] | |
while True: | |
try: | |
ec2.describe_dhcp_options(DhcpOptionsIds=[dhoid]) | |
break | |
except Exception as e: | |
print(str(e), file=sys.stderr) | |
time.sleep(.1) | |
ec2.create_tags(Resources=[dhoid], Tags=[{'Key': 'Name', 'Value': name}]) | |
ass = ec2.associate_dhcp_options(DhcpOptionsId=dhoid, VpcId=vpcid) | |
ec2.delete_dhcp_options(DhcpOptionsId=odhoid) | |
for i in range(n_subnets): | |
cs = ec2.create_subnet(VpcId=vpcid, CidrBlock=str(subnets[i]), AvailabilityZone=zones[i]) | |
sn = cs['Subnet'] | |
snid = sn['SubnetId'] | |
while True: | |
try: | |
ec2.create_tags(Resources=[snid], Tags=[{'Key': 'Name', 'Value': name + '-' + zones[i]}]) | |
break | |
except Exception as e: | |
print(str(e), file=sys.stderr) | |
time.sleep(.1) | |
mkvpc('prod', ipaddress.IPv4Address('10.20.0.0'), 16, 20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment