Skip to content

Instantly share code, notes, and snippets.

@jaumzors
Created May 4, 2022 23:29
Show Gist options
  • Save jaumzors/04ae22029333771cc9998dbc6ccea7ba to your computer and use it in GitHub Desktop.
Save jaumzors/04ae22029333771cc9998dbc6ccea7ba to your computer and use it in GitHub Desktop.
quick and dirty: get aws resources by tags
from pprint import pprint
import boto3
client = boto3.client('resourcegroupstaggingapi')
def get_resources_tags():
resources = client.get_resources(PaginationToken='')
filtered = {res['ResourceARN']: [r['Value'] for r in res['Tags']]
for res in resources['ResourceTagMappingList'] if res['Tags']}
pprint(filtered)
def get_resources_tags_values(value=""):
resources = client.get_resources(PaginationToken='')
filtered = {res['ResourceARN']: [r['Value'] for r in res['Tags']]
for res in resources['ResourceTagMappingList']}
pprint(filtered)
def filter_resources_include_by_value(value=""):
resources = client.get_resources(PaginationToken='')
filtered = {res['ResourceARN'] for res in resources['ResourceTagMappingList']
if [r['Value'] for r in res['Tags'] if r['Value'] == value]}
pprint(filtered)
def filter_resources_exclude_by_value(value=""):
resources = client.get_resources(PaginationToken='')
filtered = {res['ResourceARN'] for res in resources['ResourceTagMappingList']
if [r['Value'] for r in res['Tags'] if r['Value'] != value]}
pprint(filtered)
get_resources_tags_values("k8s-instance_master_0")
filter_resources_include_by_value("k8s-instance_master_0")
filter_resources_exclude_by_value("k8s-instance_master_0")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment