@@ -318,6 +318,14 @@ def enable_detective(d_client: botocore.client.BaseClient, region: str, tags: di
318
318
logging .info (f'Amazon Detective is enabled in region { region } ' )
319
319
320
320
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
321
329
322
330
if __name__ == '__main__' :
323
331
args = setup_command_line ()
@@ -340,30 +348,33 @@ def enable_detective(d_client: botocore.client.BaseClient, region: str, tags: di
340
348
# In this case the traceback adds LOTS of value.
341
349
logging .exception (f'error creating session { e .args } ' )
342
350
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 ):
350
353
354
+ for region in detective_regions :
351
355
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
+
361
377
except NameError as e :
362
378
logging .error (f'account is not defined: { e } ' )
363
379
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