|
| 1 | +import boto3 |
| 2 | + |
| 3 | +def get_resources_list(): |
| 4 | + try: |
| 5 | + client = boto3.client('resource-explorer-2') |
| 6 | + |
| 7 | + #AWS resource explorer view ARN ( Default use when not provided) |
| 8 | + view_arn = "arn:aws:resource-explorer-2:us-west-2:357171621133:view/all-resources/2a87ad65-7bf3-4be5-984f-864c935e6a3b" |
| 9 | + |
| 10 | + # Query string to filter resources without a specific tag key |
| 11 | + query_string = '-tag.key:Lab' |
| 12 | + |
| 13 | + # Initialize list to store resource ARNs |
| 14 | + list_of_resource_ids = [] |
| 15 | + |
| 16 | + # Paginate through results to retrieve all resources |
| 17 | + paginator = client.get_paginator('search') |
| 18 | + response_iterator = paginator.paginate( |
| 19 | + QueryString=query_string, |
| 20 | + ViewArn=view_arn |
| 21 | + ) |
| 22 | + |
| 23 | + for page in response_iterator: |
| 24 | + resources = page['Resources'] |
| 25 | + list_of_resource_ids.extend([item['Arn'] for item in resources]) |
| 26 | + |
| 27 | + # Remove duplicates if needed (though unlikely for ARNs) |
| 28 | + return list(set(list_of_resource_ids)) |
| 29 | + except Exception as e: |
| 30 | + print("Error in get resources list {}".format(e)) |
| 31 | + |
| 32 | +# function to group the resources by region |
| 33 | +def group_resources_by_region(list_of_resource_ids): |
| 34 | + try: |
| 35 | + # Group resources by region |
| 36 | + resource_groups = {} |
| 37 | + |
| 38 | + for arn in list_of_resource_ids: |
| 39 | + if ':' in arn: |
| 40 | + region = arn.split(':')[3] |
| 41 | + if region not in resource_groups: |
| 42 | + resource_groups[region] = [] |
| 43 | + resource_groups[region].append(arn) |
| 44 | + |
| 45 | + return resource_groups |
| 46 | + except Exception as e: |
| 47 | + print("Error in function to group the resources by region {}".format(e)) |
| 48 | + |
| 49 | +# Function to tag resources |
| 50 | +def tag_resources_by_region(resource_groups): |
| 51 | + try: |
| 52 | + Not_Tagged_List = [] |
| 53 | + for region, resource_list in resource_groups.items(): |
| 54 | + if region: |
| 55 | + tag_resources_client = boto3.client('resourcegroupstaggingapi', region_name=region) |
| 56 | + batches = [resource_list[i:i + 20] for i in range(0, len(resource_list), 20)] |
| 57 | + |
| 58 | + for batch in batches: |
| 59 | + tag_response = tag_resources_client.tag_resources( |
| 60 | + ResourceARNList=batch, |
| 61 | + Tags={'Lab' : 'Demo'} |
| 62 | + ) |
| 63 | + if tag_response['FailedResourcesMap']: |
| 64 | + Not_Tagged_List.extend(list(tag_response['FailedResourcesMap'].keys())) |
| 65 | + else: |
| 66 | + tag_resources_client = boto3.client('resourcegroupstaggingapi', region_name="us-east-1") |
| 67 | + batches = [resource_list[i:i + 20] for i in range(0, len(resource_list), 20)] |
| 68 | + |
| 69 | + for batch in batches: |
| 70 | + tag_response = tag_resources_client.tag_resources( |
| 71 | + ResourceARNList=batch, |
| 72 | + Tags={'Lab' : 'Demo'} |
| 73 | + ) |
| 74 | + if tag_response['FailedResourcesMap']: |
| 75 | + Not_Tagged_List.extend(list(tag_response['FailedResourcesMap'].keys())) |
| 76 | + return Not_Tagged_List |
| 77 | + except Exception as e: |
| 78 | + print("Error in tag resources by region function {}".format(e)) |
| 79 | + |
| 80 | +def lambda_handler(event, context): |
| 81 | + try: |
| 82 | + print("starting the execution now..") |
| 83 | + Resourcelist = get_resources_list() |
| 84 | + if Resourcelist: |
| 85 | + print("Invoking the resource grouping by regions value") |
| 86 | + ResourceGroups = group_resources_by_region(Resourcelist) |
| 87 | + if ResourceGroups: |
| 88 | + print("Invoking the tag_resources_by_region") |
| 89 | + TotalErrorResources = tag_resources_by_region(ResourceGroups) |
| 90 | + print (TotalErrorResources) |
| 91 | + print (len(TotalErrorResources)) |
| 92 | + except Exception as e: |
| 93 | + print("Error in face record creation {}".format(e)) |
0 commit comments