I use tkinter to create a GUI application that returns security groups. Currently, if you want to change your credentials (for example, if you accidentally entered the wrong ones), you will have to restart the application, otherwise boto3 will continue to use the old credentials.
I'm not sure why he continues to use the old credentials because I am starting everything up again using the credentials I have currently entered.
This is a piece of code that sets environment variables and starts boto3 . It works great if you enter the correct credentials for the first time.
os.environ['AWS_ACCESS_KEY_ID'] = self.accessKey os.environ['AWS_SECRET_ACCESS_KEY'] = self.secretKey self.sts_client = boto3.client('sts') self.assumedRoleObject = self.sts_client.assume_role( RoleArn=self.role, RoleSessionName="AssumeRoleSession1" ) self.credentials = self.assumedRoleObject['Credentials'] self.ec2 = boto3.resource( 'ec2', region_name=self.region, aws_access_key_id=credentials['AccessKeyId'], aws_secret_access_key=credentials['SecretAccessKey'], aws_session_token=credentials['SessionToken'], )
Credential variables are set using:
self.accessKey = str(self.AWS_ACCESS_KEY_ID_Form.get()) self.secretKey = str(self.AWS_SECRET_ACCESS_KEY_Form.get()) self.role = str(self.AWS_ROLE_ARN_Form.get()) self.region = str(self.AWS_REGION_Form.get()) self.instanceID = str(self.AWS_INSTANCE_ID_Form.get())
Is there a way to use different credentials in boto3 without restarting the program?
source share