Skip to content

Instantly share code, notes, and snippets.

@atheiman
Last active January 19, 2024 13:21
Show Gist options
  • Save atheiman/be41e630b8f3e30c53594912abe3c033 to your computer and use it in GitHub Desktop.
Save atheiman/be41e630b8f3e30c53594912abe3c033 to your computer and use it in GitHub Desktop.
Get all accounts within an AWS Organizations organizational unit recursively (all accounts nested under any child OUs)
#!/bin/bash
if [ -z "$1" ]; then
echo "Error - Usage: $0 <parent-id>"
exit 1
fi
set -eu
# set -x
get_accounts_recursive() {
accounts="$(aws organizations list-accounts-for-parent --parent-id "$1" --output text --query 'Accounts[][Id]')"
for ou in $(aws organizations list-organizational-units-for-parent --parent-id "$1" --output text --query 'OrganizationalUnits[][Id]'); do
accounts="$accounts $(get_accounts_recursive "$ou")"
done
echo "$accounts" | xargs
}
get_accounts_recursive $1
#!/usr/bin/env python3
import boto3
orgs = boto3.client('organizations', region_name='us-east-1')
accounts_paginator = orgs.get_paginator('list_accounts_for_parent')
ou_paginator = orgs.get_paginator('list_organizational_units_for_parent')
def get_accounts_recursive(parent_id):
accounts = []
for page in accounts_paginator.paginate(ParentId=parent_id):
accounts += page['Accounts']
for page in ou_paginator.paginate(ParentId=parent_id):
for ou in page['OrganizationalUnits']:
accounts += get_accounts_recursive(ou['Id'])
return accounts
# Example organization structure:
#
# Org o-abcde12345/
# Root r-abcd/
# OU ou-abcd-aaaaaaaa/
# OU ou-abcd-bbbbbbbb/
# Account 'account-1' 111111111111 (master)
# OU ou-abcd-cccccccc/
# Account 'account-2' 222222222222
# OU ou-abcd-dddddddd/
# Account 'account-3' 333333333333
#
# Example usage:
from pprint import pprint as pp
pp(get_accounts_recursive('r-abcd'))
# [{'Arn': 'arn:aws:organizations::111111111111:account/o-abcde12345/222222222222',
# 'Email': '[email protected]',
# 'Id': '222222222222',
# 'JoinedMethod': 'INVITED',
# 'JoinedTimestamp': datetime.datetime(2020, 10, 5, 9, 9, 18, 655000, tzinfo=tzlocal()),
# 'Name': 'account-2',
# 'Status': 'ACTIVE'},
# {'Arn': 'arn:aws:organizations::111111111111:account/o-abcde12345/333333333333',
# 'Email': '[email protected]',
# 'Id': '333333333333',
# 'JoinedMethod': 'INVITED',
# 'JoinedTimestamp': datetime.datetime(2020, 10, 6, 12, 12, 21, 16000, tzinfo=tzlocal()),
# 'Name': 'account-3',
# 'Status': 'ACTIVE'},
# {'Arn': 'arn:aws:organizations::111111111111:account/o-abcde12345/111111111111',
# 'Email': '[email protected]',
# 'Id': '111111111111',
# 'JoinedMethod': 'INVITED',
# 'JoinedTimestamp': datetime.datetime(2020, 10, 5, 8, 52, 4, 697000, tzinfo=tzlocal()),
# 'Name': 'account-1',
# 'Status': 'ACTIVE'}]
pp(get_accounts_recursive('ou-abcd-bbbbbbbb'))
# [{'Arn': 'arn:aws:organizations::111111111111:account/o-abcde12345/111111111111',
# 'Email': '[email protected]',
# 'Id': '111111111111',
# 'JoinedMethod': 'INVITED',
# 'JoinedTimestamp': datetime.datetime(2020, 10, 5, 8, 52, 4, 697000, tzinfo=tzlocal()),
# 'Name': 'account-1',
# 'Status': 'ACTIVE'}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment