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.

Revisions

  1. rswrz created this gist Apr 21, 2021.
    30 changes: 30 additions & 0 deletions aws_search_resources.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/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" "$@"
    }
    17 changes: 17 additions & 0 deletions usage.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    #!/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