Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bsg-bob/d90b64e3ec63aaaac6e748c144c2fcc7 to your computer and use it in GitHub Desktop.
Save bsg-bob/d90b64e3ec63aaaac6e748c144c2fcc7 to your computer and use it in GitHub Desktop.
aws organizations cli
#list all accounts
aws organizations list-accounts
#list all active accounts
aws organizations list-accounts | jq -r '.Accounts[] | select(.Status == "ACTIVE") | "\(.Id) \(.Name)"'
#list all in-active accounts
aws organizations list-accounts | jq -r '.Accounts[] | select(.Status != "ACTIVE") | "\(.Id) \(.Name)"'
# list OU's of a parent
aws organizations list-organizational-units-for-parent --parent-id <ROOT-OU> | jq '.OrganizationalUnits[] | "\(.Id): \(.Name)"'
# accounts whose name end with Prod
aws organizations list-accounts | jq -r '.Accounts[] | select(.Status == "ACTIVE") | select(.Name|test("Prod$")|w "\(.Id) \(.Name)"'
aws organizations list-accounts | jq -r '.Accounts[] | select(.Status == "ACTIVE") | select(.Name|test("Prod$|Dev$|Test$"))| "\(.Id) \(.Name)"'
# list accounts whose name doesn't match the set of patterns, case insensitive
aws organizations list-accounts | jq -r '.Accounts[] | select(.Status == "ACTIVE") | select(.Name|test("^(?!.*(Prod$|QA$|dev$|test$).*)";"i"))| "\(.Id) \(.Name)"'
#list accounts whose name doesn't start with ORG
aws organizations list-accounts | jq -r '.Accounts[] | select(.Status == "ACTIVE") | select(.Name|test("^(?!.*(^ORG).*)";"i"))| "\(.Id) \(.Name)"'
# accounts whose name has Test, case insensitive
aws organizations list-accounts | jq -r '.Accounts[] | select(.Status == "ACTIVE") | select(.Name|test("TEST";"i"))| "\(.Id) \(.Name)"'
# list parent id of a given account
aws organizations list-parents --child-id <AWS-ACCT-NO> | jq -r '.Parents[].Id'
# list parent id of the accounts whose name end with Test, with xargs
aws organizations list-accounts | jq -r '.Accounts[] | select(.Status == "ACTIVE") | select(.Name|test("Test$"))| .Id' | xargs -t -L 1 -I {} aws organizations list-parents --child-id {} | jq -r '.Parents[].Id'
# list parent id of the accounts whose name end with Test, with for loop
for acc in $(aws organizations list-accounts | jq -r '.Accounts[] | select(.Status == "ACTIVE") | select(.Name|test("Test$"))| .Id'); do aws organizations list-parents --child-id $acc | jq -r '.Parents[].Id'; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment