Skip to content

Instantly share code, notes, and snippets.

@xbalaji
Last active March 7, 2024 01:12
Show Gist options
  • Save xbalaji/972454034166d95fe307cb41343412c9 to your computer and use it in GitHub Desktop.
Save xbalaji/972454034166d95fe307cb41343412c9 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)"'
# create a key value pair of account number to name
aws organizations list-accounts | jq -M '[.Accounts[] | select(.Status == "ACTIVE") | {(.Id): .Name}] | add | to_entries | sort_by(.key) | from_entries'
#list all in-active accounts
aws organizations list-accounts | jq -r '.Accounts[] | select(.Status != "ACTIVE") | "\(.Id) \(.Name)"'
# list status of specific accounts - select from array
aws organizations list-accounts | jq -Mr '.Accounts[] | select(any(.Id; IN("723683702622", "086565899246", "676225643038")))'
# skip accounts from an array - select not IN
aws organizations list-accounts | jq -Mr '.Accounts[] | select(any(.Id; IN("723683702622", "086565899246", "676225643038") | not))'
# 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