如何临时切换AWS CLI的配置文件?

Jam*_*iro 13 bash shell command-line amazon-web-services aws-cli

编辑:这是为我工作的解决方案:

export AWS_DEFAULT_PROFILE=user2
Run Code Online (Sandbox Code Playgroud)

完整问题如下所示:


(1.)成功为AWS CLI配置第二个配置文件后,我尝试使用以下命令在bash会话中将配置文件设置为user2失败:

export AWS_PROFILE=user2
Run Code Online (Sandbox Code Playgroud)

...根据这里的建议:https : //docs.aws.amazon.com/cli/latest/userguide/cli-multiple-profiles.html

(2.)以下命令有效:

aws s3 ls --profile user2
Run Code Online (Sandbox Code Playgroud)

因此,我知道AWS CLI和user2配置文件都在我的计算机上工作。

(3.)但是,当我随后(即输入“ export AWS_PROFILE = user2”后)尝试执行以下操作:

aws s3 ls
Run Code Online (Sandbox Code Playgroud)

... AWS的响应假定我想以默认用户身份查询它(不是user2)

(4.)因此,我可以从命令行使用user2配置文件的唯一方法是继续在每个命令后附加“ --profile user2”,这很繁琐。

(5.)

echo $AWS_PROFILE
Run Code Online (Sandbox Code Playgroud)

产量:

>> user2
Run Code Online (Sandbox Code Playgroud)

,正如预期的那样。

知道这里发生了什么吗?我确定我在某处犯了一些愚蠢的错误。

Mac*_*cio 32

接受的答案假设您使用的是 Linux 或 Mac 终端。我添加了适用于 Windows、Linux 和 Mac OS 的命令。

视窗

指令管理系统

set AWS_PROFILE=profile_name

电源外壳

$env:AWS_PROFILE = 'profile_name'

Linux 或 Mac

export AWS_PROFILE=profile_name

这些将设置您每次执行 aws 命令时将使用的 aws 配置文件。但如果您只想暂时切换一个 aws 命令的配置文件。

aws [command] [sub-command] --profile [profile-name]


Jam*_*iro 19

根据@kintuparantu的建议,对我有用的是:

export AWS_DEFAULT_PROFILE=user2
Run Code Online (Sandbox Code Playgroud)

之后,命令如下:

aws s3 ls
Run Code Online (Sandbox Code Playgroud)

...是从适当的帐户处理的。谢谢!


ox.*_*ox. 7

创建或编辑此文件:

% vim ~/.aws/credentials
Run Code Online (Sandbox Code Playgroud)

列出任意数量的密钥对:

[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

[user1]
aws_access_key_id=AKIAI44QH8DHBEXAMPLE
aws_secret_access_key=je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY
Run Code Online (Sandbox Code Playgroud)

包括--profile user1选择配置文件并执行您喜欢的操作:

aws s3api list-buckets --profile user1
# any aws cli command now using user1 pair of keys
Run Code Online (Sandbox Code Playgroud)

.... 或者 ....

设置一个局部变量来选择您要使用的键对:

% export AWS_PROFILE=user1
Run Code Online (Sandbox Code Playgroud)

然后做你喜欢做的事:

aws s3api list-buckets  # any aws cli command now using user1 pair of keys
Run Code Online (Sandbox Code Playgroud)

更多详细信息: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html


Die*_*ano 5

你可以看到它是如何工作的

$ export AWS_PROFILE=myprofile
$ aws s3 ls --debug 2>&1 | grep profile
2018-04-08 19:19:17,990 - MainThread - botocore.session - DEBUG - Loading variable profile from environment with value 'myprofile'.
Run Code Online (Sandbox Code Playgroud)

我怀疑这对你的工作方式不同。

您还可以验证

$ AWS_PROFILE=myprofile aws s3 ls --debug 2>&1 | grep profile
Run Code Online (Sandbox Code Playgroud)

$ aws s3 ls --profile myprofile --debug 2>&1 | grep profile
Run Code Online (Sandbox Code Playgroud)

都给出相同的结果。