Last active
November 8, 2021 07:22
-
-
Save netmarkjp/72a94f607eb82b8dcc8abf73ca3a784f to your computer and use it in GitHub Desktop.
List AWS Resources(Name, ARN, Tag) from Tag Manager output
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
#!python | |
# coding: utf-8 | |
import json | |
import os | |
import sys | |
def main(): | |
""" | |
Usage: | |
aws resourcegroupstaggingapi get-resources | tee aws-resources.json | |
# Markdown | |
python3 aws_resources.py aws-resources.json | tee aws-resources.md | |
# CSV | |
env OUTPUT_FORMAT=csv python3 aws_resources.py aws-resources.json | tee aws-resources.csv | |
""" | |
with open(sys.argv[1]) as fd: | |
resources = json.load(fd) | |
""" | |
resources_by_service: Dict[str, List[Dict[str, Any]]] = { | |
service: [ | |
{ | |
arn: str, # ResourceARN | |
name: str, # Name from Tags | |
tags: List[Dict[str, str]], # List of Tags( {"Key": key, "Value": value} ) | |
} | |
] | |
} | |
""" | |
resources_by_service = {} | |
for resource in resources.get("ResourceTagMappingList", []): | |
r = {} | |
r["arn"] = resource["ResourceARN"] | |
r["name"] = "".join([d["Value"] for d in resource["Tags"] if d["Key"] == "Name"]) | |
if not r["name"]: | |
r["name"] = "N/A" | |
r["tags"] = resource["Tags"] | |
r["tags"] = sorted(r["tags"], key=lambda x: (x["Key"], x["Value"])) | |
_arn_parts = r["arn"].split(":") | |
service = "" | |
if len(_arn_parts) >= 2: | |
service = _arn_parts[2] | |
r["service"] = service | |
resource_type = "" | |
if len(_arn_parts) >= 6: | |
resource_type = _arn_parts[5].split("/")[0] | |
r["resource_type"] = resource_type | |
resources_by_service.setdefault(service, []) | |
resources_by_service[service].append(r) | |
for k, v in resources_by_service.items(): | |
resources_by_service[k] = sorted(v, key=lambda x: (x["resource_type"], x["name"], x["arn"])) | |
if os.getenv("OUTPUT_FORMAT", "").lower() == "csv": | |
print("Service,ResourceType,Name,ARN,Tags") | |
for key in sorted(list(resources_by_service.keys())): | |
resources = resources_by_service[key] | |
for val in resources: | |
tags = " ".join([f"{v['Key']}:{v['Value']}" for v in val["tags"]]) | |
print(f"{key},{val['resource_type']},{val['name']},{val['arn']},{tags}") | |
else: | |
for key in sorted(list(resources_by_service.keys())): | |
resources = resources_by_service[key] | |
print(f"## Service: {key}({len(resources)} resources)") | |
print("") | |
print("ResourceType | Name | ARN | Tags") | |
print("--- | --- | --- | ---") | |
for val in resources: | |
tags = "<br />".join([f"{v['Key']}: {v['Value']}" for v in val["tags"]]) | |
print(f"{val['resource_type']} | {val['name']} | {val['arn']} | {tags}") | |
print("") | |
def _arn_comparator(arn: str): | |
if not arn: | |
return "" | |
parts = arn.split(":") | |
if not len(parts) < 6: | |
return "" | |
service = parts[2] | |
description = parts[5] | |
return (service, description) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment