Skip to content

Instantly share code, notes, and snippets.

@atheiman
Last active July 8, 2024 01:33
Show Gist options
  • Save atheiman/950cccf2c0c3fb98a09e2ae20b37d205 to your computer and use it in GitHub Desktop.
Save atheiman/950cccf2c0c3fb98a09e2ae20b37d205 to your computer and use it in GitHub Desktop.
Get all EC2 instances from an AWS Config Aggregator using AWS CLI
aws configservice batch-get-aggregate-resource-config \
--configuration-aggregator-name 'MyConfigAggregator' \
--resource-identifiers "$(
aws configservice list-aggregate-discovered-resources \
--configuration-aggregator-name 'MyConfigAggregator' \
--resource-type 'AWS::EC2::Instance' \
--query 'ResourceIdentifiers'
)"
@JackPott
Copy link

Very useful, thank you. I piped it into jq to break down the nested JSON string inside. In this case checking state and flattening the tags list out:

jq '[ .BaseConfigurationItems[] 
    | { accountId, 
      awsRegion, 
      resourceId, 
      state: (.configuration | fromjson | select(.state.name=="running")).state.name, 
      tags: (.configuration | fromjson | [.tags[]?] | from_entries) } ]'

Result:

{
    "accountId": "012345678901",
    "awsRegion": "us-west-2",
    "resourceId": "i-0e12345",
    "state": "running",
    "tags": {
      "Environment": "dev",
      "SystemsManager": "True",
      "Name": "my-special-server",
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment