title: AWS - boto3 - how to list of all the ORG IDs(even nested) under organization
body: <p>I am trying to get a list of all the org ids under organization using boto3. The current structure is like this -</p>
  
  <pre><code>                          Root
                              |
                              |
                      ou1-----OU2-----OU3
                       |      |        |
                      ou4    ou5      ou6
                       |
                      ou7
                       |
                      ou8
  </code></pre>
  
  <p>This structure may change in future more ORG units might get added some of them may be deleted so I would like to make the function dynamic. I was hoping I could provide the Root id after which it should be able to find out all the org id under it. But this seems a little complicated as there is no existing API in boto3 which lists all the ORG ids under root. I would really appreciate if someone could give guidance/suggestion</p>
  
  <p>I have taken a look at -
  <a href="https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/organizations.html#Organizations.Client.list_children" rel="nofollow noreferrer">https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/organizations.html#Organizations.Client.list_children</a></p>
  
  <p><a href="https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/organizations.html#Organizations.Client.list_parents" rel="nofollow noreferrer">https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/organizations.html#Organizations.Client.list_parents</a></p>
  
  <p>but not sure how to interconnect them so it can find all the org ids, below is the code that I wrote but this will only fetch 2nd layer of children, that is till org4,5 and 6 </p>
  
  <pre><code>org = session.client("organizations")
      response = org.list_roots()
      for PolicyTypes in response["Roots"]:
          parent_id = PolicyTypes["Id"]
      OUlist = []
      NextToken = False
      while NextToken is not None:
          if not NextToken:
              response_iterator = org.list_organizational_units_for_parent(ParentId=parent_id, MaxResults=20)
          else:
              response_iterator = org.list_organizational_units_for_parent(ParentId=parent_id, MaxResults=20,
                                                                           NextToken=NextToken)
          OUlist = get_OUlist(OUlist, response_iterator)
          try:
              NextToken = response_iterator['NextToken']
          except KeyError:
              break
  
      get_child_ou(org, OUlist)
  
  
  
  def get_child_ou(org, OUlist):
      for ou in OUlist:
          NextToken = False
          while NextToken is not None:
              if not NextToken:
                  response_iterator = org.list_children(ParentId=ou, ChildType='ORGANIZATIONAL_UNIT', MaxResults=20)
              else:
                  response_iterator = org.list_children(ParentId=ou, ChildType='ORGANIZATIONAL_UNIT', NextToken=NextToken,
                                                        MaxResults=20)
              try:
                  NextToken = response_iterator['NextToken']
              except KeyError:
                  break
      for orgid in response_iterator["Children"]:
          OUlist.append(orgid["Id"])
      return OUlist
  </code></pre>