'how can i get Organization names for the fetched aws organizations for a parent id?

I fetched the organization list and the response includes the organization ids and the organization type. I want to fetch the organization names because my logic needs to look for a particular organization. How can i enhance the code to do that?

def get_ou_ids(parent_id): list_of_OU_ids = [] client = boto3.client('organizations')

paginator = client.get_paginator('list_children')
  iterator  = paginator.paginate(
  ParentId=parent_id,
  ChildType='ORGANIZATIONAL_UNIT'
)

for page in iterator:
  print(page['Children'])
  for ou in page['Children']:
    list_of_OU_ids.append(ou['Id'])
    list_of_OU_ids.extend(get_ou_ids(ou['Id']))

return list_of_OU_ids

Output:-

    [{'Id': 'ou-kfg9-68vbrelq', 'Type': 'ORGANIZATIONAL_UNIT'}, {'Id': 'ou-kfg9-7s4296qn', 'Type': 'ORGANIZATIONAL_UNIT'}, {'Id': 'ou-kfg9-tohoeosn', 'Type': 'ORGANIZATIONAL_UNIT'}, {'Id': 'ou-kfg9-39u8reeb', 'Type': 'ORGANIZATIONAL_UNIT'}, {'Id': 'ou-kfg9-9fxflop7', 'Type': 'ORGANIZATIONAL_UNIT'}]
['ou-kfg9-jtd17hu3', 'ou-kfg9-r1zyqdhf', 'ou-kfg9-68vbrelq', 'ou-kfg9-7s4296qn', 'ou-kfg9-tohoeosn', 'ou-kfg9-39u8reeb', 'ou-kfg9-9fxflop7']
    parent_Id in get_ou_name_id : r-kfg9


Solution 1:[1]

Not sure if this answers your question. You can do a describe_organizational_unit with the org_id. See boto documentation here

def get_org_details(org_id):
      org_details = org_client.describe_organizational_unit(OrganizationalUnitId=org_id)
      return org_details['OrganizationalUnit']

Solution 2:[2]

in addition to describe_organizational_unit, you can call list_organizational_units_for_parent (instead of 'list_children'); by doing so you are returned a dict with values:

    OrganizationalUnits:
        'Id': 'xxx123',
        'Arn': 'arn:aws:organizations:123:xxxxx',
        'Name': 'xxxxxxx' 

as opposed to

     Children: 
        'Id': 'ou-xxx-xxxxx', 
        'Type': 'xxxxxxxx',

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 coppaste
Solution 2 xxyjoel