如何在 AWS CLI 中设置默认配置文件的名称?

How do I set the name of the default profile in AWS CLI?

当我发出命令 aws config list 时,我得到以下默认配置文件的输出:

      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                <not set>             None    None

但是,当我为命名配置文件发出命令时,我得到一个配置文件名称

$ aws configure list --profile MyProfile
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                MyProfile           manual    --profile

我已经尝试 aws configure set profile Default 通过阅读 'set' CLI command, I also tried aws configure set StoreAs Default because I thought that's what's the variable is named after I read this page 将默认配置文件命名为 Default。我想这样做是因为我想要两个配置文件,并且我想使用 AWS_DEFAULT_PROFILE 环境变量在配置文件之间切换。

您只需切换到您想要的配置文件(例如:在 Linux 中使用:export AWS_DEFAULT_PROFILE=MyProfile),然后使用 export AWS_DEFAULT_PROFILE=default 切换回默认配置文件。 'default' 是您创建第一个个人资料时为您指定的个人资料名称。

我也有多个配置文件,我使用AWS_DEFAULT_PROFILE来回切换。但是,我已将每个配置文件命名为具有描述性的名称,例如 aws-engineeringaws-production。然后,我可以使用 set AWS_DEFAULT_PROFILE=aws-engineering,我很高兴。

我的 ~/.aws/config 中没有指定默认配置文件,这是故意的,因此我总是必须明确选择我正在使用的环境。

在命令行设置 AWS_DEFAULT_PROFILE 环境变量应该指定配置文件。

参见:

例如:

Linux、macOS 或 Unix

export AWS_DEFAULT_PROFILE=user2

注意:要取消设置,运行:unset AWS_DEFAULT_PROFILE

要使更改持久化,请将以上行添加到您的 ~/.bashrc 用户文件中。

注意:您也可以使用AWS_PROFILE。参见:Named Profiles.

Windows

set AWS_DEFAULT_PROFILE=user2

来源:User Guide » Configuring the AWS CLI » Named Profiles.

另请参阅:A New and Standardized Way to Manage Credentials in the AWS SDKs

in Windows 如果您想确保 配置文件的更改在新的命令行会话中持续存在 更好用setx

setx AWS_DEFAULT_PROFILE profle_name

记得关闭命令提示符并打开一个新的