vpcutil.py 1.46 KB
Newer Older
1
import boto
2 3 4 5
import boto.rds2
import boto.rds

CFN_TAG_KEY = 'aws:cloudformation:stack-name'
6

7 8
def vpc_for_stack_name(stack_name, aws_id=None, aws_secret=None):
    cfn = boto.connect_cloudformation(aws_id, aws_secret)
9 10
    resources = cfn.list_stack_resources(stack_name)
    for resource in resources:
11 12 13
        if resource.resource_type == 'AWS::EC2::VPC':
            return resource.physical_resource_id

14

15
def stack_name_for_vpc(vpc_name, aws_id, aws_secret):
16
    vpc = boto.connect_vpc(aws_id, aws_secret)
17
    resource = vpc.get_all_vpcs(vpc_ids=[vpc_name])[0]
18 19
    if CFN_TAG_KEY in resource.tags:
        return resource.tags[CFN_TAG_KEY]
20 21 22
    else:
        msg = "VPC({}) is not part of a cloudformation stack.".format(vpc_name)
        raise Exception(msg)
23 24


25 26 27 28 29 30 31 32 33
def rds_subnet_group_name_for_stack_name(stack_name, region='us-east-1', aws_id=None, aws_secret=None):
    # Helper function to look up a subnet group name by stack name
    rds = boto.rds2.connect_to_region(region)
    vpc = vpc_for_stack_name(stack_name)
    for group in rds.describe_db_subnet_groups()['DescribeDBSubnetGroupsResponse']['DescribeDBSubnetGroupsResult']['DBSubnetGroups']:
        if group['VpcId'] == vpc:
            return group['DBSubnetGroupName']
    return None

34

35 36 37 38
def all_stack_names(region='us-east-1', aws_id=None, aws_secret=None):
    vpc_conn = boto.connect_vpc(aws_id, aws_secret)
    return [vpc.tags[CFN_TAG_KEY] for vpc in vpc_conn.get_all_vpcs()
            if CFN_TAG_KEY in vpc.tags.keys()]