Created
May 20, 2018 10:57
-
-
Save davidporter-id-au/1bb8e437ad772061caedc04baeddf25f to your computer and use it in GitHub Desktop.
Get an amazon resource by tag, a helper script
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
#!/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