Skip to content

Instantly share code, notes, and snippets.

@rswrz
Created April 21, 2021 11:51
Show Gist options
  • Select an option

  • Save rswrz/0e91763a5f3fb69b61b63d3d7b18d72e to your computer and use it in GitHub Desktop.

Select an option

Save rswrz/0e91763a5f3fb69b61b63d3d7b18d72e to your computer and use it in GitHub Desktop.
A simple Bash function that allows the usage of formated JSON with aws resource-group search
#!/bin/bash
aws_search_resources() {
# First argument has to be the JSON string
json=${1:?}
# All other arguments will be passed to aws cli
# e.g.: --region eu-central-1
shift
# Escape quotes in JSON string
json=${json//\"/\\\"}
# Remove \n (new lines) in JSON string
json=${json//$'\n'/}
# Template for search query
query='{
"Type": "TAG_FILTERS_1_0",
"Query": "__JSON__"
}'
# Insert JSON string into search query template
query=${query/__JSON__/$json}
# Execute aws cli
# Docs: https://docs.aws.amazon.com/cli/latest/reference/resource-groups/search-resources.html
aws resource-groups search-resources --resource-query "$query" "$@"
}
#!/bin/bash
# Simple query
aws_search_resources '{
"ResourceTypeFilters": [ "AWS::EC2::Instance" ],
"TagFilters": [
{ "Key": "Patch Group", "Values": [ "Dev" ] }
]
}'
# Query with additional aws cli arguments
aws_search_resources '{
"ResourceTypeFilters": [ "AWS::EC2::Instance" ],
"TagFilters": [
{ "Key": "Patch Group", "Values": [ "Dev" ] }
]
}' --max-items 7 --region eu-central-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment