i am new at this and here is a piece my code:
with open('Accounts.txt') as f: account_list = f.readlines() for account in account_list: while account_list: try: account = account.rstrip('\n') #strip end of account in accounts.txt assume_response = sts_default_role.assume_role( RoleArn = f'arn:aws:iam::{account}:role/user/user.infosec', RoleSessionName = 'test_session', ) print(f"Logged in to {account}"), print("test first short loop") break except ClientError: print(f"Couldn't login to {account}"), break assume_creds = assume_response['Credentials'] session = boto3.session.Session( aws_access_key_id=assume_creds['AccessKeyId'], aws_secret_access_key=assume_creds['SecretAccessKey'], aws_session_token=assume_creds['SessionToken'], ) print("test outside the loop") Here is my output:
Logged in to 733443824660 test first short loop test outside the loop Couldn't login to 111111222211 test outside the loop as you can see it works great, the only problem i have is that once i hit an exception where i cant log in to an account i do not want the code to go further, because it does not make sense for it to go further and print (test outside the loop) comment when you cant log in to the account.
Any thoughts?
exit()to break from the program