@@ -221,7 +221,7 @@ class AWSShell(object):
221221 """
222222
223223 def __init__ (self , completer , model_completer , docs ,
224- input = None , output = None ):
224+ env = None , input = None , output = None ):
225225 self .completer = completer
226226 self .model_completer = model_completer
227227 self .history = InMemoryHistory ()
@@ -232,10 +232,12 @@ def __init__(self, completer, model_completer, docs,
232232 self .refresh_cli = False
233233 self .key_manager = None
234234 self ._dot_cmd = DotCommandHandler ()
235- self ._env = os .environ .copy ()
236235 self ._profile = None
237236 self ._input = input
238237 self ._output = output
238+ self ._env = env
239+ if self ._env is None :
240+ self ._env = os .environ .copy ()
239241
240242 # These attrs come from the config file.
241243 self .config_obj = None
@@ -483,6 +485,11 @@ def profile(self, new_profile_name):
483485 # it's worth adding an event system or observers just yet.
484486 # If this gets hard to manage, the complexity of those systems
485487 # would be worth it.
488+
489+ # Remove explicit keys to ensure the profile will be used
490+ self ._env .pop ('AWS_ACCESS_KEY_ID' , None )
491+ self ._env .pop ('AWS_SECRET_ACCESS_KEY' , None )
492+
486493 self ._env ['AWS_DEFAULT_PROFILE' ] = new_profile_name
487494 self .completer .change_profile (new_profile_name )
488495 self ._profile = new_profile_name
0 commit comments