@@ -318,6 +318,14 @@ def enable_detective(d_client: botocore.client.BaseClient, region: str, tags: di
318318 logging .info (f'Amazon Detective is enabled in region { region } ' )
319319
320320 return graphs
321+
322+ def chunked (it , size ):
323+ it = iter (it )
324+ while True :
325+ p = tuple (itertools .islice (it , size ))
326+ if not p :
327+ break
328+ yield p
321329
322330if __name__ == '__main__' :
323331 args = setup_command_line ()
@@ -340,30 +348,33 @@ def enable_detective(d_client: botocore.client.BaseClient, region: str, tags: di
340348 # In this case the traceback adds LOTS of value.
341349 logging .exception (f'error creating session { e .args } ' )
342350
343- for region in detective_regions :
344- try :
345- d_client = master_session .client ('detective' , region_name = region )
346- graphs = enable_detective (d_client , region , args .tags )
347-
348- if graphs is None :
349- continue
351+ #Chunk the list of accounts in the .csv into batches of 50 due to the API limitation of 50 accounts per invokation
352+ for chunk in chunked (aws_account_dict .items (), 50 ):
350353
354+ for region in detective_regions :
351355 try :
352- all_members , pending = get_members (d_client , graphs )
353-
354- for graph , members in all_members .items ():
355- new_accounts = create_members (
356- d_client , graph , args .disable_email , members , aws_account_dict )
357- print ("Sleeping for 5s to allow new members' invitations to propagate." )
358- time .sleep (5 )
359- accept_invitations (args .assume_role , itertools .chain (
360- new_accounts , pending [graph ]), graph , region )
356+ d_client = master_session .client ('detective' , region_name = region )
357+ graphs = enable_detective (d_client , region , args .tags )
358+
359+ if graphs is None :
360+ continue
361+
362+ try :
363+ all_members , pending = get_members (d_client , graphs )
364+
365+ for graph , members in all_members .items ():
366+ new_accounts = create_members (
367+ d_client , graph , args .disable_email , members , chunk )
368+ print ("Sleeping for 5s to allow new members' invitations to propagate." )
369+ time .sleep (5 )
370+ accept_invitations (args .assume_role , itertools .chain (
371+ new_accounts , pending [graph ]), graph , region )
372+ except NameError as e :
373+ logging .error (f'account is not defined: { e } ' )
374+ except Exception as e :
375+ logging .exception (f'unable to accept invitiation: { e } ' )
376+
361377 except NameError as e :
362378 logging .error (f'account is not defined: { e } ' )
363379 except Exception as e :
364- logging .exception (f'unable to accept invitiation: { e } ' )
365-
366- except NameError as e :
367- logging .error (f'account is not defined: { e } ' )
368- except Exception as e :
369- logging .exception (f'error with region { region } : { e } ' )
380+ logging .exception (f'error with region { region } : { e } ' )
0 commit comments