Created
December 16, 2025 14:59
-
-
Save nightspotlight/cc839e27473437737cdc78be70edcfc7 to your computer and use it in GitHub Desktop.
Get account name and OU from AWS account id
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| export AWS_PAGER="" | |
| declare -a acc_ids=( | |
| 012345678901 | |
| 123456789012 | |
| ) | |
| declare -a acc_names | |
| declare -a acc_ous | |
| for acc in "${acc_ids[@]}"; do | |
| printf "Checking %s... " "$acc" | |
| # get account name from AWS account id | |
| acc_name=$(aws organizations describe-account --account-id "$acc" \ | |
| | jq -cr '.Account.Name') | |
| printf "(%s) " "$acc_name" | |
| acc_names+=("$acc_name") | |
| # get organiational unit (OU) id from AWS account id | |
| acc_parent=$(aws organizations list-parents --child-id "$acc" \ | |
| | jq -cr '.Parents[] | select(.Type=="ORGANIZATIONAL_UNIT") | .Id') | |
| printf "%s\n" "$acc_parent" | |
| acc_ous+=("$acc_parent") | |
| done | |
| # print CSV of found accounts data | |
| printf "\n" | |
| printf "account_id,account_name,account_ou\n" | |
| for i in "${!acc_ids[@]}"; do | |
| printf "%s,%s,%s\n" "${acc_ids[$i]}" "${acc_names[$i]}" "${acc_ous[$i]}" | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment