Last active
August 29, 2015 14:17
-
-
Save kerin/1c3a01288c2f9b783031 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
subnets = { | |
'PublicSubnet1': { | |
'AZ': 'eu-west-1a', | |
'CIDR': '10.0.0.0/24' | |
}, | |
'PublicSubnet2': { | |
'AZ': 'eu-west-1b', | |
'CIDR': '10.0.1.0/24' | |
}, | |
'PublicSubnet3': { | |
'AZ': 'eu-west-1c', | |
'CIDR': '10.0.2.0/24' | |
}, | |
'PrivateSubnet1': { | |
'AZ': 'eu-west-1a', | |
'CIDR': '10.0.3.0/24' | |
}, | |
'PrivateSubnet2': { | |
'AZ': 'eu-west-1b', | |
'CIDR': '10.0.4.0/24' | |
}, | |
'PrivateSubnet3': { | |
'AZ': 'eu-west-1c', | |
'CIDR': '10.0.5.0/24' | |
}, | |
} | |
@task | |
def vpc_name2id(name='bototest'): | |
vpcs = ec2.describe_vpcs(Filters=[{'Name': 'tag:Name', | |
'Values': [name]}]) | |
if vpcs: | |
try: | |
return vpcs['Vpcs'][0]['VpcId'] | |
except (KeyError, IndexError): | |
pass | |
return None | |
@task | |
def create_vpc(vpc_name='bototest'): | |
vpc_id = vpc_name2id(vpc_name) | |
if not vpc_id: | |
print 'creating VPC %s' % vpc_name | |
vpc_id = ec2.create_vpc(CidrBlock=vpc_cidr)['Vpc']['VpcId'] | |
ec2.get_waiter('vpc_available').wait(VpcIds=[vpc_id]) | |
ec2.create_tags(Resources=[vpc_id], | |
Tags=[{'Key': 'Name', 'Value': vpc_name}]) | |
ec2.modify_vpc_attribute(VpcId=vpc_id, | |
EnableDnsHostnames={'Value': True}) | |
for subnet_name, config in subnets.items(): | |
print 'creating Subnet %s' % subnet_name | |
subnet_id = ec2.create_subnet( | |
VpcId=vpc_id, CidrBlock=config['CIDR'], | |
AvailabilityZone=config['AZ'])['Subnet']['SubnetId'] | |
ec2.get_waiter('subnet_available').wait(SubnetIds=[subnet_id]) | |
ec2.create_tags(Resources=[subnet_id], | |
Tags=[{'Key': 'Name', 'Value': subnet_name}]) | |
else: | |
print 'VPC %s already exists' % vpc_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment