如何临时切换AWS CLI配置文件?(含v1/v2版本适配方案)
Fixing AWS CLI Profile Switching Not Working
I ran into this exact gotcha not too long ago—turns out the environment variable you use to switch profiles depends entirely on which AWS CLI version you're running!
When you tried export AWS_PROFILE=user2 and it didn't take effect, that's probably because you're using AWS CLI v1 instead of v2. Here's the correct command for each version:
- For AWS CLI v1: You need to set the
AWS_DEFAULT_PROFILEenvironment variable to switch profiles:export AWS_DEFAULT_PROFILE=user2 - For AWS CLI v2: The
AWS_PROFILEvariable works directly, so your original command will work as expected:export AWS_PROFILE=user2
To make sure the switch was successful, you can verify the current authenticated identity with this command:
aws sts get-caller-identity
内容的提问来源于stack exchange,提问作者James Shapiro




