Skip to content

Instantly share code, notes, and snippets.

@davidporter-id-au
Created May 20, 2018 10:57
Show Gist options
  • Save davidporter-id-au/1bb8e437ad772061caedc04baeddf25f to your computer and use it in GitHub Desktop.
Save davidporter-id-au/1bb8e437ad772061caedc04baeddf25f to your computer and use it in GitHub Desktop.
Get an amazon resource by tag, a helper script
#!/bin/bash -e
# A helper script to make finding thigs by AWS tag less annoying
# DO NOT USE THIS WITH UNTRUSTED INPUT, it's an arbitrary code execution attack
# waiting to happen.
if [[ $# -lt 1 ]]; then
echo "Usage: $0 <tag-key=tag-value> [... tag-key=tag-value]"
echo ""
echo "eg: $0 GitBranch=master Application=vision-adapter"
exit 1
fi
filterArgs=""
for arg in "$@"; do
key=$(echo "$arg" | sed 's/=.*$//g' | sed 's/[^a-zA-Z0-9_-]*//g')
value=$(echo "$arg" | sed 's/^.*=//g' | sed 's/[^a-zA-Z0-9_-]*//g' )
filterArgs="${filterArgs} 'Key=${key},Values=${value}'"
done
# bash is a lisp
# no... really the problem is that the filterargs need to be multiple key-value pairs
# passed to the tag-filters flag (https://github.com/aws/aws-cli/issues/582#issuecomment-31788606)
# and doing this without string templating is annoying
echo "aws resourcegroupstaggingapi get-resources --tag-filters $filterArgs --query 'ResourceTagMappingList[].ResourceARN'" | bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment