list_accounts_in_organisations.sh
#!/bin/bash
# Lists just the accounts in an organiation,
# suitable for redirecting to a file to iterate over
# for processing something in all accounts.
# This gives a text output, the default is json, --output table is also valid.
aws organizations list-accounts --query 'Accounts[*].[Id]' --output text
# Using jq to parse the JSON
aws organizations list-accounts | jq -r '.Accounts.[].Id'
Iterates over the accounts in an organisation.
#!/bin/bash
ACCOUNTS_RAW=$(aws organizations list-accounts --query 'Accounts[*].[Id]' --output json | jq -c .[][] | tr -d '\n')
ACCOUNTS="${ACCOUNTS_RAW//\"/ }"
echo "accounts_raw:- ${ACCOUNTS_RAW}"
echo "accounts is:- $ACCOUNTS"
for ACCOUNT in $ACCOUNTS
do
echo "Account is >${ACCOUNT}<"
echo "do some work on each account"
done