Last active
June 15, 2020 22:42
-
-
Save fruch/bb677370de66cf8e97cfc30056cc5ff4 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
| from itertools import chain | |
| import boto3 | |
| from aws_operations import CLOUD_ACCESS | |
| region_name = 'us-west-1' | |
| cloudformation = boto3.resource('cloudformation', region_name=region_name, **CLOUD_ACCESS) | |
| ec2 = boto3.resource('ec2', region_name=region_name, **CLOUD_ACCESS) | |
| ec2_client = boto3.client('ec2', region_name=region_name, **CLOUD_ACCESS) | |
| def get_resource(arn): | |
| if 'elastic-ip/' in arn: | |
| return ec2.VpcAddress(allocation_id=arn.split('/')[1]) | |
| if 'stack/' in arn: | |
| return cloudformation.Stack(arn.split('/')[1]) | |
| if 'internet-gateway/' in arn: | |
| return ec2.InternetGateway(arn.split('/')[1]) | |
| if 'network-interface/' in arn: | |
| return ec2.NetworkInterface(arn.split('/')[1]) | |
| if 'route-table/' in arn: | |
| return ec2.RouteTable(arn.split('/')[1]) | |
| if 'subnet/' in arn: | |
| return ec2.Subnet(arn.split('/')[1]) | |
| if 'security-group/' in arn: | |
| return ec2.SecurityGroup(arn.split('/')[1]) | |
| if 'vpc/' in arn: | |
| return ec2.Vpc(arn.split('/')[1]) | |
| def get_peerings(vpc_id): | |
| vpc_peers = ec2_client.describe_vpc_peering_connections( | |
| Filters=[{'Name': 'requester-vpc-info.vpc-id', 'Values': [vpc_id]}]) | |
| return [ vpc_peer['VpcPeeringConnectionId'] for vpc_peer in vpc_peers['VpcPeeringConnections']] | |
| def find_resources_by_cluster_id(cluster_id="1874"): | |
| print('looking for resources {}'.format(cluster_id)) | |
| tagging = boto3.client('resourcegroupstaggingapi', region_name=region_name, **CLOUD_ACCESS) | |
| paginator = tagging.get_paginator('get_resources') | |
| tag_mappings = chain.from_iterable( | |
| page['ResourceTagMappingList'] | |
| for page in paginator.paginate(TagFilters=[ | |
| { | |
| "Key": "ClusterID", | |
| "Values": [cluster_id] | |
| }]) | |
| ) | |
| left_resources = [] | |
| for t in tag_mappings: | |
| resource = get_resource(t['ResourceARN']) | |
| if resource: | |
| if hasattr(resource, 'name'): | |
| left_resources.append(resource.name) | |
| if hasattr(resource, 'events'): | |
| events = list(resource.events.all()) | |
| if events[0].resource_status_reason: | |
| print(events[0].resource_status_reason) | |
| print(events[1].resource_status_reason) | |
| left_resources.extend(get_peerings(events[1].physical_resource_id)) | |
| if hasattr(resource, 'id'): | |
| left_resources.append(resource.id) | |
| if hasattr(resource, 'allocation_id'): | |
| left_resources.append(resource.allocation_id) | |
| else: | |
| print(t['ResourceARN']) | |
| return left_resources | |
| a = cloudformation.stacks.all() | |
| for s in a: | |
| cluster_id = [t for t in s.tags if t['Key'] == 'ClusterID'][0]['Value'] | |
| print(find_resources_by_cluster_id(cluster_id)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment