@@ -286,6 +286,22 @@ def accept_invitations(role: str, accounts: typing.Set[str], graph: str, region:
286286 except Exception as e :
287287 logging .exception (f'error accepting invitation { e .args } ' )
288288
289+ def enable_detective (d_client : botocore .client .BaseClient , region : str ):
290+ graphs = get_graphs (d_client )
291+
292+ if not graphs :
293+ confirm = input ('Should Amazon Detective be enabled in {}? Enter [Y/N]: ' .format (region ))
294+
295+ if confirm == 'Y' or confirm == 'y' :
296+ logging .info (f'Enabling Amazon Detective in { region } ' )
297+ graphs = [d_client .create_graph ()['GraphArn' ]]
298+ else :
299+ logging .info (f'Skipping { region } ' )
300+ return None
301+ logging .info (f'Amazon Detective is enabled in region { region } ' )
302+
303+ return graphs
304+
289305if __name__ == '__main__' :
290306 args = setup_command_line ()
291307 aws_account_dict = read_accounts_csv (args .input_file )
@@ -310,19 +326,18 @@ def accept_invitations(role: str, accounts: typing.Set[str], graph: str, region:
310326 for region in detective_regions :
311327 try :
312328 d_client = master_session .client ('detective' , region_name = region )
313- graphs = get_graphs (d_client )
314- if not graphs :
315- logging . info ( f'AWS Detective is NOT enabled in { region } ' )
329+ graphs = enable_detective (d_client , region )
330+
331+ if graphs is None :
316332 continue
317- logging .info (f'AWS Detective is enabled in region { region } ' )
318333
319334 try :
320335 all_members , pending = get_members (d_client , graphs )
321336
322337 for graph , members in all_members .items ():
323338 new_accounts = create_members (
324339 d_client , graph , members , aws_account_dict )
325- print ("Sleeping for 5s to allow new members' invitations to propagate through DDB ." )
340+ print ("Sleeping for 5s to allow new members' invitations to propagate." )
326341 time .sleep (5 )
327342 accept_invitations (args .assume_role , itertools .chain (
328343 new_accounts , pending [graph ]), graph , region )
0 commit comments